diff --git a/barcode.dart b/barcode.dart new file mode 100644 index 0000000..fd9a946 --- /dev/null +++ b/barcode.dart @@ -0,0 +1,95 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:barcode_scan/barcode_scan.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:image_picker/image_picker.dart'; + +void main() { + runApp(new MyApp()); +} + +class MyApp extends StatefulWidget { + @override + _MyAppState createState() => new _MyAppState(); +} + +class _MyAppState extends State { + String barcode = ""; + + @override + initState() { + super.initState(); + } + + File galleryFile; + + imageSelectorGallery() async { + galleryFile = await ImagePicker.pickImage( + source: ImageSource.gallery, +// maxHeight: 50.0, +// maxWidth: 50.0, + ); + print("You selected gallery image : " + galleryFile.path); + setState(() {}); + } + + @override + Widget build(BuildContext context) { + return new MaterialApp( + home: new Scaffold( + appBar: new AppBar( + title: new Text('Scan Barcode'), + ), + body: new Center( + child: new Column( + children: [ + new Container( + child: new RaisedButton( + onPressed: barcodeScanning, child: new Text("Capture image")), + padding: const EdgeInsets.all(8.0), + ), + new Padding( + padding: const EdgeInsets.all(8.0), + ), + new Text("Barcode Number after Scan : " + barcode), + // displayImage(), + ], + ), + )), + ); + } + + Widget displayImage() { + return new SizedBox( + height: 300.0, + width: 400.0, + child: galleryFile == null + ? new Text('Sorry nothing to display') + : new Image.file(galleryFile), + ); + } + +// Method for scanning barcode.... + Future barcodeScanning() async { +//imageSelectorGallery(); + + try { + String barcode = await BarcodeScanner.scan(); + setState(() => this.barcode = barcode); + } on PlatformException catch (e) { + if (e.code == BarcodeScanner.CameraAccessDenied) { + setState(() { + this.barcode = 'No camera permission!'; + }); + } else { + setState(() => this.barcode = 'Unknown error: $e'); + } + } on FormatException { + setState(() => this.barcode = + 'Nothing captured.'); + } catch (e) { + setState(() => this.barcode = 'Unknown error: $e'); + } + } +}