Showing
3 changed files
with
135 additions
and
76 deletions
| 1 | +import 'package:flutter/foundation.dart'; | ||
| 2 | +import 'package:flutter/material.dart'; | ||
| 3 | +import 'package:image_picker/image_picker.dart'; | ||
| 4 | +import 'package:mobile_scanner/mobile_scanner.dart'; | ||
| 5 | + | ||
| 6 | +class BarcodeScannerAnalyzeImage extends StatefulWidget { | ||
| 7 | + const BarcodeScannerAnalyzeImage({super.key}); | ||
| 8 | + | ||
| 9 | + @override | ||
| 10 | + State<BarcodeScannerAnalyzeImage> createState() => | ||
| 11 | + _BarcodeScannerAnalyzeImageState(); | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +class _BarcodeScannerAnalyzeImageState | ||
| 15 | + extends State<BarcodeScannerAnalyzeImage> { | ||
| 16 | + final MobileScannerController _controller = MobileScannerController(); | ||
| 17 | + | ||
| 18 | + BarcodeCapture? _barcodeCapture; | ||
| 19 | + | ||
| 20 | + Future<void> _analyzeImageFromFile() async { | ||
| 21 | + try { | ||
| 22 | + final XFile? file = | ||
| 23 | + await ImagePicker().pickImage(source: ImageSource.gallery); | ||
| 24 | + | ||
| 25 | + if (!mounted || file == null) { | ||
| 26 | + return; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + final BarcodeCapture? barcodeCapture = | ||
| 30 | + await _controller.analyzeImage(file.path); | ||
| 31 | + | ||
| 32 | + if (mounted) { | ||
| 33 | + setState(() { | ||
| 34 | + _barcodeCapture = barcodeCapture; | ||
| 35 | + }); | ||
| 36 | + } | ||
| 37 | + } catch (_) {} | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + @override | ||
| 41 | + Widget build(BuildContext context) { | ||
| 42 | + Widget label = const Text('Pick a file to detect barcode'); | ||
| 43 | + | ||
| 44 | + if (_barcodeCapture != null) { | ||
| 45 | + label = Text( | ||
| 46 | + _barcodeCapture?.barcodes.firstOrNull?.displayValue ?? | ||
| 47 | + 'No barcode detected', | ||
| 48 | + ); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + return Scaffold( | ||
| 52 | + appBar: AppBar(title: const Text('Analyze image from file')), | ||
| 53 | + body: Column( | ||
| 54 | + children: [ | ||
| 55 | + Expanded( | ||
| 56 | + child: Center( | ||
| 57 | + child: ElevatedButton( | ||
| 58 | + onPressed: kIsWeb ? null : _analyzeImageFromFile, | ||
| 59 | + child: kIsWeb | ||
| 60 | + ? const Text('Analyze image is not supported on web') | ||
| 61 | + : const Text('Choose file'), | ||
| 62 | + ), | ||
| 63 | + ), | ||
| 64 | + ), | ||
| 65 | + Expanded(child: Center(child: label)), | ||
| 66 | + ], | ||
| 67 | + ), | ||
| 68 | + ); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @override | ||
| 72 | + void dispose() { | ||
| 73 | + _controller.dispose(); | ||
| 74 | + super.dispose(); | ||
| 75 | + } | ||
| 76 | +} |
| 1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
| 2 | +import 'package:mobile_scanner_example/barcode_scanner_analyze_image.dart'; | ||
| 2 | import 'package:mobile_scanner_example/barcode_scanner_controller.dart'; | 3 | import 'package:mobile_scanner_example/barcode_scanner_controller.dart'; |
| 3 | import 'package:mobile_scanner_example/barcode_scanner_listview.dart'; | 4 | import 'package:mobile_scanner_example/barcode_scanner_listview.dart'; |
| 4 | import 'package:mobile_scanner_example/barcode_scanner_pageview.dart'; | 5 | import 'package:mobile_scanner_example/barcode_scanner_pageview.dart'; |
| @@ -20,95 +21,75 @@ void main() { | @@ -20,95 +21,75 @@ void main() { | ||
| 20 | class MyHome extends StatelessWidget { | 21 | class MyHome extends StatelessWidget { |
| 21 | const MyHome({super.key}); | 22 | const MyHome({super.key}); |
| 22 | 23 | ||
| 24 | + Widget _buildItem(BuildContext context, String label, Widget page) { | ||
| 25 | + return Padding( | ||
| 26 | + padding: const EdgeInsets.all(8.0), | ||
| 27 | + child: Center( | ||
| 28 | + child: ElevatedButton( | ||
| 29 | + onPressed: () { | ||
| 30 | + Navigator.of(context).push( | ||
| 31 | + MaterialPageRoute( | ||
| 32 | + builder: (context) => page, | ||
| 33 | + ), | ||
| 34 | + ); | ||
| 35 | + }, | ||
| 36 | + child: Text(label), | ||
| 37 | + ), | ||
| 38 | + ), | ||
| 39 | + ); | ||
| 40 | + } | ||
| 41 | + | ||
| 23 | @override | 42 | @override |
| 24 | Widget build(BuildContext context) { | 43 | Widget build(BuildContext context) { |
| 25 | return Scaffold( | 44 | return Scaffold( |
| 26 | appBar: AppBar(title: const Text('Mobile Scanner Example')), | 45 | appBar: AppBar(title: const Text('Mobile Scanner Example')), |
| 27 | body: Center( | 46 | body: Center( |
| 28 | - child: Column( | ||
| 29 | - mainAxisAlignment: MainAxisAlignment.spaceAround, | 47 | + child: ListView( |
| 30 | children: [ | 48 | children: [ |
| 31 | - ElevatedButton( | ||
| 32 | - onPressed: () { | ||
| 33 | - Navigator.of(context).push( | ||
| 34 | - MaterialPageRoute( | ||
| 35 | - builder: (context) => const BarcodeScannerSimple(), | ||
| 36 | - ), | ||
| 37 | - ); | ||
| 38 | - }, | ||
| 39 | - child: const Text('MobileScanner Simple'), | 49 | + _buildItem( |
| 50 | + context, | ||
| 51 | + 'MobileScanner Simple', | ||
| 52 | + const BarcodeScannerSimple(), | ||
| 40 | ), | 53 | ), |
| 41 | - ElevatedButton( | ||
| 42 | - onPressed: () { | ||
| 43 | - Navigator.of(context).push( | ||
| 44 | - MaterialPageRoute( | ||
| 45 | - builder: (context) => const BarcodeScannerListView(), | ||
| 46 | - ), | ||
| 47 | - ); | ||
| 48 | - }, | ||
| 49 | - child: const Text('MobileScanner with ListView'), | 54 | + _buildItem( |
| 55 | + context, | ||
| 56 | + 'MobileScanner with ListView', | ||
| 57 | + const BarcodeScannerListView(), | ||
| 50 | ), | 58 | ), |
| 51 | - ElevatedButton( | ||
| 52 | - onPressed: () { | ||
| 53 | - Navigator.of(context).push( | ||
| 54 | - MaterialPageRoute( | ||
| 55 | - builder: (context) => const BarcodeScannerWithController(), | ||
| 56 | - ), | ||
| 57 | - ); | ||
| 58 | - }, | ||
| 59 | - child: const Text('MobileScanner with Controller'), | 59 | + _buildItem( |
| 60 | + context, | ||
| 61 | + 'MobileScanner with Controller', | ||
| 62 | + const BarcodeScannerWithController(), | ||
| 60 | ), | 63 | ), |
| 61 | - ElevatedButton( | ||
| 62 | - onPressed: () { | ||
| 63 | - Navigator.of(context).push( | ||
| 64 | - MaterialPageRoute( | ||
| 65 | - builder: (context) => const BarcodeScannerWithScanWindow(), | ||
| 66 | - ), | ||
| 67 | - ); | ||
| 68 | - }, | ||
| 69 | - child: const Text('MobileScanner with ScanWindow'), | 64 | + _buildItem( |
| 65 | + context, | ||
| 66 | + 'MobileScanner with ScanWindow', | ||
| 67 | + const BarcodeScannerWithScanWindow(), | ||
| 70 | ), | 68 | ), |
| 71 | - ElevatedButton( | ||
| 72 | - onPressed: () { | ||
| 73 | - Navigator.of(context).push( | ||
| 74 | - MaterialPageRoute( | ||
| 75 | - builder: (context) => const BarcodeScannerReturningImage(), | ||
| 76 | - ), | ||
| 77 | - ); | ||
| 78 | - }, | ||
| 79 | - child: const Text( | ||
| 80 | - 'MobileScanner with Controller (returning image)', | ||
| 81 | - ), | 69 | + _buildItem( |
| 70 | + context, | ||
| 71 | + 'MobileScanner with Controller (return image)', | ||
| 72 | + const BarcodeScannerReturningImage(), | ||
| 73 | + ), | ||
| 74 | + _buildItem( | ||
| 75 | + context, | ||
| 76 | + 'MobileScanner with zoom slider', | ||
| 77 | + const BarcodeScannerWithZoom(), | ||
| 82 | ), | 78 | ), |
| 83 | - ElevatedButton( | ||
| 84 | - onPressed: () { | ||
| 85 | - Navigator.of(context).push( | ||
| 86 | - MaterialPageRoute( | ||
| 87 | - builder: (context) => const BarcodeScannerWithZoom(), | ||
| 88 | - ), | ||
| 89 | - ); | ||
| 90 | - }, | ||
| 91 | - child: const Text('MobileScanner with zoom slider'), | 79 | + _buildItem( |
| 80 | + context, | ||
| 81 | + 'MobileScanner with PageView', | ||
| 82 | + const BarcodeScannerPageView(), | ||
| 92 | ), | 83 | ), |
| 93 | - ElevatedButton( | ||
| 94 | - onPressed: () { | ||
| 95 | - Navigator.of(context).push( | ||
| 96 | - MaterialPageRoute( | ||
| 97 | - builder: (context) => const BarcodeScannerPageView(), | ||
| 98 | - ), | ||
| 99 | - ); | ||
| 100 | - }, | ||
| 101 | - child: const Text('MobileScanner pageView'), | 84 | + _buildItem( |
| 85 | + context, | ||
| 86 | + 'MobileScanner with Overlay', | ||
| 87 | + const BarcodeScannerWithOverlay(), | ||
| 102 | ), | 88 | ), |
| 103 | - ElevatedButton( | ||
| 104 | - onPressed: () { | ||
| 105 | - Navigator.of(context).push( | ||
| 106 | - MaterialPageRoute( | ||
| 107 | - builder: (context) => BarcodeScannerWithOverlay(), | ||
| 108 | - ), | ||
| 109 | - ); | ||
| 110 | - }, | ||
| 111 | - child: const Text('MobileScanner with Overlay'), | 89 | + _buildItem( |
| 90 | + context, | ||
| 91 | + 'Analyze image from file', | ||
| 92 | + const BarcodeScannerAnalyzeImage(), | ||
| 112 | ), | 93 | ), |
| 113 | ], | 94 | ], |
| 114 | ), | 95 | ), |
| @@ -5,6 +5,8 @@ import 'package:mobile_scanner_example/scanner_button_widgets.dart'; | @@ -5,6 +5,8 @@ import 'package:mobile_scanner_example/scanner_button_widgets.dart'; | ||
| 5 | import 'package:mobile_scanner_example/scanner_error_widget.dart'; | 5 | import 'package:mobile_scanner_example/scanner_error_widget.dart'; |
| 6 | 6 | ||
| 7 | class BarcodeScannerWithOverlay extends StatefulWidget { | 7 | class BarcodeScannerWithOverlay extends StatefulWidget { |
| 8 | + const BarcodeScannerWithOverlay({super.key}); | ||
| 9 | + | ||
| 8 | @override | 10 | @override |
| 9 | _BarcodeScannerWithOverlayState createState() => | 11 | _BarcodeScannerWithOverlayState createState() => |
| 10 | _BarcodeScannerWithOverlayState(); | 12 | _BarcodeScannerWithOverlayState(); |
-
Please register or login to post a comment