網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Flutter實(shí)現(xiàn)掃二維碼功能的具體代碼,供大家參考,具體內(nèi)容如下
首先在pubspec.yaml中添加:
dependencies: ? qrscan: ^0.3.2 ? event_bus: ^2.0.0
在androd清單文件中加入以下權(quán)限:
import 'package:flutter/material.dart'; import 'package:qrscan/qrscan.dart' as scanner; import 'package:event_bus/event_bus.dart'; void main() { ? runApp(MyApp()); } final eventBus = EventBus(); class MyApp extends StatelessWidget { ? @override ? Widget build(BuildContext context) { ? ? return MaterialApp(home: HomePage()); ? } } class HomePage extends StatelessWidget { ? @override ? Widget build(BuildContext context) { ? ? return Scaffold( ? ? ? appBar: AppBar(title: Text("QRScan")), ? ? ? body: BodyContent(), ? ? ); ? } } class BodyContent extends StatelessWidget { ? const BodyContent({Key? key}) : super(key: key); ? @override ? Widget build(BuildContext context) { ? ? return Container( ? ? ? ? child: Column( ? ? ? children: [HYButton(), HYText()], ? ? )); ? } } class HYButton extends StatelessWidget { ? @override ? Widget build(BuildContext context) { ? ? return ElevatedButton( ? ? ? child: Text("掃一掃"), ? ? ? onPressed: scan, ? ? ); ? } ? Future scan() async { ? ? String? cameraScanResult = await scanner.scan(); //通過掃碼獲取二維碼中的數(shù)據(jù) ? ? final info = QRInfo("${cameraScanResult}"); ? ? eventBus.fire(info); ? ? print(cameraScanResult); //在控制臺(tái)打印 ? } } class HYText extends StatefulWidget { ? @override ? _HYTextState createState() => _HYTextState(); } class _HYTextState extends State{ ? String message = "Hello GalenWu"; ? @override ? void initState() { ? ? super.initState(); ? ? eventBus.on ().listen((data) { ? ? ? setState(() { ? ? ? ? message = "${data.qrcode}"; ? ? ? }); ? ? }); ? } ? @override ? Widget build(BuildContext context) { ? ? return Text( ? ? ? message, ? ? ? style: TextStyle(fontSize: 30), ? ? ); ? } } class QRInfo { ? String? qrcode; ? QRInfo(this.qrcode); }
原文鏈接:https://blog.csdn.net/m0_38013946/article/details/120777406
相關(guān)推薦
- 2022-05-25 Docker?compose配置文件寫法及命令使用示例_docker
- 2022-05-28 Redis如何使用樂觀鎖(CAS)保證數(shù)據(jù)一致性_Redis
- 2022-08-15 matplotlib基礎(chǔ)知識
- 2022-12-02 C語言中qsort函數(shù)用法及用冒泡排序?qū)崿F(xiàn)_C 語言
- 2022-09-15 Nginx如何獲取自定義請求header頭和URL參數(shù)詳解_nginx
- 2023-01-15 圖鄰接矩陣可視化解析_python
- 2022-10-13 windows?server設(shè)置FTP域用戶隔離的方法_FTP服務(wù)器
- 2022-10-25 python庫-dotenv包?及?.env配置文件詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支