Flutter_学习记录_barcode_scan2 实现扫描条形码、二维码

插件地址:https://pub.dev/packages/barcode_scan2

  1. 安装插件
  2. Android 的配置

配置:android\app\src\main\AndroidManifest.xml

dart 复制代码
<uses-permission android:name="android.permission.CAMERA" />
  1. iOS 的配置

配置:info.plist

dart 复制代码
 <key>NSCameraUsageDescription</key> 
 	<string>Camera permission is required for barcode scanning</string> 
  1. 使用
dart 复制代码
var options = ScanOptions(
      // 是否自动打开闪光灯
      autoEnableFlash: true, 
      // 配置文案
      strings: {
         'cancel': '取消', 
         'flash_on': '打开Flash', 
         'flash_off': '关闭Flash'
      }
    );
    var result = await BarcodeScanner.scan(options: options);
    print(result.type); // The result type (barcode, cancelled, failed)
    print(result.rawContent); // The barcode content
    print(result.format); // The barcode format (as enum)
    print(result.formatNote); // If a unknown format was scanned this field contains a note
  1. 完整代码
dart 复制代码
import 'package:flutter/material.dart';
import 'package:barcode_scan2/barcode_scan2.dart';


class ScanDemo extends StatelessWidget {
  const ScanDemo({super.key});

  void _doBarcodeScan() async {
    var options = ScanOptions(
      // 是否自动打开闪光灯
      autoEnableFlash: true, 
      // 配置文案
      strings: {
         'cancel': '取消', 
         'flash_on': '打开Flash', 
         'flash_off': '关闭Flash'
      }
    );
    var result = await BarcodeScanner.scan(options: options);
    print(result.type); // The result type (barcode, cancelled, failed)
    print(result.rawContent); // The barcode content
    print(result.format); // The barcode format (as enum)
    print(result.formatNote); // If a unknown format was scanned this field contains a note
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("扫描二维码"),
      ),
      body: Center(
        child: ElevatedButton(onPressed: _doBarcodeScan, child: Text("点击开始扫描二维码")),
      ),
    );
  }
}
相关推荐
程序员老刘14 小时前
跨平台开发地图 | 2026年6月
flutter·ai编程·客户端
悟空瞎说1 天前
Flutter 架构详解:新手必懂底层原理
flutter
SoaringHeart2 天前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter
恋猫de小郭2 天前
KMP / CMP 鸿蒙版本 Beta 发布,他有什么特别之处?
android·前端·flutter
风华圆舞2 天前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
韩曙亮2 天前
【Flutter】Flutter 组件 ④ ( 组件渲染 的 三棵树理论 | Widget 树 → Element 树 → RenderObject 树 )
flutter·element·widget·renderobject
恋猫de小郭3 天前
Android 17 正式版发布,全新 AI 和各种破坏性更新
android·前端·flutter
kingbal3 天前
Windows:flutter环境搭建
windows·flutter
911hzh3 天前
Flutter MethodChannel 跨端通信框架 zh_native_channel:快速入门、优势分析与 Pigeon 对比
flutter
911hzh3 天前
Flutter 快速搭建新项目:用 Flutter Foundation Kit 一条命令生成带基础架构的 App 模板
flutter