idread.dart 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'model/id_card_info_model.dart';
  4. class Idread {
  5. static const MethodChannel _methodChannel = const MethodChannel('com.hs.flutter.idRead/MethodChannel');
  6. static const EventChannel _eventChannel = const EventChannel('com.hs.flutter.idRead/EventChannel');
  7. /// 检测扫码结果数据
  8. static void dataStreamListen(dynamic success, {dynamic error}) {
  9. _eventChannel.receiveBroadcastStream().listen((data) {
  10. success(IdCardInfoModel.fromJson(data));
  11. }, onError: error);
  12. }
  13. static Future<bool> init() async {
  14. final bool result = await _methodChannel.invokeMethod('init');
  15. return result;
  16. }
  17. static Future<void> unInit() async {
  18. try {
  19. await _methodChannel.invokeMethod('unInit');
  20. } catch (e) {
  21. print(e.toString());
  22. }
  23. }
  24. static Future<bool> startRead() async {
  25. bool result = await _methodChannel.invokeMethod('startRead');
  26. return result;
  27. }
  28. static Future<void> stopRead() async {
  29. await _methodChannel.invokeMethod('stopRead');
  30. }
  31. }