scan_barcode 0.3.0 copy "scan_barcode: ^0.3.0" to clipboard
scan_barcode: ^0.3.0 copied to clipboard

A flutter plugin to scan barcode. base the Google mlkit. Support android and iOS.

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
copied to clipboard
scan_barcode:
  git:
    url: https://github.com/FlutterCandies/scan_barcode.git
    ref: git-ref
copied to clipboard
scan_barcode:
  git:
    url: https://gitee.com/kikt/scan_barcode.git
    ref: git-ref
copied to clipboard

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 = 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.
}
copied to clipboard
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(),
    );
  }
}

copied to clipboard

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';

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'),
              ),
            ],
          ),
    );
  }
}
copied to clipboard

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.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
copied to clipboard

LICENSE #

Apache License 2.0

4
likes
150
points
81
downloads

Publisher

verified publisherfluttercandies.com

Weekly Downloads

2024.08.26 - 2025.03.10

A flutter plugin to scan barcode. base the Google mlkit. Support android and iOS.

Repository (GitHub)

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

camera, flutter, google_mlkit_barcode_scanning, google_mlkit_commons

More

Packages that depend on scan_barcode