Showing
1 changed file
with
46 additions
and
15 deletions
| @@ -11,30 +11,42 @@ class BarcodeScannerWithOverlay extends StatefulWidget { | @@ -11,30 +11,42 @@ class BarcodeScannerWithOverlay extends StatefulWidget { | ||
| 11 | class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | 11 | 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 | - BarcodeCapture? currentBarcodeCapture; | 14 | + |
| 15 | + final ValueNotifier<bool> torchStateNotifier = ValueNotifier<bool>(false); | ||
| 16 | + | ||
| 15 | final MobileScannerController controller = MobileScannerController( | 17 | final MobileScannerController controller = MobileScannerController( |
| 16 | - facing: CameraFacing.back, | ||
| 17 | formats: const [BarcodeFormat.qrCode], | 18 | formats: const [BarcodeFormat.qrCode], |
| 18 | autoStart: false, | 19 | autoStart: false, |
| 19 | ); | 20 | ); |
| 20 | 21 | ||
| 21 | @override | 22 | @override |
| 22 | - dispose() { | 23 | + void dispose() { |
| 23 | controller.dispose(); | 24 | controller.dispose(); |
| 24 | super.dispose(); | 25 | super.dispose(); |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | void startCamera() { | 28 | void startCamera() { |
| 29 | + try { | ||
| 28 | setState(() { | 30 | setState(() { |
| 29 | camStarted = !camStarted; | 31 | camStarted = !camStarted; |
| 30 | controller.start(); | 32 | controller.start(); |
| 31 | }); | 33 | }); |
| 34 | + } on Exception catch (e) { | ||
| 35 | + ScaffoldMessenger.of(context).showSnackBar( | ||
| 36 | + SnackBar( | ||
| 37 | + content: Text('Something went wrong! $e'), | ||
| 38 | + backgroundColor: Colors.red, | ||
| 39 | + ), | ||
| 40 | + ); | ||
| 41 | + } | ||
| 32 | } | 42 | } |
| 33 | 43 | ||
| 34 | void onBarcodeDetect(BarcodeCapture barcodeCapture) { | 44 | void onBarcodeDetect(BarcodeCapture barcodeCapture) { |
| 45 | + final barcode = barcodeCapture.barcodes.last; | ||
| 35 | setState(() { | 46 | setState(() { |
| 36 | - currentBarcodeCapture = barcodeCapture; | ||
| 37 | - overlayText = barcodeCapture.barcodes.last.displayValue!; | 47 | + overlayText = barcodeCapture.barcodes.last.displayValue ?? |
| 48 | + barcode.rawValue ?? | ||
| 49 | + 'Barcode has no displayable value'; | ||
| 38 | }); | 50 | }); |
| 39 | } | 51 | } |
| 40 | 52 | ||
| @@ -52,7 +64,6 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | @@ -52,7 +64,6 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | ||
| 52 | ), | 64 | ), |
| 53 | body: Center( | 65 | body: Center( |
| 54 | child: Column( | 66 | child: Column( |
| 55 | - crossAxisAlignment: CrossAxisAlignment.center, | ||
| 56 | mainAxisAlignment: MainAxisAlignment.center, | 67 | mainAxisAlignment: MainAxisAlignment.center, |
| 57 | children: <Widget>[ | 68 | children: <Widget>[ |
| 58 | Expanded( | 69 | Expanded( |
| @@ -101,14 +112,23 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | @@ -101,14 +112,23 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | ||
| 101 | child: Row( | 112 | child: Row( |
| 102 | mainAxisAlignment: MainAxisAlignment.spaceEvenly, | 113 | mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| 103 | children: [ | 114 | children: [ |
| 104 | - IconButton( | ||
| 105 | - onPressed: () => controller.toggleTorch(), | 115 | + ValueListenableBuilder<bool>( |
| 116 | + valueListenable: torchStateNotifier, | ||
| 117 | + builder: (context, isTorchOn, child) { | ||
| 118 | + return IconButton( | ||
| 119 | + onPressed: () { | ||
| 120 | + controller.toggleTorch(); | ||
| 121 | + torchStateNotifier.value = | ||
| 122 | + !torchStateNotifier.value; | ||
| 123 | + }, | ||
| 106 | icon: Icon( | 124 | icon: Icon( |
| 107 | Icons.flashlight_on, | 125 | Icons.flashlight_on, |
| 108 | - color: controller.torchEnabled | 126 | + color: isTorchOn |
| 109 | ? Colors.yellow | 127 | ? Colors.yellow |
| 110 | : Colors.black, | 128 | : Colors.black, |
| 111 | ), | 129 | ), |
| 130 | + ); | ||
| 131 | + }, | ||
| 112 | ), | 132 | ), |
| 113 | IconButton( | 133 | IconButton( |
| 114 | onPressed: () => controller.switchCamera(), | 134 | onPressed: () => controller.switchCamera(), |
| @@ -124,7 +144,8 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | @@ -124,7 +144,8 @@ class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { | ||
| 124 | ], | 144 | ], |
| 125 | ) | 145 | ) |
| 126 | : const Center( | 146 | : const Center( |
| 127 | - child: Text("Tap on Camera to activate QR Scanner")), | 147 | + child: Text("Tap on Camera to activate QR Scanner"), |
| 148 | + ), | ||
| 128 | ), | 149 | ), |
| 129 | ], | 150 | ], |
| 130 | ), | 151 | ), |
| @@ -147,11 +168,21 @@ class ScannerOverlay extends CustomPainter { | @@ -147,11 +168,21 @@ class ScannerOverlay extends CustomPainter { | ||
| 147 | ScannerOverlay(this.scanWindow); | 168 | ScannerOverlay(this.scanWindow); |
| 148 | 169 | ||
| 149 | final Rect scanWindow; | 170 | final Rect scanWindow; |
| 171 | + final double borderRadius = 12.0; | ||
| 150 | 172 | ||
| 151 | @override | 173 | @override |
| 152 | void paint(Canvas canvas, Size size) { | 174 | void paint(Canvas canvas, Size size) { |
| 153 | final backgroundPath = Path()..addRect(Rect.largest); | 175 | final backgroundPath = Path()..addRect(Rect.largest); |
| 154 | - final cutoutPath = Path()..addRect(scanWindow); | 176 | + final cutoutPath = Path() |
| 177 | + ..addRRect( | ||
| 178 | + RRect.fromRectAndCorners( | ||
| 179 | + scanWindow, | ||
| 180 | + topLeft: Radius.circular(borderRadius), | ||
| 181 | + topRight: Radius.circular(borderRadius), | ||
| 182 | + bottomLeft: Radius.circular(borderRadius), | ||
| 183 | + bottomRight: Radius.circular(borderRadius), | ||
| 184 | + ), | ||
| 185 | + ); | ||
| 155 | 186 | ||
| 156 | final backgroundPaint = Paint() | 187 | final backgroundPaint = Paint() |
| 157 | ..color = Colors.black.withOpacity(0.5) | 188 | ..color = Colors.black.withOpacity(0.5) |
| @@ -174,10 +205,10 @@ class ScannerOverlay extends CustomPainter { | @@ -174,10 +205,10 @@ class ScannerOverlay extends CustomPainter { | ||
| 174 | // Adjust the radius as needed | 205 | // Adjust the radius as needed |
| 175 | final borderRect = RRect.fromRectAndCorners( | 206 | final borderRect = RRect.fromRectAndCorners( |
| 176 | scanWindow, | 207 | scanWindow, |
| 177 | - topLeft: const Radius.circular(12.0), | ||
| 178 | - topRight: const Radius.circular(12.0), | ||
| 179 | - bottomLeft: const Radius.circular(12.0), | ||
| 180 | - bottomRight: const Radius.circular(12.0), | 208 | + topLeft: Radius.circular(borderRadius), |
| 209 | + topRight: Radius.circular(borderRadius), | ||
| 210 | + bottomLeft: Radius.circular(borderRadius), | ||
| 211 | + bottomRight: Radius.circular(borderRadius), | ||
| 181 | ); | 212 | ); |
| 182 | 213 | ||
| 183 | // Draw the white border | 214 | // Draw the white border |
-
Please register or login to post a comment