Flutter 扫码插件

效果

qr_scan.gif

使用🤓

QR Code Scanner

A Flutter plugin  to scanning. Ready for Android 

permission:

  • <uses-permission android:name="android.permission.CAMERA" />

Installation

Add this to your package's pubspec.yaml file:

dependencies:
 qrscan: ^0.2.10

Usage example

import 'package:qrscan/qrscan.dart' as scanner;

String barcode = await scanner.scan();

Contribute

We would ❤️ to see your contribution!

Example

import 'package:flutter/services.dart';
import 'package:qrscan/qrscan.dart';

void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
  String barcode = "";

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Qrcode Scanner Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Text(barcode),
              MaterialButton(
                onPressed: scan,
                child: Text("Scan"),
                color: Colors.blue,
                textColor: Colors.white,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future scan() async {
    try {
      String barcode = await Qrscan.scan();
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == Qrscan.CameraAccessDenied) {
        setState(() {
          this.barcode = 'The user did not grant the camera permission!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException {
      setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
  }
}

github地址:https://github.com/leyan95/qrcode_scanner

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容