Scans QR code and Barcode using Google's Mobile Vision API
import 'package:flutter/material.dart';
import 'package:flutter_qr_scanner/qr_scanner_camera.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter QR/Bar Code Reader',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter QR/Bar Code Reader'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _qrInfo = 'Scan a QR/Bar code';
bool _camState = false;
_qrCallback(String code) {
setState(() {
_camState = false;
_qrInfo = code;
});
}
_scanCode() {
setState(() {
_camState = true;
});
}
@override
void initState() {
super.initState();
_scanCode();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _camState
? Center(
child: SizedBox(
height: 1000,
width: 500,
child: QRScannerCamera(
onError: (context, error) => Text(
error.toString(),
style: TextStyle(color: Colors.red),
),
qrCodeCallback: (code) {
_qrCallback(code);
},
),
),
)
: Center(
child: Text(_qrInfo),
),
);
}
}
The QrCodeCallback can do anything you'd like, and wil keep receiving QR/Bar codes until the camera is stopped.
There are also optional parameters to QRScannerCamera.
Takes as parameter the flutter BoxFit
.
Setting this to different values should get the preview image to fit in
different ways, but only BoxFit = cover
has been tested extensively.
A callback that must return a widget if defined. This should build whatever you want to show up while the camera is loading (which can take from milliseconds to seconds depending on the device).
Widget that is shown on top of the QRScannerCamera. If you give it a specific size it may cause weird issues so try not to.
Standard flutter key argument. Can be used to get QRScannerCameraState with a GlobalKey.
A callback that must return a widget if defined. This should build whatever you want to show up when the camera view is 'offscreen'. i.e. when the app is paused. May or may not show up in preview of app.
Callback for if there's an error.
A list of supported formats, all by default. If you use all, you shouldn't define any others.
These are the supported types:
ALL_FORMATS,
AZTEC,
CODE_128,
CODE_39,
CODE_93,
CODABAR,
DATA_MATRIX,
EAN_13,
EAN_8,
ITF,
PDF417,
QR_CODE,
UPC_A,
UPC_E
If you push a new widget on top of a the current page using the navigator, the camera doesn't necessarily know about it.
Any kind of contribution will be appreciated.