qrscan 0.0.2 qrscan: ^0.0.2 copied to clipboard
A new Flutter project.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qrscan/qrscan.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String barcode = "";
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Qrcode Scanner Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(barcode),
MaterialButton(
onPressed: scan,
child: Text("Scan"),
color: Colors.blue,
textColor: Colors.white,
),
],
),
),
),
);
}
Future scan() async {
try {
String barcode = await Qrscan.scan();
setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
if (e.code == Qrscan.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException {
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}
}