Showing
1 changed file
with
57 additions
and
8 deletions
| @@ -2,7 +2,6 @@ import 'dart:async'; | @@ -2,7 +2,6 @@ import 'dart:async'; | ||
| 2 | 2 | ||
| 3 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
| 4 | import 'package:mobile_scanner/mobile_scanner.dart'; | 4 | import 'package:mobile_scanner/mobile_scanner.dart'; |
| 5 | -import 'package:mobile_scanner_example/scanned_barcode_label.dart'; | ||
| 6 | import 'package:mobile_scanner_example/scanner_button_widgets.dart'; | 5 | import 'package:mobile_scanner_example/scanner_button_widgets.dart'; |
| 7 | import 'package:mobile_scanner_example/scanner_error_widget.dart'; | 6 | import 'package:mobile_scanner_example/scanner_error_widget.dart'; |
| 8 | 7 | ||
| @@ -15,7 +14,7 @@ class BarcodeScannerWithController extends StatefulWidget { | @@ -15,7 +14,7 @@ class BarcodeScannerWithController extends StatefulWidget { | ||
| 15 | } | 14 | } |
| 16 | 15 | ||
| 17 | class _BarcodeScannerWithControllerState | 16 | class _BarcodeScannerWithControllerState |
| 18 | - extends State<BarcodeScannerWithController> { | 17 | + extends State<BarcodeScannerWithController> with WidgetsBindingObserver { |
| 19 | final MobileScannerController controller = MobileScannerController( | 18 | final MobileScannerController controller = MobileScannerController( |
| 20 | torchEnabled: true, useNewCameraSelector: true, | 19 | torchEnabled: true, useNewCameraSelector: true, |
| 21 | // formats: [BarcodeFormat.qrCode] | 20 | // formats: [BarcodeFormat.qrCode] |
| @@ -25,10 +24,61 @@ class _BarcodeScannerWithControllerState | @@ -25,10 +24,61 @@ class _BarcodeScannerWithControllerState | ||
| 25 | // returnImage: false, | 24 | // returnImage: false, |
| 26 | ); | 25 | ); |
| 27 | 26 | ||
| 27 | + Barcode? _barcode; | ||
| 28 | + StreamSubscription<Object?>? _subscription; | ||
| 29 | + | ||
| 30 | + Widget _buildBarcode(Barcode? value) { | ||
| 31 | + if (value == null) { | ||
| 32 | + return const Text( | ||
| 33 | + 'Scan something!', | ||
| 34 | + overflow: TextOverflow.fade, | ||
| 35 | + style: TextStyle(color: Colors.white), | ||
| 36 | + ); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + return Text( | ||
| 40 | + value.displayValue ?? 'No display value.', | ||
| 41 | + overflow: TextOverflow.fade, | ||
| 42 | + style: const TextStyle(color: Colors.white), | ||
| 43 | + ); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + void _handleBarcode(BarcodeCapture barcodes) { | ||
| 47 | + if (mounted) { | ||
| 48 | + setState(() { | ||
| 49 | + _barcode = barcodes.barcodes.firstOrNull; | ||
| 50 | + }); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 28 | @override | 54 | @override |
| 29 | void initState() { | 55 | void initState() { |
| 30 | super.initState(); | 56 | super.initState(); |
| 31 | - controller.start(); | 57 | + WidgetsBinding.instance.addObserver(this); |
| 58 | + | ||
| 59 | + _subscription = controller.barcodes.listen(_handleBarcode); | ||
| 60 | + | ||
| 61 | + unawaited(controller.start()); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @override | ||
| 65 | + void didChangeAppLifecycleState(AppLifecycleState state) { | ||
| 66 | + super.didChangeAppLifecycleState(state); | ||
| 67 | + | ||
| 68 | + switch (state) { | ||
| 69 | + case AppLifecycleState.detached: | ||
| 70 | + case AppLifecycleState.hidden: | ||
| 71 | + case AppLifecycleState.paused: | ||
| 72 | + return; | ||
| 73 | + case AppLifecycleState.resumed: | ||
| 74 | + _subscription ??= controller.barcodes.listen(_handleBarcode); | ||
| 75 | + | ||
| 76 | + unawaited(controller.start()); | ||
| 77 | + case AppLifecycleState.inactive: | ||
| 78 | + unawaited(_subscription?.cancel()); | ||
| 79 | + _subscription = null; | ||
| 80 | + unawaited(controller.stop()); | ||
| 81 | + } | ||
| 32 | } | 82 | } |
| 33 | 83 | ||
| 34 | @override | 84 | @override |
| @@ -56,11 +106,7 @@ class _BarcodeScannerWithControllerState | @@ -56,11 +106,7 @@ class _BarcodeScannerWithControllerState | ||
| 56 | children: [ | 106 | children: [ |
| 57 | ToggleFlashlightButton(controller: controller), | 107 | ToggleFlashlightButton(controller: controller), |
| 58 | StartStopMobileScannerButton(controller: controller), | 108 | StartStopMobileScannerButton(controller: controller), |
| 59 | - Expanded( | ||
| 60 | - child: Center( | ||
| 61 | - child: ScannedBarcodeLabel(barcodes: controller.barcodes), | ||
| 62 | - ), | ||
| 63 | - ), | 109 | + Expanded(child: Center(child: _buildBarcode(_barcode))), |
| 64 | SwitchCameraButton(controller: controller), | 110 | SwitchCameraButton(controller: controller), |
| 65 | AnalyzeImageFromGalleryButton(controller: controller), | 111 | AnalyzeImageFromGalleryButton(controller: controller), |
| 66 | ], | 112 | ], |
| @@ -74,6 +120,9 @@ class _BarcodeScannerWithControllerState | @@ -74,6 +120,9 @@ class _BarcodeScannerWithControllerState | ||
| 74 | 120 | ||
| 75 | @override | 121 | @override |
| 76 | Future<void> dispose() async { | 122 | Future<void> dispose() async { |
| 123 | + WidgetsBinding.instance.removeObserver(this); | ||
| 124 | + unawaited(_subscription?.cancel()); | ||
| 125 | + _subscription = null; | ||
| 77 | super.dispose(); | 126 | super.dispose(); |
| 78 | await controller.dispose(); | 127 | await controller.dispose(); |
| 79 | } | 128 | } |
-
Please register or login to post a comment