Showing
1 changed file
with
164 additions
and
0 deletions
example/lib/scanner_button_widgets.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:image_picker/image_picker.dart'; | ||
| 3 | +import 'package:mobile_scanner/mobile_scanner.dart'; | ||
| 4 | + | ||
| 5 | +class AnalyzeImageFromGalleryButton extends StatelessWidget { | ||
| 6 | + const AnalyzeImageFromGalleryButton({required this.controller, super.key}); | ||
| 7 | + | ||
| 8 | + final MobileScannerController controller; | ||
| 9 | + | ||
| 10 | + @override | ||
| 11 | + Widget build(BuildContext context) { | ||
| 12 | + return IconButton( | ||
| 13 | + color: Colors.white, | ||
| 14 | + icon: const Icon(Icons.image), | ||
| 15 | + iconSize: 32.0, | ||
| 16 | + onPressed: () async { | ||
| 17 | + final ImagePicker picker = ImagePicker(); | ||
| 18 | + | ||
| 19 | + final XFile? image = await picker.pickImage( | ||
| 20 | + source: ImageSource.gallery, | ||
| 21 | + ); | ||
| 22 | + | ||
| 23 | + if (image == null) { | ||
| 24 | + return; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + final BarcodeCapture? barcodes = await controller.analyzeImage( | ||
| 28 | + image.path, | ||
| 29 | + ); | ||
| 30 | + | ||
| 31 | + if (!context.mounted) { | ||
| 32 | + return; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + final SnackBar snackbar = barcodes != null | ||
| 36 | + ? const SnackBar( | ||
| 37 | + content: Text('Barcode found!'), | ||
| 38 | + backgroundColor: Colors.green, | ||
| 39 | + ) | ||
| 40 | + : const SnackBar( | ||
| 41 | + content: Text('No barcode found!'), | ||
| 42 | + backgroundColor: Colors.red, | ||
| 43 | + ); | ||
| 44 | + | ||
| 45 | + ScaffoldMessenger.of(context).showSnackBar(snackbar); | ||
| 46 | + }, | ||
| 47 | + ); | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +class StartStopMobileScannerButton extends StatelessWidget { | ||
| 52 | + const StartStopMobileScannerButton({required this.controller, super.key}); | ||
| 53 | + | ||
| 54 | + final MobileScannerController controller; | ||
| 55 | + | ||
| 56 | + @override | ||
| 57 | + Widget build(BuildContext context) { | ||
| 58 | + return ValueListenableBuilder( | ||
| 59 | + valueListenable: controller, | ||
| 60 | + builder: (context, state, child) { | ||
| 61 | + if (!state.isInitialized || !state.isRunning) { | ||
| 62 | + return IconButton( | ||
| 63 | + color: Colors.white, | ||
| 64 | + icon: const Icon(Icons.play_arrow), | ||
| 65 | + iconSize: 32.0, | ||
| 66 | + onPressed: () async { | ||
| 67 | + await controller.start(); | ||
| 68 | + }, | ||
| 69 | + ); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + return IconButton( | ||
| 73 | + color: Colors.white, | ||
| 74 | + icon: const Icon(Icons.stop), | ||
| 75 | + iconSize: 32.0, | ||
| 76 | + onPressed: () async { | ||
| 77 | + await controller.stop(); | ||
| 78 | + }, | ||
| 79 | + ); | ||
| 80 | + }, | ||
| 81 | + ); | ||
| 82 | + } | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +class SwitchCameraButton extends StatelessWidget { | ||
| 86 | + const SwitchCameraButton({required this.controller, super.key}); | ||
| 87 | + | ||
| 88 | + final MobileScannerController controller; | ||
| 89 | + | ||
| 90 | + @override | ||
| 91 | + Widget build(BuildContext context) { | ||
| 92 | + return ValueListenableBuilder( | ||
| 93 | + valueListenable: controller, | ||
| 94 | + builder: (context, state, child) { | ||
| 95 | + if (!state.isInitialized || !state.isRunning) { | ||
| 96 | + return const SizedBox.shrink(); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + final Widget icon; | ||
| 100 | + | ||
| 101 | + switch (state.cameraDirection) { | ||
| 102 | + case CameraFacing.front: | ||
| 103 | + icon = const Icon(Icons.camera_front); | ||
| 104 | + break; | ||
| 105 | + case CameraFacing.back: | ||
| 106 | + icon = const Icon(Icons.camera_rear); | ||
| 107 | + break; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + return IconButton( | ||
| 111 | + iconSize: 32.0, | ||
| 112 | + icon: icon, | ||
| 113 | + onPressed: () async { | ||
| 114 | + await controller.switchCamera(); | ||
| 115 | + }, | ||
| 116 | + ); | ||
| 117 | + }, | ||
| 118 | + ); | ||
| 119 | + } | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +class ToggleFlashlightButton extends StatelessWidget { | ||
| 123 | + const ToggleFlashlightButton({required this.controller, super.key}); | ||
| 124 | + | ||
| 125 | + final MobileScannerController controller; | ||
| 126 | + | ||
| 127 | + @override | ||
| 128 | + Widget build(BuildContext context) { | ||
| 129 | + return ValueListenableBuilder( | ||
| 130 | + valueListenable: controller, | ||
| 131 | + builder: (context, state, child) { | ||
| 132 | + if (!state.isInitialized || !state.isRunning) { | ||
| 133 | + return const SizedBox.shrink(); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + switch (state.torchState) { | ||
| 137 | + case TorchState.off: | ||
| 138 | + return IconButton( | ||
| 139 | + color: Colors.white, | ||
| 140 | + iconSize: 32.0, | ||
| 141 | + icon: const Icon(Icons.flash_off), | ||
| 142 | + onPressed: () async { | ||
| 143 | + await controller.toggleTorch(); | ||
| 144 | + }, | ||
| 145 | + ); | ||
| 146 | + case TorchState.on: | ||
| 147 | + return IconButton( | ||
| 148 | + color: Colors.white, | ||
| 149 | + iconSize: 32.0, | ||
| 150 | + icon: const Icon(Icons.flash_on), | ||
| 151 | + onPressed: () async { | ||
| 152 | + await controller.toggleTorch(); | ||
| 153 | + }, | ||
| 154 | + ); | ||
| 155 | + case TorchState.unavailable: | ||
| 156 | + return const Icon( | ||
| 157 | + Icons.no_flash, | ||
| 158 | + color: Colors.grey, | ||
| 159 | + ); | ||
| 160 | + } | ||
| 161 | + }, | ||
| 162 | + ); | ||
| 163 | + } | ||
| 164 | +} |
-
Please register or login to post a comment