idread.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'model/id_card_info_model.dart';
  4. typedef AsyncVoidFunction = Future<void> Function();
  5. class Idread {
  6. static const MethodChannel _methodChannel =
  7. const MethodChannel('com.hs.flutter.idRead/MethodChannel');
  8. static const EventChannel _eventChannel =
  9. const EventChannel('com.hs.flutter.idRead/EventChannel');
  10. static AsyncVoidFunction shellRead = () async => {};
  11. static AsyncVoidFunction shellStopRead = () async => {};
  12. static bool isShell = false;
  13. /// 检测扫码结果数据
  14. static void dataStreamListen(dynamic success, {dynamic error}) {
  15. if (isShell) {
  16. return;
  17. }
  18. _eventChannel.receiveBroadcastStream().listen((data) {
  19. success(IdCardInfoModel.fromJson(data));
  20. }, onError: error);
  21. }
  22. static Future<bool> init() async {
  23. if (isShell) {
  24. return true;
  25. }
  26. final bool result = await _methodChannel.invokeMethod('init');
  27. return result;
  28. }
  29. static Future<void> unInit() async {
  30. try {
  31. if (isShell) {
  32. return;
  33. }
  34. await _methodChannel.invokeMethod('unInit');
  35. } catch (e) {
  36. print(e.toString());
  37. }
  38. }
  39. static Future<bool> startRead() async {
  40. if (isShell) {
  41. shellRead.call();
  42. return true;
  43. }
  44. bool result = await _methodChannel.invokeMethod('startRead');
  45. return result;
  46. }
  47. static Future<void> stopRead() async {
  48. if (isShell) {
  49. shellStopRead.call();
  50. return;
  51. }
  52. await _methodChannel.invokeMethod('stopRead');
  53. }
  54. }