Showing
3 changed files
with
78 additions
and
14 deletions
| @@ -26,6 +26,31 @@ class _BarcodeListScannerWithControllerState | @@ -26,6 +26,31 @@ class _BarcodeListScannerWithControllerState | ||
| 26 | 26 | ||
| 27 | bool isStarted = true; | 27 | bool isStarted = true; |
| 28 | 28 | ||
| 29 | + void _startOrStop() { | ||
| 30 | + if (isStarted) { | ||
| 31 | + controller.stop(); | ||
| 32 | + } else { | ||
| 33 | + controller.start().catchError((error) { | ||
| 34 | + final exception = error as MobileScannerException; | ||
| 35 | + | ||
| 36 | + switch (exception.errorCode) { | ||
| 37 | + case MobileScannerErrorCode.controllerUninitialized: | ||
| 38 | + break; // This error code is not used by `start()`. | ||
| 39 | + case MobileScannerErrorCode.genericError: | ||
| 40 | + debugPrint('Scanner failed to start'); | ||
| 41 | + break; | ||
| 42 | + case MobileScannerErrorCode.permissionDenied: | ||
| 43 | + debugPrint('Camera permission denied'); | ||
| 44 | + break; | ||
| 45 | + } | ||
| 46 | + }); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + setState(() { | ||
| 50 | + isStarted = !isStarted; | ||
| 51 | + }); | ||
| 52 | + } | ||
| 53 | + | ||
| 29 | @override | 54 | @override |
| 30 | Widget build(BuildContext context) { | 55 | Widget build(BuildContext context) { |
| 31 | return Scaffold( | 56 | return Scaffold( |
| @@ -93,10 +118,7 @@ class _BarcodeListScannerWithControllerState | @@ -93,10 +118,7 @@ class _BarcodeListScannerWithControllerState | ||
| 93 | ? const Icon(Icons.stop) | 118 | ? const Icon(Icons.stop) |
| 94 | : const Icon(Icons.play_arrow), | 119 | : const Icon(Icons.play_arrow), |
| 95 | iconSize: 32.0, | 120 | iconSize: 32.0, |
| 96 | - onPressed: () => setState(() { | ||
| 97 | - isStarted ? controller.stop() : controller.start(); | ||
| 98 | - isStarted = !isStarted; | ||
| 99 | - }), | 121 | + onPressed: _startOrStop, |
| 100 | ), | 122 | ), |
| 101 | Center( | 123 | Center( |
| 102 | child: SizedBox( | 124 | child: SizedBox( |
| @@ -26,6 +26,31 @@ class _BarcodeScannerWithControllerState | @@ -26,6 +26,31 @@ class _BarcodeScannerWithControllerState | ||
| 26 | 26 | ||
| 27 | bool isStarted = true; | 27 | bool isStarted = true; |
| 28 | 28 | ||
| 29 | + void _startOrStop() { | ||
| 30 | + if (isStarted) { | ||
| 31 | + controller.stop(); | ||
| 32 | + } else { | ||
| 33 | + controller.start().catchError((error) { | ||
| 34 | + final exception = error as MobileScannerException; | ||
| 35 | + | ||
| 36 | + switch (exception.errorCode) { | ||
| 37 | + case MobileScannerErrorCode.controllerUninitialized: | ||
| 38 | + break; // This error code is not used by `start()`. | ||
| 39 | + case MobileScannerErrorCode.genericError: | ||
| 40 | + debugPrint('Scanner failed to start'); | ||
| 41 | + break; | ||
| 42 | + case MobileScannerErrorCode.permissionDenied: | ||
| 43 | + debugPrint('Camera permission denied'); | ||
| 44 | + break; | ||
| 45 | + } | ||
| 46 | + }); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + setState(() { | ||
| 50 | + isStarted = !isStarted; | ||
| 51 | + }); | ||
| 52 | + } | ||
| 53 | + | ||
| 29 | @override | 54 | @override |
| 30 | Widget build(BuildContext context) { | 55 | Widget build(BuildContext context) { |
| 31 | return Scaffold( | 56 | return Scaffold( |
| @@ -90,10 +115,7 @@ class _BarcodeScannerWithControllerState | @@ -90,10 +115,7 @@ class _BarcodeScannerWithControllerState | ||
| 90 | ? const Icon(Icons.stop) | 115 | ? const Icon(Icons.stop) |
| 91 | : const Icon(Icons.play_arrow), | 116 | : const Icon(Icons.play_arrow), |
| 92 | iconSize: 32.0, | 117 | iconSize: 32.0, |
| 93 | - onPressed: () => setState(() { | ||
| 94 | - isStarted ? controller.stop() : controller.start(); | ||
| 95 | - isStarted = !isStarted; | ||
| 96 | - }), | 118 | + onPressed: _startOrStop, |
| 97 | ), | 119 | ), |
| 98 | Center( | 120 | Center( |
| 99 | child: SizedBox( | 121 | child: SizedBox( |
| @@ -28,6 +28,31 @@ class _BarcodeScannerReturningImageState | @@ -28,6 +28,31 @@ class _BarcodeScannerReturningImageState | ||
| 28 | 28 | ||
| 29 | bool isStarted = true; | 29 | bool isStarted = true; |
| 30 | 30 | ||
| 31 | + void _startOrStop() { | ||
| 32 | + if (isStarted) { | ||
| 33 | + controller.stop(); | ||
| 34 | + } else { | ||
| 35 | + controller.start().catchError((error) { | ||
| 36 | + final exception = error as MobileScannerException; | ||
| 37 | + | ||
| 38 | + switch (exception.errorCode) { | ||
| 39 | + case MobileScannerErrorCode.controllerUninitialized: | ||
| 40 | + break; // This error code is not used by `start()`. | ||
| 41 | + case MobileScannerErrorCode.genericError: | ||
| 42 | + debugPrint('Scanner failed to start'); | ||
| 43 | + break; | ||
| 44 | + case MobileScannerErrorCode.permissionDenied: | ||
| 45 | + debugPrint('Camera permission denied'); | ||
| 46 | + break; | ||
| 47 | + } | ||
| 48 | + }); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + setState(() { | ||
| 52 | + isStarted = !isStarted; | ||
| 53 | + }); | ||
| 54 | + } | ||
| 55 | + | ||
| 31 | @override | 56 | @override |
| 32 | Widget build(BuildContext context) { | 57 | Widget build(BuildContext context) { |
| 33 | return Scaffold( | 58 | return Scaffold( |
| @@ -112,12 +137,7 @@ class _BarcodeScannerReturningImageState | @@ -112,12 +137,7 @@ class _BarcodeScannerReturningImageState | ||
| 112 | ? const Icon(Icons.stop) | 137 | ? const Icon(Icons.stop) |
| 113 | : const Icon(Icons.play_arrow), | 138 | : const Icon(Icons.play_arrow), |
| 114 | iconSize: 32.0, | 139 | iconSize: 32.0, |
| 115 | - onPressed: () => setState(() { | ||
| 116 | - isStarted | ||
| 117 | - ? controller.stop() | ||
| 118 | - : controller.start(); | ||
| 119 | - isStarted = !isStarted; | ||
| 120 | - }), | 140 | + onPressed: _startOrStop, |
| 121 | ), | 141 | ), |
| 122 | Center( | 142 | Center( |
| 123 | child: SizedBox( | 143 | child: SizedBox( |
-
Please register or login to post a comment