Committed by
GitHub
Merge pull request #53 from juliansteenbakker/example-app
bug: fixed pop() not working
Showing
3 changed files
with
24 additions
and
13 deletions
| @@ -25,8 +25,7 @@ class _BarcodeScannerWithControllerState | @@ -25,8 +25,7 @@ class _BarcodeScannerWithControllerState | ||
| 25 | 25 | ||
| 26 | @override | 26 | @override |
| 27 | Widget build(BuildContext context) { | 27 | Widget build(BuildContext context) { |
| 28 | - return MaterialApp( | ||
| 29 | - home: Scaffold( | 28 | + return Scaffold( |
| 30 | backgroundColor: Colors.black, | 29 | backgroundColor: Colors.black, |
| 31 | body: Builder(builder: (context) { | 30 | body: Builder(builder: (context) { |
| 32 | return Stack( | 31 | return Stack( |
| @@ -34,16 +33,15 @@ class _BarcodeScannerWithControllerState | @@ -34,16 +33,15 @@ class _BarcodeScannerWithControllerState | ||
| 34 | MobileScanner( | 33 | MobileScanner( |
| 35 | controller: controller, | 34 | controller: controller, |
| 36 | fit: BoxFit.contain, | 35 | fit: BoxFit.contain, |
| 36 | + allowDuplicates: false, | ||
| 37 | // controller: MobileScannerController( | 37 | // controller: MobileScannerController( |
| 38 | // torchEnabled: true, | 38 | // torchEnabled: true, |
| 39 | // facing: CameraFacing.front, | 39 | // facing: CameraFacing.front, |
| 40 | // ), | 40 | // ), |
| 41 | onDetect: (barcode, args) { | 41 | onDetect: (barcode, args) { |
| 42 | - if (this.barcode != barcode.rawValue) { | ||
| 43 | setState(() { | 42 | setState(() { |
| 44 | this.barcode = barcode.rawValue; | 43 | this.barcode = barcode.rawValue; |
| 45 | }); | 44 | }); |
| 46 | - } | ||
| 47 | }), | 45 | }), |
| 48 | Align( | 46 | Align( |
| 49 | alignment: Alignment.bottomCenter, | 47 | alignment: Alignment.bottomCenter, |
| @@ -150,7 +148,6 @@ class _BarcodeScannerWithControllerState | @@ -150,7 +148,6 @@ class _BarcodeScannerWithControllerState | ||
| 150 | ], | 148 | ], |
| 151 | ); | 149 | ); |
| 152 | }), | 150 | }), |
| 153 | - ), | ||
| 154 | ); | 151 | ); |
| 155 | } | 152 | } |
| 156 | } | 153 | } |
| @@ -16,20 +16,18 @@ class _BarcodeScannerWithoutControllerState | @@ -16,20 +16,18 @@ class _BarcodeScannerWithoutControllerState | ||
| 16 | 16 | ||
| 17 | @override | 17 | @override |
| 18 | Widget build(BuildContext context) { | 18 | Widget build(BuildContext context) { |
| 19 | - return MaterialApp( | ||
| 20 | - home: Scaffold( | 19 | + return Scaffold( |
| 21 | backgroundColor: Colors.black, | 20 | backgroundColor: Colors.black, |
| 22 | body: Builder(builder: (context) { | 21 | body: Builder(builder: (context) { |
| 23 | return Stack( | 22 | return Stack( |
| 24 | children: [ | 23 | children: [ |
| 25 | MobileScanner( | 24 | MobileScanner( |
| 26 | fit: BoxFit.contain, | 25 | fit: BoxFit.contain, |
| 26 | + allowDuplicates: false, | ||
| 27 | onDetect: (barcode, args) { | 27 | onDetect: (barcode, args) { |
| 28 | - if (this.barcode != barcode.rawValue) { | ||
| 29 | setState(() { | 28 | setState(() { |
| 30 | this.barcode = barcode.rawValue; | 29 | this.barcode = barcode.rawValue; |
| 31 | }); | 30 | }); |
| 32 | - } | ||
| 33 | }), | 31 | }), |
| 34 | Align( | 32 | Align( |
| 35 | alignment: Alignment.bottomCenter, | 33 | alignment: Alignment.bottomCenter, |
| @@ -64,7 +62,6 @@ class _BarcodeScannerWithoutControllerState | @@ -64,7 +62,6 @@ class _BarcodeScannerWithoutControllerState | ||
| 64 | ], | 62 | ], |
| 65 | ); | 63 | ); |
| 66 | }), | 64 | }), |
| 67 | - ), | ||
| 68 | ); | 65 | ); |
| 69 | } | 66 | } |
| 70 | } | 67 | } |
| @@ -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