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.0scan_barcode: git: url: https://github.com/FlutterCandies/scan_barcode.gitref: git-refscan_barcode: git: url: https://gitee.com/kikt/scan_barcode.gitref: git-refSee 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 =awaitNavigator.push<List<Barcode>>( context, MaterialPageRoute( builder: (context) =>constScanAndPopPageExample(), ), ); 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'; classScanAndPopPageExampleextendsStatefulWidget{constScanAndPopPageExample({Key? key}) :super(key: key); @overrideState<ScanAndPopPageExample> createState() =>_ScanAndPopPageExampleState()} class_ScanAndPopPageExampleStateextendsState<ScanAndPopPageExample>{var isPop =false; @overrideWidgetbuild(BuildContext context){returnBarcodeWidget( onHandleBarcodeList: (List<Barcode> barcode) async{if (isPop){// Prevent multiple popreturn} 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'; classShowDialogExampleextendsStatelessWidget{constShowDialogExample({Key? key, }) :super(key: key); @overrideWidgetbuild(BuildContext context){returnBarcodeScanPage( title:'Show dialog when scanned', onHandleBarcodeList: (List<Barcode> barCode) async{if (barCode.isEmpty) return; awaitshowBarcodeListDialog( 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{awaitshowDialog( context: context, builder: (context) =>AlertDialog( title:constText('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:constIcon(Icons.copy), onPressed: (){Clipboard.setData( ClipboardData(text: barcode.rawValue ??''), )}, ), ), ], ), actions: [ ElevatedButton( onPressed: (){Navigator.of(context).pop()}, child:constText('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.0dependency_overrides: google_mlkit_barcode_scanning: git: url: https://gitee.com/kikt/Google-Ml-Kit-plugin.gitref: barcode-0.5.0-forkedpath: packages/google_mlkit_barcode_scanningApache License 2.0
