flutter_smart_scan 1.0.4 copy "flutter_smart_scan: ^1.0.4" to clipboard
flutter_smart_scan: ^1.0.4 copied to clipboard

A plugin for scanning barcodes on Android and iOS. Supports barcodes, QR codes, etc.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_smart_scan/flutter_smart_scan.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  TextEditingController serialNumberController = TextEditingController();
  String serialNumber = '';

  @override
  void initState() {
    super.initState();
  }

  Future<void> startBarcodeScanStream() async {
    var stream = FlutterBarcodeScanner.getBarcodeStreamReceiver(
        '#ff6666', 'Cancel', true, ScanMode.BARCODE)!;
    StreamSubscription? subscription;
    subscription = stream.listen((barcode) {
      // Exit the scan when the barcode matches a certain condition
      // In this case, the barcode starts with "HH", so it matches the format of
      // a serial number.
      if (barcode.substring(0, 2) == 'HH') {
        // Update the serial number variable and the corresponding text field.
        setState(() {
          serialNumber = barcode;
          serialNumberController.text = barcode;
        });
        // Cancel the StreamSubscription to exit the camera scanning view.
        subscription!.cancel();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('Serial Number Scan'),
          ),
          body: Center(
              child: Column(children: <Widget>[
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: serialNumberController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Serial Number',
                  ),
                  onChanged: (text) {
                    setState(() {
                      serialNumber = text;
                    });
                  },
                )),
            Container(
                alignment: Alignment.center,
                child: Flex(
                    direction: Axis.vertical,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      ElevatedButton(
                          onPressed: () => startBarcodeScanStream(),
                          child: Text('Scan for Barcode')),
                    ]))
          ]))),
    );
  }
}
0
likes
160
pub points
11%
popularity

Publisher

unverified uploader

A plugin for scanning barcodes on Android and iOS. Supports barcodes, QR codes, etc.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_plugin_android_lifecycle

More

Packages that depend on flutter_smart_scan