1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import 'dart:async';
- import 'package:flutter/services.dart';
- import 'model/id_card_info_model.dart';
- typedef AsyncVoidFunction = Future<void> Function();
- class Idread {
- static const MethodChannel _methodChannel =
- const MethodChannel('com.hs.flutter.idRead/MethodChannel');
- static const EventChannel _eventChannel =
- const EventChannel('com.hs.flutter.idRead/EventChannel');
- static AsyncVoidFunction shellRead = () async => {};
- static AsyncVoidFunction shellStopRead = () async => {};
- static bool isShell = false;
- /// 检测扫码结果数据
- static void dataStreamListen(dynamic success, {dynamic error}) {
- if (isShell) {
- return;
- }
- _eventChannel.receiveBroadcastStream().listen((data) {
- success(IdCardInfoModel.fromJson(data));
- }, onError: error);
- }
- static Future<bool> init() async {
- if (isShell) {
- return true;
- }
- final bool result = await _methodChannel.invokeMethod('init');
- return result;
- }
- static Future<void> unInit() async {
- try {
- if (isShell) {
- return;
- }
- await _methodChannel.invokeMethod('unInit');
- } catch (e) {
- print(e.toString());
- }
- }
- static Future<bool> startRead() async {
- if (isShell) {
- shellRead.call();
- return true;
- }
- bool result = await _methodChannel.invokeMethod('startRead');
- return result;
- }
- static Future<void> stopRead() async {
- if (isShell) {
- shellStopRead.call();
- return;
- }
- await _methodChannel.invokeMethod('stopRead');
- }
- }
|