Showing
4 changed files
with
19 additions
and
12 deletions
| 1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:image_picker/image_picker.dart'; | 2 | import 'package:image_picker/image_picker.dart'; |
| 3 | import 'package:mobile_scanner/mobile_scanner.dart'; | 3 | import 'package:mobile_scanner/mobile_scanner.dart'; |
| 4 | +import 'package:mobile_scanner_example/scanner_error_widget.dart'; | ||
| 4 | 5 | ||
| 5 | class BarcodeListScannerWithController extends StatefulWidget { | 6 | class BarcodeListScannerWithController extends StatefulWidget { |
| 6 | const BarcodeListScannerWithController({Key? key}) : super(key: key); | 7 | const BarcodeListScannerWithController({Key? key}) : super(key: key); |
| @@ -25,7 +26,6 @@ class _BarcodeListScannerWithControllerState | @@ -25,7 +26,6 @@ class _BarcodeListScannerWithControllerState | ||
| 25 | ); | 26 | ); |
| 26 | 27 | ||
| 27 | bool isStarted = true; | 28 | bool isStarted = true; |
| 28 | - MobileScannerException? exception; | ||
| 29 | 29 | ||
| 30 | void _startOrStop() { | 30 | void _startOrStop() { |
| 31 | if (isStarted) { | 31 | if (isStarted) { |
| @@ -33,9 +33,7 @@ class _BarcodeListScannerWithControllerState | @@ -33,9 +33,7 @@ class _BarcodeListScannerWithControllerState | ||
| 33 | } else { | 33 | } else { |
| 34 | controller.start().catchError((error) { | 34 | controller.start().catchError((error) { |
| 35 | if (mounted) { | 35 | if (mounted) { |
| 36 | - setState(() { | ||
| 37 | - exception = error as MobileScannerException; | ||
| 38 | - }); | 36 | + setState(() {}); |
| 39 | } | 37 | } |
| 40 | }); | 38 | }); |
| 41 | } | 39 | } |
| @@ -55,6 +53,9 @@ class _BarcodeListScannerWithControllerState | @@ -55,6 +53,9 @@ class _BarcodeListScannerWithControllerState | ||
| 55 | children: [ | 53 | children: [ |
| 56 | MobileScanner( | 54 | MobileScanner( |
| 57 | controller: controller, | 55 | controller: controller, |
| 56 | + errorBuilder: (context, error, child) { | ||
| 57 | + return ScannerErrorWidget(error: error); | ||
| 58 | + }, | ||
| 58 | fit: BoxFit.contain, | 59 | fit: BoxFit.contain, |
| 59 | onDetect: (barcodeCapture) { | 60 | onDetect: (barcodeCapture) { |
| 60 | setState(() { | 61 | setState(() { |
| 1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:image_picker/image_picker.dart'; | 2 | import 'package:image_picker/image_picker.dart'; |
| 3 | import 'package:mobile_scanner/mobile_scanner.dart'; | 3 | import 'package:mobile_scanner/mobile_scanner.dart'; |
| 4 | +import 'package:mobile_scanner_example/scanner_error_widget.dart'; | ||
| 4 | 5 | ||
| 5 | class BarcodeScannerWithController extends StatefulWidget { | 6 | class BarcodeScannerWithController extends StatefulWidget { |
| 6 | const BarcodeScannerWithController({Key? key}) : super(key: key); | 7 | const BarcodeScannerWithController({Key? key}) : super(key: key); |
| @@ -25,7 +26,6 @@ class _BarcodeScannerWithControllerState | @@ -25,7 +26,6 @@ class _BarcodeScannerWithControllerState | ||
| 25 | ); | 26 | ); |
| 26 | 27 | ||
| 27 | bool isStarted = true; | 28 | bool isStarted = true; |
| 28 | - MobileScannerException? exception; | ||
| 29 | 29 | ||
| 30 | void _startOrStop() { | 30 | void _startOrStop() { |
| 31 | if (isStarted) { | 31 | if (isStarted) { |
| @@ -33,9 +33,7 @@ class _BarcodeScannerWithControllerState | @@ -33,9 +33,7 @@ class _BarcodeScannerWithControllerState | ||
| 33 | } else { | 33 | } else { |
| 34 | controller.start().catchError((error) { | 34 | controller.start().catchError((error) { |
| 35 | if (mounted) { | 35 | if (mounted) { |
| 36 | - setState(() { | ||
| 37 | - exception = error as MobileScannerException; | ||
| 38 | - }); | 36 | + setState(() {}); |
| 39 | } | 37 | } |
| 40 | }); | 38 | }); |
| 41 | } | 39 | } |
| @@ -55,6 +53,9 @@ class _BarcodeScannerWithControllerState | @@ -55,6 +53,9 @@ class _BarcodeScannerWithControllerState | ||
| 55 | children: [ | 53 | children: [ |
| 56 | MobileScanner( | 54 | MobileScanner( |
| 57 | controller: controller, | 55 | controller: controller, |
| 56 | + errorBuilder: (context, error, child) { | ||
| 57 | + return ScannerErrorWidget(error: error); | ||
| 58 | + }, | ||
| 58 | fit: BoxFit.contain, | 59 | fit: BoxFit.contain, |
| 59 | onDetect: (barcode) { | 60 | onDetect: (barcode) { |
| 60 | setState(() { | 61 | setState(() { |
| @@ -2,6 +2,7 @@ import 'dart:math'; | @@ -2,6 +2,7 @@ import 'dart:math'; | ||
| 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/scanner_error_widget.dart'; | ||
| 5 | 6 | ||
| 6 | class BarcodeScannerReturningImage extends StatefulWidget { | 7 | class BarcodeScannerReturningImage extends StatefulWidget { |
| 7 | const BarcodeScannerReturningImage({Key? key}) : super(key: key); | 8 | const BarcodeScannerReturningImage({Key? key}) : super(key: key); |
| @@ -27,7 +28,6 @@ class _BarcodeScannerReturningImageState | @@ -27,7 +28,6 @@ class _BarcodeScannerReturningImageState | ||
| 27 | ); | 28 | ); |
| 28 | 29 | ||
| 29 | bool isStarted = true; | 30 | bool isStarted = true; |
| 30 | - MobileScannerException? exception; | ||
| 31 | 31 | ||
| 32 | void _startOrStop() { | 32 | void _startOrStop() { |
| 33 | if (isStarted) { | 33 | if (isStarted) { |
| @@ -35,9 +35,7 @@ class _BarcodeScannerReturningImageState | @@ -35,9 +35,7 @@ class _BarcodeScannerReturningImageState | ||
| 35 | } else { | 35 | } else { |
| 36 | controller.start().catchError((error) { | 36 | controller.start().catchError((error) { |
| 37 | if (mounted) { | 37 | if (mounted) { |
| 38 | - setState(() { | ||
| 39 | - exception = error as MobileScannerException; | ||
| 40 | - }); | 38 | + setState(() {}); |
| 41 | } | 39 | } |
| 42 | }); | 40 | }); |
| 43 | } | 41 | } |
| @@ -77,6 +75,9 @@ class _BarcodeScannerReturningImageState | @@ -77,6 +75,9 @@ class _BarcodeScannerReturningImageState | ||
| 77 | children: [ | 75 | children: [ |
| 78 | MobileScanner( | 76 | MobileScanner( |
| 79 | controller: controller, | 77 | controller: controller, |
| 78 | + errorBuilder: (context, error, child) { | ||
| 79 | + return ScannerErrorWidget(error: error); | ||
| 80 | + }, | ||
| 80 | fit: BoxFit.contain, | 81 | fit: BoxFit.contain, |
| 81 | onDetect: (barcode) { | 82 | onDetect: (barcode) { |
| 82 | setState(() { | 83 | setState(() { |
| 1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:mobile_scanner/mobile_scanner.dart'; | 2 | import 'package:mobile_scanner/mobile_scanner.dart'; |
| 3 | +import 'package:mobile_scanner_example/scanner_error_widget.dart'; | ||
| 3 | 4 | ||
| 4 | class BarcodeScannerWithoutController extends StatefulWidget { | 5 | class BarcodeScannerWithoutController extends StatefulWidget { |
| 5 | const BarcodeScannerWithoutController({Key? key}) : super(key: key); | 6 | const BarcodeScannerWithoutController({Key? key}) : super(key: key); |
| @@ -24,6 +25,9 @@ class _BarcodeScannerWithoutControllerState | @@ -24,6 +25,9 @@ class _BarcodeScannerWithoutControllerState | ||
| 24 | children: [ | 25 | children: [ |
| 25 | MobileScanner( | 26 | MobileScanner( |
| 26 | fit: BoxFit.contain, | 27 | fit: BoxFit.contain, |
| 28 | + errorBuilder: (context, error, child) { | ||
| 29 | + return ScannerErrorWidget(error: error); | ||
| 30 | + }, | ||
| 27 | onDetect: (capture) { | 31 | onDetect: (capture) { |
| 28 | setState(() { | 32 | setState(() { |
| 29 | this.capture = capture; | 33 | this.capture = capture; |
-
Please register or login to post a comment