flutter 二维码
一、生成二维码
https://pub.flutter-io.cn/packages/qr_flutter
qr_flutter: ^3.1.0
QrImage( data: "1234567890", version: QrVersions.auto, size: 200.0, ),
全
//生成二维码 import 'package:flutter/material.dart'; import 'package:qr_flutter/qr_flutter.dart'; class RQTest extends StatefulWidget { @override _RQTestState createState() => _RQTestState(); } class _RQTestState extends State<RQTest> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Center( child: QrImage( data: "1234567890", version: QrVersions.auto, size: 200.0, ), ), ); } }
二、扫描二维码
https://pub.flutter-io.cn/packages/barcode_scan
barcode_scan: 3.0.1 #生成二维码
() async { var result = await BarcodeScanner.scan(); print(result.type); // 返回结果类型(barcode, cancelled, failed) print(result.rawContent); //扫描返回的内容 print(result.format); // The barcode format (as enum) print(result.formatNote); // If a unknown format was scanned this field contains a note }();
全
//扫描二维码 import 'package:flutter/material.dart'; import 'package:barcode_scan/barcode_scan.dart'; class RQTest extends StatefulWidget { @override _RQTestState createState() => _RQTestState(); } class _RQTestState extends State<RQTest> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Center( child: RaisedButton( child: Text("扫一扫"), onPressed: (){ () async { var result = await BarcodeScanner.scan(); print(result.type); // 返回结果类型(barcode, cancelled, failed) print(result.rawContent); //扫描返回的内容 print(result.format); // The barcode format (as enum) print(result.formatNote); // If a unknown format was scanned this field contains a note }(); }, ) ), ); } }
1.Android端
AndroidManifest.xml添加
<!--照相机--> <uses-permission android:name="android.permission.CAMERA" />
项目级别build.gradle添加 ---外层
buildscript { ext.kotlin_version = '1.3.61' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
应用级别build.gradle添加 --内层
apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // ... }
build.gradle 这两个文件如果生成时默认勾选的kotlin,貌似不用在单独添加
报错:Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 18 declared in library
说明某个依赖包的manifests
中的minSdkVersion
版本比项目的minSdkVersion
要高
解决
1.更改项目的uses-sdk:minSdkVersion
2.更改依赖包的uses-sdk:minSdkVersion
3.在项目的manifests
标签下添加
xmlns:tools="http://schemas.android.com/tools"
tools:overrideLibrary="依赖包的包名"
<uses-sdk tools:overrideLibrary="xxx.xxx.aaa, xxx.xxx.bbb"/>
2.IOS端
Info.plist添加
<dict> <!-- ... --> <key>NSCameraUsageDescription</key> <string>Camera permission is required for barcode scanning.</string> <!-- ... --> </dict>