|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter/services.dart'; |
| 5 | +import 'package:mobile_scanner/mobile_scanner.dart'; |
| 6 | +import 'package:mobile_scanner_example/picklist/classes/barcode_at_center.dart'; |
| 7 | +import 'package:mobile_scanner_example/picklist/widgets/crosshair.dart'; |
| 8 | +import 'package:mobile_scanner_example/scanner_error_widget.dart'; |
| 9 | + |
| 10 | +// This sample implements picklist functionality. |
| 11 | +// The scanning can temporarily be suspended by the user by touching the screen. |
| 12 | +// When the scanning is active, the crosshair turns red. |
| 13 | +// When the scanning is suspended, the crosshair turns green. |
| 14 | +// A barcode has to touch the center of viewfinder to be scanned. |
| 15 | +// Therefore the Crosshair widget needs to be placed at the center of the |
| 16 | +// MobileScanner widget to visually line up. |
| 17 | +class BarcodeScannerPicklist extends StatefulWidget { |
| 18 | + const BarcodeScannerPicklist({super.key}); |
| 19 | + |
| 20 | + @override |
| 21 | + State<BarcodeScannerPicklist> createState() => _BarcodeScannerPicklistState(); |
| 22 | +} |
| 23 | + |
| 24 | +class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> { |
| 25 | + final _mobileScannerController = MobileScannerController( |
| 26 | + // The controller is started from the initState method. |
| 27 | + autoStart: false, |
| 28 | + ); |
| 29 | + |
| 30 | + final orientation = DeviceOrientation.portraitUp; |
| 31 | + |
| 32 | + // On this subscription the barcodes are received. |
| 33 | + StreamSubscription<Object?>? _subscription; |
| 34 | + |
| 35 | + // This boolean indicates if the detection of barcodes is enabled or |
| 36 | + // temporarily suspended. |
| 37 | + final _scannerEnabled = ValueNotifier(true); |
| 38 | + |
| 39 | + // This boolean is used to prevent multiple pops. |
| 40 | + var _validBarcodeFound = false; |
| 41 | + |
| 42 | + @override |
| 43 | + void initState() { |
| 44 | + // Lock to portrait (may not work on iPad with multitasking). |
| 45 | + SystemChrome.setPreferredOrientations([orientation]); |
| 46 | + // Get a stream subscription and listen to received barcodes. |
| 47 | + _subscription = _mobileScannerController.barcodes.listen(_handleBarcodes); |
| 48 | + super.initState(); |
| 49 | + // Start the controller to start scanning. |
| 50 | + unawaited(_mobileScannerController.start()); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + void dispose() { |
| 55 | + // Cancel the stream subscription. |
| 56 | + unawaited(_subscription?.cancel()); |
| 57 | + _subscription = null; |
| 58 | + super.dispose(); |
| 59 | + // Dispose the controller. |
| 60 | + _mobileScannerController.dispose(); |
| 61 | + } |
| 62 | + |
| 63 | + // Check the list of barcodes only if scannerEnables is true. |
| 64 | + // Only take the barcode that is at the center of the image. |
| 65 | + // Return the barcode found to the calling page with the help of the |
| 66 | + // navigator. |
| 67 | + void _handleBarcodes(BarcodeCapture barcodeCapture) { |
| 68 | + // Discard all events when the scanner is disabled or when already a valid |
| 69 | + // barcode is found. |
| 70 | + if (!_scannerEnabled.value || _validBarcodeFound) { |
| 71 | + return; |
| 72 | + } |
| 73 | + final barcode = findBarcodeAtCenter(barcodeCapture, orientation); |
| 74 | + if (barcode != null) { |
| 75 | + _validBarcodeFound = true; |
| 76 | + Navigator.of(context).pop(barcode); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @override |
| 81 | + Widget build(BuildContext context) { |
| 82 | + return PopScope( |
| 83 | + onPopInvokedWithResult: (didPop, result) { |
| 84 | + // Reset the page orientation to the system default values, when this page is popped |
| 85 | + if (!didPop) { |
| 86 | + return; |
| 87 | + } |
| 88 | + SystemChrome.setPreferredOrientations(<DeviceOrientation>[]); |
| 89 | + }, |
| 90 | + child: Scaffold( |
| 91 | + appBar: AppBar(title: const Text('Picklist scanner')), |
| 92 | + backgroundColor: Colors.black, |
| 93 | + body: Listener( |
| 94 | + // Detect if the user touches the screen and disable/enable the scanner accordingly |
| 95 | + behavior: HitTestBehavior.opaque, |
| 96 | + onPointerDown: (_) => _scannerEnabled.value = false, |
| 97 | + onPointerUp: (_) => _scannerEnabled.value = true, |
| 98 | + onPointerCancel: (_) => _scannerEnabled.value = true, |
| 99 | + // A stack containing the image feed and the crosshair |
| 100 | + // The location of the crosshair must be at the center of the MobileScanner, otherwise the detection area and the visual representation do not line up. |
| 101 | + child: Stack( |
| 102 | + fit: StackFit.expand, |
| 103 | + children: [ |
| 104 | + MobileScanner( |
| 105 | + controller: _mobileScannerController, |
| 106 | + errorBuilder: (context, error, child) => |
| 107 | + ScannerErrorWidget(error: error), |
| 108 | + fit: BoxFit.contain, |
| 109 | + ), |
| 110 | + Crosshair(_scannerEnabled), |
| 111 | + ], |
| 112 | + ), |
| 113 | + ), |
| 114 | + ), |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments