The plugin is a plugin for scanning barcodes. Only support android and ios. Because the plugin is based on Google mlkit.
See apk in release
dependencies:
scan_barcode: ^0.2.0
scan_barcode:
git:
url: https://github.com/FlutterCandies/scan_barcode.git
ref: git-ref
scan_barcode:
git:
url: https://gitee.com/kikt/scan_barcode.git
ref: git-ref
See the package scan_barcode for more version.
import 'package:scan_barcode/scan_barcode.dart';
The plugin is developed by version 3.7.0 of flutter, so it is recommended to use version 3.1.0 or above. If you want to use the package of Flutter 2.x, click here.
The package has the following important classes:
In most cases, we will use this component to complete code scanning.
This class is used to configure the scanning config. And, hold instance to update config.
The config has the following parts:
- CameraConfig: The camera config.
- BarcodeConfig: The barcode config.
- UIConfig: The UI config.
See the example for more examples.
If you want to scan a code once, you can use the example.
Click to expand code
Future<void> _scanBarcode() async {
final barcodes = await Navigator.push<List<Barcode>>(
context,
MaterialPageRoute(
builder: (context) => const ScanAndPopPageExample(),
),
);
if (barcodes == null) return;
showBarcodeListDialog(context, barcodes); // show barcode list dialog to display barcode.
}
import 'package:flutter/material.dart';
import 'package:scan_barcode/scan_barcode.dart';
class ScanAndPopPageExample extends StatefulWidget {
const ScanAndPopPageExample({Key? key}) : super(key: key);
@override
State<ScanAndPopPageExample> createState() => _ScanAndPopPageExampleState();
}
class _ScanAndPopPageExampleState extends State<ScanAndPopPageExample> {
var isPop = false;
@override
Widget build(BuildContext context) {
return BarcodeWidget(
onHandleBarcodeList: (List<Barcode> barcode) async {
if (isPop) { // Prevent multiple pop
return;
}
if (barcode.isEmpty) return;
isPop = true;
Navigator.of(context).pop(barcode);
},
scanValue: ScanValue(),
);
}
}
Click to expand code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:scan_barcode/scan_barcode.dart';
class ShowDialogExample extends StatelessWidget {
const ShowDialogExample({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return BarcodeScanPage(
title: 'Show dialog when scanned',
onHandleBarcodeList: (List<Barcode> barCode) async {
if (barCode.isEmpty) return;
await showBarcodeListDialog(
context, barCode); // The await is important, if you don't await, multiple dialogs will be shown.
},
);
}
Future<void> showBarcodeListDialog(BuildContext context, List<Barcode> barCode) async {
await showDialog(
context: context,
builder: (context) =>
AlertDialog(
title: const Text('Barcode list'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
for (final barcode in barCode)
ListTile(
title: Text(barcode.rawValue ?? ''),
subtitle:
Text('type: ${barcode.type}, format: ${barcode.format}'),
trailing: IconButton(
icon: const Icon(Icons.copy),
onPressed: () {
Clipboard.setData(
ClipboardData(text: barcode.rawValue ?? ''),
);
},
),
),
],
),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
),
);
}
}
- How to change config
- How to custom barcode rect
- One shot scan
- Show the barcode list dialog when scanned
Because of the upstream API modification, only 0.1.0
can be used on Flutter 2.x.x.
Add the following code to your pubspec.yaml
:
dependencies:
scan_barcode: ^0.1.0
dependency_overrides:
google_mlkit_barcode_scanning:
git:
url: https://gitee.com/kikt/Google-Ml-Kit-plugin.git
ref: barcode-0.5.0-forked
path: packages/google_mlkit_barcode_scanning
Apache License 2.0