Showing
1 changed file
with
85 additions
and
3 deletions
| 1 | -import 'package:flutter/foundation.dart'; | 1 | +import 'dart:async'; |
| 2 | + | ||
| 2 | import 'package:flutter/services.dart'; | 3 | import 'package:flutter/services.dart'; |
| 4 | +import 'package:flutter/widgets.dart'; | ||
| 5 | +import 'package:mobile_scanner/src/enums/camera_facing.dart'; | ||
| 6 | +import 'package:mobile_scanner/src/enums/torch_state.dart'; | ||
| 3 | import 'package:mobile_scanner/src/mobile_scanner_platform_interface.dart'; | 7 | import 'package:mobile_scanner/src/mobile_scanner_platform_interface.dart'; |
| 4 | 8 | ||
| 5 | /// An implementation of [MobileScannerPlatform] that uses method channels. | 9 | /// An implementation of [MobileScannerPlatform] that uses method channels. |
| 6 | class MethodChannelMobileScanner extends MobileScannerPlatform { | 10 | class MethodChannelMobileScanner extends MobileScannerPlatform { |
| 7 | /// The method channel used to interact with the native platform. | 11 | /// The method channel used to interact with the native platform. |
| 8 | @visibleForTesting | 12 | @visibleForTesting |
| 9 | - final methodChannel = const MethodChannel('dev.steenbakker.mobile_scanner/scanner/method'); | 13 | + final methodChannel = const MethodChannel( |
| 14 | + 'dev.steenbakker.mobile_scanner/scanner/method', | ||
| 15 | + ); | ||
| 10 | 16 | ||
| 11 | /// The event channel that sends back scanned barcode events. | 17 | /// The event channel that sends back scanned barcode events. |
| 12 | @visibleForTesting | 18 | @visibleForTesting |
| 13 | - final eventChannel = const EventChannel('dev.steenbakker.mobile_scanner/scanner/event'); | 19 | + final eventChannel = const EventChannel( |
| 20 | + 'dev.steenbakker.mobile_scanner/scanner/event', | ||
| 21 | + ); | ||
| 22 | + | ||
| 23 | + Stream<Map<Object?, Object?>>? _eventsStream; | ||
| 24 | + | ||
| 25 | + Stream<Map<Object?, Object?>> get eventsStream { | ||
| 26 | + _eventsStream ??= | ||
| 27 | + eventChannel.receiveBroadcastStream().cast<Map<Object?, Object?>>(); | ||
| 28 | + | ||
| 29 | + return _eventsStream!; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + int? _textureId; | ||
| 33 | + | ||
| 34 | + @override | ||
| 35 | + Stream<TorchState> get torchStateStream { | ||
| 36 | + return eventsStream | ||
| 37 | + .where((event) => event['name'] == 'torchState') | ||
| 38 | + .map((event) => TorchState.fromRawValue(event['data'] as int? ?? 0)); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + @override | ||
| 42 | + Stream<double> get zoomScaleStateStream { | ||
| 43 | + return eventsStream | ||
| 44 | + .where((event) => event['name'] == 'zoomScaleState') | ||
| 45 | + .map((event) => event['data'] as double? ?? 0.0); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @override | ||
| 49 | + Future<bool> analyzeImage(String path) async { | ||
| 50 | + final bool? result = await methodChannel.invokeMethod<bool>( | ||
| 51 | + 'analyzeImage', | ||
| 52 | + path, | ||
| 53 | + ); | ||
| 54 | + | ||
| 55 | + return result ?? false; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @override | ||
| 59 | + Widget buildCameraView() => Texture(textureId: _textureId!); | ||
| 60 | + | ||
| 61 | + @override | ||
| 62 | + Future<void> resetZoomScale() async { | ||
| 63 | + await methodChannel.invokeMethod<void>('resetScale'); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + @override | ||
| 67 | + Future<void> setTorchState(TorchState torchState) async { | ||
| 68 | + await methodChannel.invokeMethod<void>('torch', torchState.rawValue); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @override | ||
| 72 | + Future<void> setZoomScale(double zoomScale) async { | ||
| 73 | + await methodChannel.invokeMethod<void>('setScale', zoomScale); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + @override | ||
| 77 | + Future<void> start(CameraFacing cameraDirection) {} | ||
| 78 | + | ||
| 79 | + @override | ||
| 80 | + Future<void> stop() async { | ||
| 81 | + await methodChannel.invokeMethod<void>('stop'); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + @override | ||
| 85 | + Future<void> updateScanWindow(Rect? window) async { | ||
| 86 | + await methodChannel.invokeMethod<void>( | ||
| 87 | + 'updateScanWindow', | ||
| 88 | + {'rect': window}, | ||
| 89 | + ); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + @override | ||
| 93 | + Future<void> dispose() async { | ||
| 94 | + await stop(); | ||
| 95 | + } | ||
| 14 | } | 96 | } |
-
Please register or login to post a comment