扫码 二维码 条形码

一、Flutter 扫描二维码条形码插件
https://pub.dev/packages/barcode_scan
1、安装
dependencies:
barcode_scan: ^1.0.0
2、配置权限
Add the camera permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
Add the BarcodeScanner activity to your AndroidManifest.xml. Do NOT
modify the name.
<activity
android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/>
3、检查、配置 build.gradle湖北众猿腾网络科技有限公司
3.1 编 辑 你 的 android 目 录 下 面 的 build.gradle ( Edit your project-level
build.gradle file to look like this)
注意:官方文档配置的 kotlin_version 的版本是 1.2.31,但是实际发现 1.2.31会报错。所以本项目使用 1.3.0。
buildscript {
ext.kotlin_version = '1.3.0'
...
dependencies {
...
classpath
"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
...
3.2 编 辑 你 的 android/app 目 录 下 面 的 build.gradle ( Edit your app-levelbuild.gradle file to look like this)
apply plugin: 'kotlin-android'
...
dependencies {
implementation
"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
...
}
4、使用
import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';
class ScanPage extends StatefulWidget {
ScanPage({Key key}) : super(key: key);
_ScanPageState createState() => _ScanPageState();
}
class _ScanPageState extends State<ScanPage> {湖北众猿腾网络科技有限公司
var barcode;
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');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.camera_roll),
onPressed: _scan,
),
appBar: AppBar(
title: Text("扫码"),
),
body:Text("扫码--${barcode}"),
);
}
}
 
二、Flutter 使用barcode_scan提示如下错误解决方案
Android dependency ‘androidx.core:core’ has different version for the compile (1.0.0)
and runtime (1.0.2) classpath. You should manually set the same version via
DependencyResolution
http://bbs.itying.com/topic/5d0468735923fe0334c35ea2

猜你喜欢

转载自www.cnblogs.com/zhaofeis/p/12375019.html