日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Flutter - 生成二维码与识别二维码

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Flutter - 生成二维码与识别二维码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#生成二維碼

##首先需要在pubspec.yaml:中添加

qr_flutter: ^1.1.3

其次,引入代碼:

import 'package:qr_flutter/qr_flutter.dart';

核心代碼如下:

child: QrImage(data: "這里是需要生成二維碼的數據",size: 100.0,onError: (ex) {print("[QR] ERROR - $ex");}, 復制代碼

整體代碼與效果如下:

#識別二維碼

這里也用第三方的包在pubspec.yaml中添加庫鏈接;

barcode_scan: ^0.0.4

識別的主要代碼如下:

Future scan() async {try {String barcode = await BarcodeScanner.scan();setState(() {return this.barcode = barcode;});} on PlatformException catch (e) {if (e.code == BarcodeScanner.CameraAccessDenied) {setState(() {return this.barcode = 'The user did not grant the camera permission!';});} else {setState(() {return 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');}} 復制代碼

整體代碼如下:

import 'package:flutter/material.dart'; import 'package:barcode_scan/barcode_scan.dart'; import 'dart:async'; import 'package:flutter/services.dart';class scanqr extends StatelessWidget {@overrideWidget build(BuildContext context) {// TODO: implement buildreturn MaterialApp(home: sacnBody(),);} }class sacnBody extends StatefulWidget { @overrideState<StatefulWidget> createState() {// TODO: implement createStatereturn _MyScanState();} }class _MyScanState extends State<sacnBody> {String barcode = "";@overridevoid initState() {// TODO: implement initStatesuper.initState();}@overrideWidget build(BuildContext context) {// TODO: implement buildreturn Scaffold(appBar: new AppBar(title: new Text('QR Code'),),body: new Center(child: new Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.stretch,children: <Widget>[Padding(padding: EdgeInsets.symmetric(horizontal: 15.0,vertical: 8.0),child: RaisedButton(color: Colors.orange,textColor: Colors.white,splashColor: Colors.blueGrey,onPressed: scan,child: const Text('START CAMERA SCAN')),),Padding (padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),child: Text(barcode, textAlign: TextAlign.center,),)],),),);}Future scan() async {try {String barcode = await BarcodeScanner.scan();setState(() {return this.barcode = barcode;});} on PlatformException catch (e) {if (e.code == BarcodeScanner.CameraAccessDenied) {setState(() {return this.barcode = 'The user did not grant the camera permission!';});} else {setState(() {return 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');}}} 復制代碼

安卓和iOS工程設置如下:

Android For Android, you must do the following before you can use the plugin:

Add the camera permission to your AndroidManifest.xml<uses-permission android:name="android.permission.CAMERA" />Add the Barcode activity to your AndroidManifest.xml<activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/> 復制代碼

iOS To use on iOS, you must add the the camera usage description to your Info.plist

<key>NSCameraUsageDescription</key> <string>Camera permission is required for barcode scanning.</string> 復制代碼

總結

以上是生活随笔為你收集整理的Flutter - 生成二维码与识别二维码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。