QRCodeDartScan .A QR code scanner that works on both iOS and Android using dart decoder.
|
First, add qr_code_dart_scan as a dependency in your pubspec.yaml file.
iOS 10.0 of higher is needed to use the camera plugin. If compiling for any version lower than 10.0 make sure to check the iOS version before using the camera plugin. For example, using the device_info plugin.
Add two rows to the ios/Runner/Info.plist:
Or in text format add the key:
<key>NSCameraUsageDescription</key>
<string>Can I use the camera please?</string>
<key>NSMicrophoneUsageDescription</key>
<string>Can I use the mic please?</string>
Change the minimum Android sdk version to 21 (or higher) in your android/app/build.gradle file.
minSdkVersion 21
It's important to note that the MediaRecorder class is not working properly on emulators, as stated in the documentation: https://developer.android.com/reference/android/media/MediaRecorder. Specifically, when recording a video with sound enabled and trying to play it back, the duration won't be correct and you will only see the first frame.
return Scaffold(
body: QRCodeDartScanView(
scanQRCodeInverted: true, // enable scan invert qr code ( default = false)
onCapture: (Result result) {
// do anything with result
// result.text
// result.rawBytes
// result.resultPoints
// result.format
// result.numBits
// result.resultMetadata
// result.time
},
),
);
Run this command:
With Flutter:
$ flutter pub add qr_code_dart_scan
This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):
dependencies:
qr_code_dart_scan: ^0.0.2
Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:qr_code_dart_scan/qr_code_dart_scan.dart';
import 'package:flutter/material.dart';
import 'package:qr_code_dart_scan/qr_code_dart_scan.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Result? currentResult;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
QRCodeDartScanView(
scanQRCodeInverted: true,
onCapture: (Result result) {
setState(() {
currentResult = result;
});
},
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.all(20),
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Text('${currentResult?.text ?? 'Not found'}'),
),
),
],
),
);
}
}