Skip to content

fluttercandies/scan_barcode

Repository files navigation

scan_barcode

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

Getting Started

dependencies: scan_barcode: ^0.2.0
scan_barcode: git: url: https://github.com/FlutterCandies/scan_barcode.gitref: git-ref
scan_barcode: git: url: https://gitee.com/kikt/scan_barcode.gitref: 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.

Contents

The package has the following important classes:

BarcodeWidget

In most cases, we will use this component to complete code scanning.

BarcodeConfig

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.

Example

See the example for more examples.

One shot scan code

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(), )} } 

Scan and show dialog in current page

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'), ), ], ), )} }

Examples

dependencies

Common

Using in flutter 2.x

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_scanning

LICENSE

Apache License 2.0

About

Barcode/QRCode scan, base of google mikit.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages