flutter_code_scanner 0.0.1+4 copy "flutter_code_scanner: ^0.0.1+4" to clipboard
flutter_code_scanner: ^0.0.1+4 copied to clipboard

QR code scanner that can be embedded inside flutter. It can be use on Android, IOS, Web.

example/lib/main.dart

// ignore_for_file: implementation_imports

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_code_scanner/ai_barcode_scanner.dart';
import 'package:flutter_code_scanner/src/flutter_code_scanner.dart';
import 'package:flutter_code_scanner/src/flutter_code_scanner_overlay_shape.dart';
import 'package:flutter_code_scanner/src/types/barcode.dart' as bc;

void main() => runApp(const MaterialApp(home: Home()));

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String barcode = 'Tap  to scan';
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
            child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              //Scan using AI
              ElevatedButton(
                child: const Text('Scan Barcode'),
                onPressed: () async {
                  await Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => AiBarcodeScanner(
                        hasAppBar: true,
                        customAppBarWidget: Container(
                          height: 120,
                          color: Colors.blue,
                          child: Center(child: Text("Scanning")),
                        ),
                        validateText: 'https://', // link to be validated
                        validateType: ValidateType.startsWith,
                        canPop: false,
                        onScan: (String value) {
                          debugPrint(value);
                          setState(() {
                            barcode = value;
                          });
                        },
                        onDetect: (p0) {},
                        controller: MobileScannerController(
                          detectionSpeed: DetectionSpeed.noDuplicates,
                        ),
                      ),
                    ),
                  );
                },
              ),
              Text(barcode),
              //Normal scan
              ElevatedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => const QrExample(),
                  ));
                },
                child: const Text('Scan Qr Code'),
              ),
            ],
          ),
        )),
      ),
    );
  }
}

class QrExample extends StatefulWidget {
  const QrExample({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _QrExampleState();
}

class _QrExampleState extends State<QrExample> {
  bc.Barcode? result;
  QrController? controller;
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

  void resumeC() async {
    await controller?.resumeCamera();
  }

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

  String barcode = 'Tap  to scan';
  @override
  Widget build(BuildContext context) {
    resumeC();
    return Scaffold(
        backgroundColor: const Color(0xFF4A4A4A).withOpacity(1),
        body: Center(
          child: _buildQrView(context),
        ));
  }

  Widget _buildQrView(BuildContext context) {
    // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
    var scanArea = (MediaQuery.of(context).size.width < 400 ||
            MediaQuery.of(context).size.height < 400)
        ? 180.0
        : 240.0;
    return QRView(
      key: qrKey,
      onQRViewCreated: _onQRViewCreated,
      overlay: QrScannerOverlayShape(
        borderColor: Colors.white,
        borderLength: 30,
        borderWidth: 5,
        cutOutSize: scanArea,
      ),
      onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
    );
  }

  void _onQRViewCreated(QrController controller) {
    setState(() {
      this.controller = controller;
    });
    controller.scannedDataStream.listen((scanData) {
      setState(() {
        result = scanData;
      });
    });
  }

  void _onPermissionSet(BuildContext context, QrController ctrl, bool p) {
    log('${DateTime.now().toIso8601String()}_onPermissionSet $p');
    if (!p) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('no Permission')),
      );
    }
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }
}
7
likes
110
pub points
48%
popularity

Publisher

unverified uploader

QR code scanner that can be embedded inside flutter. It can be use on Android, IOS, Web.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, flutter_test, flutter_web_plugins, js, mobile_scanner, plugin_platform_interface

More

Packages that depend on flutter_code_scanner