Showing
3 changed files
with
38 additions
and
23 deletions
| @@ -33,16 +33,15 @@ class _BarcodeScannerWithControllerState | @@ -33,16 +33,15 @@ class _BarcodeScannerWithControllerState | ||
| 33 | MobileScanner( | 33 | MobileScanner( |
| 34 | controller: controller, | 34 | controller: controller, |
| 35 | fit: BoxFit.contain, | 35 | fit: BoxFit.contain, |
| 36 | + allowDuplicates: false, | ||
| 36 | // controller: MobileScannerController( | 37 | // controller: MobileScannerController( |
| 37 | // torchEnabled: true, | 38 | // torchEnabled: true, |
| 38 | // facing: CameraFacing.front, | 39 | // facing: CameraFacing.front, |
| 39 | // ), | 40 | // ), |
| 40 | onDetect: (barcode, args) { | 41 | onDetect: (barcode, args) { |
| 41 | - if (this.barcode != barcode.rawValue) { | ||
| 42 | - setState(() { | ||
| 43 | - this.barcode = barcode.rawValue; | ||
| 44 | - }); | ||
| 45 | - } | 42 | + setState(() { |
| 43 | + this.barcode = barcode.rawValue; | ||
| 44 | + }); | ||
| 46 | }), | 45 | }), |
| 47 | Align( | 46 | Align( |
| 48 | alignment: Alignment.bottomCenter, | 47 | alignment: Alignment.bottomCenter, |
| @@ -73,17 +72,17 @@ class _BarcodeScannerWithControllerState | @@ -73,17 +72,17 @@ class _BarcodeScannerWithControllerState | ||
| 73 | onPressed: () => controller.toggleTorch(), | 72 | onPressed: () => controller.toggleTorch(), |
| 74 | ), | 73 | ), |
| 75 | IconButton( | 74 | IconButton( |
| 76 | - color: Colors.white, | ||
| 77 | - icon: isStarted | ||
| 78 | - ? const Icon(Icons.stop) | ||
| 79 | - : const Icon(Icons.play_arrow), | ||
| 80 | - iconSize: 32.0, | 75 | + color: Colors.white, |
| 76 | + icon: isStarted | ||
| 77 | + ? const Icon(Icons.stop) | ||
| 78 | + : const Icon(Icons.play_arrow), | ||
| 79 | + iconSize: 32.0, | ||
| 81 | onPressed: () => setState(() { | 80 | onPressed: () => setState(() { |
| 82 | - isStarted | ||
| 83 | - ? controller.stop() | ||
| 84 | - : controller.start(); | ||
| 85 | - isStarted = !isStarted; | ||
| 86 | - })), | 81 | + isStarted |
| 82 | + ? controller.stop() | ||
| 83 | + : controller.start(); | ||
| 84 | + isStarted = !isStarted; | ||
| 85 | + })), | ||
| 87 | Center( | 86 | Center( |
| 88 | child: SizedBox( | 87 | child: SizedBox( |
| 89 | width: MediaQuery.of(context).size.width - 200, | 88 | width: MediaQuery.of(context).size.width - 200, |
| @@ -23,12 +23,11 @@ class _BarcodeScannerWithoutControllerState | @@ -23,12 +23,11 @@ class _BarcodeScannerWithoutControllerState | ||
| 23 | children: [ | 23 | children: [ |
| 24 | MobileScanner( | 24 | MobileScanner( |
| 25 | fit: BoxFit.contain, | 25 | fit: BoxFit.contain, |
| 26 | + allowDuplicates: false, | ||
| 26 | onDetect: (barcode, args) { | 27 | onDetect: (barcode, args) { |
| 27 | - if (this.barcode != barcode.rawValue) { | ||
| 28 | - setState(() { | ||
| 29 | - this.barcode = barcode.rawValue; | ||
| 30 | - }); | ||
| 31 | - } | 28 | + setState(() { |
| 29 | + this.barcode = barcode.rawValue; | ||
| 30 | + }); | ||
| 32 | }), | 31 | }), |
| 33 | Align( | 32 | Align( |
| 34 | alignment: Alignment.bottomCenter, | 33 | alignment: Alignment.bottomCenter, |
| @@ -24,9 +24,16 @@ class MobileScanner extends StatefulWidget { | @@ -24,9 +24,16 @@ class MobileScanner extends StatefulWidget { | ||
| 24 | /// Handles how the widget should fit the screen. | 24 | /// Handles how the widget should fit the screen. |
| 25 | final BoxFit fit; | 25 | final BoxFit fit; |
| 26 | 26 | ||
| 27 | + /// Set to false if you don't want duplicate scans. | ||
| 28 | + final bool allowDuplicates; | ||
| 29 | + | ||
| 27 | /// Create a [MobileScanner] with a [controller], the [controller] must has been initialized. | 30 | /// Create a [MobileScanner] with a [controller], the [controller] must has been initialized. |
| 28 | const MobileScanner( | 31 | const MobileScanner( |
| 29 | - {Key? key, this.onDetect, this.controller, this.fit = BoxFit.cover}) | 32 | + {Key? key, |
| 33 | + this.onDetect, | ||
| 34 | + this.controller, | ||
| 35 | + this.fit = BoxFit.cover, | ||
| 36 | + this.allowDuplicates = true}) | ||
| 30 | : super(key: key); | 37 | : super(key: key); |
| 31 | 38 | ||
| 32 | @override | 39 | @override |
| @@ -58,6 +65,8 @@ class _MobileScannerState extends State<MobileScanner> | @@ -58,6 +65,8 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 58 | } | 65 | } |
| 59 | } | 66 | } |
| 60 | 67 | ||
| 68 | + String? lastScanned; | ||
| 69 | + | ||
| 61 | @override | 70 | @override |
| 62 | Widget build(BuildContext context) { | 71 | Widget build(BuildContext context) { |
| 63 | return LayoutBuilder(builder: (context, BoxConstraints constraints) { | 72 | return LayoutBuilder(builder: (context, BoxConstraints constraints) { |
| @@ -68,8 +77,16 @@ class _MobileScannerState extends State<MobileScanner> | @@ -68,8 +77,16 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 68 | if (value == null) { | 77 | if (value == null) { |
| 69 | return Container(color: Colors.black); | 78 | return Container(color: Colors.black); |
| 70 | } else { | 79 | } else { |
| 71 | - controller.barcodes.listen( | ||
| 72 | - (a) => widget.onDetect!(a, value as MobileScannerArguments)); | 80 | + controller.barcodes.listen((barcode) { |
| 81 | + if (!widget.allowDuplicates) { | ||
| 82 | + if (lastScanned != barcode.rawValue) { | ||
| 83 | + lastScanned = barcode.rawValue; | ||
| 84 | + widget.onDetect!(barcode, value as MobileScannerArguments); | ||
| 85 | + } | ||
| 86 | + } else { | ||
| 87 | + widget.onDetect!(barcode, value as MobileScannerArguments); | ||
| 88 | + } | ||
| 89 | + }); | ||
| 73 | return ClipRect( | 90 | return ClipRect( |
| 74 | child: SizedBox( | 91 | child: SizedBox( |
| 75 | width: MediaQuery.of(context).size.width, | 92 | width: MediaQuery.of(context).size.width, |
-
Please register or login to post a comment