lig_scanner_sdk 0.5.0+1 copy "lig_scanner_sdk: ^0.5.0+1" to clipboard
lig_scanner_sdk: ^0.5.0+1 copied to clipboard

LiG Scanner SDK which empowers your app with LiGTag scanning capability.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:lig_scanner_sdk/lig_scanner_sdk.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _ligScannerSdkPlugin = LigScannerSdk();
  static const statusChannel = EventChannel("lig_scanner_sdk_status");
  static const resultChannel = EventChannel("lig_scanner_sdk_results");
  bool _supported = false;
  bool _authenticated = false;
  Offset _center = const Offset(0.9, 0.85);
  int _ligTagID = 0;

  final Permission _permission = Permission.camera;
  PermissionStatus _permissionStatus = PermissionStatus.denied;

  @override
  void initState() {
    super.initState();

    _listenForPermissionStatus();

    statusChannel.receiveBroadcastStream().listen(_onStatusReported, onError: _onChannelError);
    resultChannel.receiveBroadcastStream().listen(_onResultDelivered, onError: _onChannelError);

    _ligScannerSdkPlugin.initialize("79AA5-5F64B-2D40F-FE67B-145C3", "F0563-9DBC0-A2DE9-F1454-F0B64"); // iOS
    initPlatformState();
  }

  void _listenForPermissionStatus() async {
    final status = await _permission.status;
    setState(() => _permissionStatus = status);
  }

  void _onStatusReported(Object? status) {
    stdout.writeln('Status reported: $status');
    if (status == null) {
      return;
    }

    if (status is! int) {
      return;
    }

    switch (status) {
      case 17: // device is supported
      case 126:
        setState(() {
          _supported = true;
          // startupCheck();
        });
      case 20: // Authentication is ok
      case 300:
        setState(() {
          _authenticated = true;
          // startupCheck();
        });
      default:
        // no-op
    }
  }

  void startupCheck() {
    // if (_supported && _authenticated) {
    if (_authenticated) {
      _ligScannerSdkPlugin.start();
    }
  }

  void _onResultDelivered(Object? result) {
    if (result == null) {
      return;
    }

    if (result is! List<Object?>) {
      return;
    }

    for (final lightMap in result) {
      var map = lightMap as Map<Object?, Object?>;
      setState(() {
        var id = map['deviceId'] as int;
        if (id != _ligTagID) {
          _ligTagID = id;
        }
        _center = Offset(map['coordinateX'] as double, map['coordinateY'] as double);
      });
    }
  }

  void _onChannelError(Object error) {
    print(error);
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _ligScannerSdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: CustomPaint(
                painter: CustomCirclePainter(center: _center, id: _ligTagID),
                child: Container(
                ),
              ),
            ),

            Expanded(child: Container(
              color: Colors.white,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    TextButton(
                      onPressed: () async {
                        final status = await _permission.request();
                        setState(() => _permissionStatus = status);
                      },
                      child: const Text('Request Permission'),
                    ),
                    TextButton(
                      onPressed: () async {
                        if (_permissionStatus.isGranted) {
                          _ligScannerSdkPlugin.start();
                        } else {
                          final status = await _permission.request();
                          setState(() => _permissionStatus = status);
                        }
                      },
                      child: const Text('Start'),
                    ),
                    TextButton(
                      onPressed: () async {
                        _ligScannerSdkPlugin.stop();
                      },
                      child: const Text('Stop'),
                    ),
                  ],
                ),
              ),
            )),
          ],
        ),
      ),
    );
  }
}

class CustomCirclePainter extends CustomPainter {
  final Offset center;
  final int id;

  CustomCirclePainter({required this.center, required this.id});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.yellow // Replace with your desired color
      ..style = PaintingStyle.fill;
    final point = Offset(center.dx * size.width, center.dy * size.height);

    canvas.drawCircle(point, 30, paint);
    const textStyle = TextStyle(
      color: Colors.black,
      fontSize: 16,
    );
    final textSpan = TextSpan(
      text: '$id',
      style: textStyle,
    );
    final textPainter = TextPainter(
      text: textSpan,
      textDirection: TextDirection.ltr,
    );
    textPainter.layout(
      minWidth: 0,
      maxWidth: size.width,
    );
    final textPoint = Offset(point.dx - 10, point.dy - 10);
    textPainter.paint(canvas, textPoint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
    // Only repaint if the center or radius changes
  }
}
0
likes
130
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

LiG Scanner SDK which empowers your app with LiGTag scanning capability.

Homepage

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

camera, flutter, path, path_provider, permission_handler, plugin_platform_interface

More

Packages that depend on lig_scanner_sdk