fast_qr_reader_view 0.0.1 fast_qr_reader_view: ^0.0.1 copied to clipboard
A QR reader widget with live preview.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:fast_qr_reader_view/fast_qr_reader_view.dart';
List<CameraDescription> cameras;
Future<Null> main() async {
// Fetch the available cameras before initializing the app.
try {
cameras = await availableCameras();
} on QRReaderException catch (e) {
logError(e.code, e.description);
}
runApp(new MyApp());
}
void logError(String code, String message) =>
print('Error: $code\nError Message: $message');
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
QRReaderController controller;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
// String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
// pick the first available camera
onNewCameraSelected(cameras[0]);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: const Text('Fast QR reader example'),
),
floatingActionButton: FloatingActionButton(
child: new Icon(Icons.check),
onPressed: () {
showInSnackBar("Just proving you can put anything on top of the scanner");
},
),
body: Stack(
children: <Widget>[
new Container(
child: new Padding(
padding: const EdgeInsets.all(0.0),
child: new Center(
child: _cameraPreviewWidget(),
),
),
),
Center(
child: SizedBox(
height: 250.0,
width: 250.0,
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 2.0)),
child: Text("THIS TEXT SHOULD SAY SOMETHING IMPORTANT, BUT IT'S ONLY PROVING THAT YOU CAN PUT STUFF OVER THE CAMERA PREVIEW! AND YES, THE RED SQUARE IS USELESS. TRY RUNNING THIS IN RELEASE MODE, IS MUCH FASTER!")),
),
)
],
),
),
);
}
/// Display the preview from the camera (or a message if the preview is not available).
Widget _cameraPreviewWidget() {
if (controller == null || !controller.value.isInitialized) {
return const Text(
'No camera selected',
style: const TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w900,
),
);
} else {
return new AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: new QRReaderPreview(controller),
);
}
}
void onCodeRead(dynamic value) {
showInSnackBar(value.toString());
// ... do something
// wait 3 seconds then start scanning again.
new Future.delayed(const Duration(seconds: 3), controller.startScanning);
}
void onNewCameraSelected(CameraDescription cameraDescription) async {
if (controller != null) {
await controller.dispose();
}
controller = new QRReaderController(
cameraDescription, ResolutionPreset.high, onCodeRead);
// If the controller is updated then update the UI.
controller.addListener(() {
if (mounted) setState(() {});
if (controller.value.hasError) {
showInSnackBar('Camera error ${controller.value.errorDescription}');
}
});
try {
await controller.initialize();
} on QRReaderException catch (e) {
logError(e.code, e.description);
showInSnackBar('Error: ${e.code}\n${e.description}');
}
if (mounted) {
setState(() {});
controller.startScanning();
}
}
void showInSnackBar(String message) {
_scaffoldKey.currentState
.showSnackBar(new SnackBar(content: new Text(message)));
}
}