-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13e457a
commit 94b679b
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MyApp> { | ||
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: <Widget>[ | ||
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'); | ||
} | ||
} | ||
} |