Navaron Bracke

remove duplicate value notifier; update try catch in start camera; format FAB button

@@ -12,8 +12,6 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { @@ -12,8 +12,6 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
12 String overlayText = "Please scan QR Code"; 12 String overlayText = "Please scan QR Code";
13 bool camStarted = false; 13 bool camStarted = false;
14 14
15 - final ValueNotifier<bool> torchStateNotifier = ValueNotifier<bool>(false);  
16 -  
17 final MobileScannerController controller = MobileScannerController( 15 final MobileScannerController controller = MobileScannerController(
18 formats: const [BarcodeFormat.qrCode], 16 formats: const [BarcodeFormat.qrCode],
19 autoStart: false, 17 autoStart: false,
@@ -26,19 +24,26 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { @@ -26,19 +24,26 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
26 } 24 }
27 25
28 void startCamera() { 26 void startCamera() {
29 - try { 27 + if (camStarted) {
  28 + return;
  29 + }
  30 +
  31 + controller.start().then((_) {
  32 + if (mounted) {
30 setState(() { 33 setState(() {
31 - camStarted = !camStarted;  
32 - controller.start(); 34 + camStarted = true;
33 }); 35 });
34 - } on Exception catch (e) { 36 + }
  37 + }).catchError((Object error, StackTrace stackTrace) {
  38 + if (mounted) {
35 ScaffoldMessenger.of(context).showSnackBar( 39 ScaffoldMessenger.of(context).showSnackBar(
36 SnackBar( 40 SnackBar(
37 - content: Text('Something went wrong! $e'), 41 + content: Text('Something went wrong! $error'),
38 backgroundColor: Colors.red, 42 backgroundColor: Colors.red,
39 ), 43 ),
40 ); 44 );
41 } 45 }
  46 + });
42 } 47 }
43 48
44 void onBarcodeDetect(BarcodeCapture barcodeCapture) { 49 void onBarcodeDetect(BarcodeCapture barcodeCapture) {
@@ -112,20 +117,25 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { @@ -112,20 +117,25 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
112 child: Row( 117 child: Row(
113 mainAxisAlignment: MainAxisAlignment.spaceEvenly, 118 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
114 children: [ 119 children: [
115 - ValueListenableBuilder<bool>(  
116 - valueListenable: torchStateNotifier,  
117 - builder: (context, isTorchOn, child) { 120 + ValueListenableBuilder<TorchState>(
  121 + valueListenable: controller.torchState,
  122 + builder: (context, value, child) {
  123 + final Color iconColor;
  124 +
  125 + switch (value) {
  126 + case TorchState.off:
  127 + iconColor = Colors.black;
  128 + break;
  129 + case TorchState.on:
  130 + iconColor = Colors.yellow;
  131 + break;
  132 + }
  133 +
118 return IconButton( 134 return IconButton(
119 - onPressed: () {  
120 - controller.toggleTorch();  
121 - torchStateNotifier.value =  
122 - !torchStateNotifier.value;  
123 - }, 135 + onPressed: () => controller.toggleTorch(),
124 icon: Icon( 136 icon: Icon(
125 Icons.flashlight_on, 137 Icons.flashlight_on,
126 - color: isTorchOn  
127 - ? Colors.yellow  
128 - : Colors.black, 138 + color: iconColor,
129 ), 139 ),
130 ); 140 );
131 }, 141 },
@@ -153,12 +163,8 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { @@ -153,12 +163,8 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> {
153 floatingActionButton: camStarted 163 floatingActionButton: camStarted
154 ? null 164 ? null
155 : FloatingActionButton( 165 : FloatingActionButton(
156 - child: const Icon(  
157 - Icons.camera_alt,  
158 - ),  
159 - onPressed: () {  
160 - startCamera();  
161 - }, 166 + onPressed: startCamera,
  167 + child: const Icon(Icons.camera_alt),
162 ), 168 ),
163 ); 169 );
164 } 170 }