https://pub.dev/packages/qrscan#-example-tab-

    前言
    前几天突发奇想就想做一个扫描二维码和条形码的demo,查阅了一些资料,发现有qrscan这个插件可以实现,就自己写了一个例子,供大家学习.

    准备工具
    这套课程是采用Android Studio进行开发的。当前在此之前请准备好Flutter开发环境,我这里就不进行讲解了。

    引入插件
    到pubspce.yaml中导包

    1. dependencies:
    2. flutter:
    3. sdk: flutter
    4. # The following adds the Cupertino Icons font to your application.
    5. # Use with the CupertinoIcons class for iOS style icons.
    6. cupertino_icons: ^0.1.2
    7. qrscan: ^0.1.3

    具体实现

    1. import 'package:flutter/material.dart';
    2. import 'package:qrscan/qrscan.dart' as scanner;
    3. void main() => runApp(MyApp());
    4. class MyApp extends StatefulWidget {
    5. MyApp({Key key}) : super(key: key);
    6. _MyAppState createState() => _MyAppState();
    7. }
    8. class _MyAppState extends State<MyApp> {
    9. String barcode = "";
    10. @override
    11. initState() {
    12. super.initState();
    13. }
    14. @override
    15. Widget build(BuildContext context) {
    16. return MaterialApp(
    17. home: Scaffold(
    18. appBar: AppBar(
    19. title: Text('Scan barcodes and qr codes'),
    20. ),
    21. body: Center(
    22. child: Column(
    23. mainAxisAlignment: MainAxisAlignment.end,
    24. children: <Widget>[
    25. Text(barcode),
    26. MaterialButton(
    27. onPressed: scan,
    28. child: Text("Scan"),
    29. color: Colors.blue,
    30. textColor: Colors.white,
    31. ),
    32. ],
    33. ),
    34. ),
    35. ),
    36. );
    37. }
    38. Future scan() async {
    39. try {
    40. String barcode = await scanner.scan();
    41. setState(() => this.barcode = barcode);
    42. } on Exception catch (e) {
    43. if (e == scanner.CameraAccessDenied) {
    44. setState(() {
    45. this.barcode = 'The user did not grant the camera permission!';
    46. });
    47. } else {
    48. setState(() => this.barcode = 'Unknown error: $e');
    49. }
    50. } on FormatException {
    51. setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
    52. } catch (e) {
    53. setState(() => this.barcode = 'Unknown error: $e');
    54. }
    55. }
    56. }

    ————————————————
    版权声明:本文为CSDN博主「追逐蓦然」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_35905501/article/details/89467886