dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
date_format: ^1.0.6
flutter_cupertino_date_picker: ^1.0.26+2
flutter_swiper: ^1.1.6
fluttertoast: ^7.1.6
http: ^0.12.2
dio: ^3.0.10
flutter_html: ^1.1.0
flutter_inappwebview: ^4.0.0+4
device_info: ^1.0.0
amap_location: ^0.2.0
image_picker: ^0.6.7+21
video_player: ^1.0.1
chewie: ^0.12.2
connectivity: ^2.0.2
shared_preferences: ^0.5.12+4
# 二维码扫描插件
barcode_scan_fix: ^1.0.2
在pubspec.yaml中配置保存后,在VS Code环境中会自动下载依赖包。
如果无法正常下载,执行 flutter pub get 。
1. 安卓配置。
第一步:如下图所示,按第一个红框里所示的路径找到该文件,添加第二个红框和第三个红框里的代码;
要添加的代码如下:
<!-- 照相机权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 二维码扫描 -->
<activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/>
第二步:检查如下图第一个红框里所示的路径文件,是否有第二和第三个红框里的代码;
要添加的代码如下:
buildscript {
ext.kotlin_version = '1.3.50'
...
dependencies {
...
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
第三步:检查如下图第一个红框里所示的路径文件,是否有第二和第三个红框里的代码;
要添加的代码如下:
apply plugin: 'kotlin-android'
...
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
...
}
2. IOS配置。
如下图所示,按第一个红框里所示的路径找到该文件,添加第二个红框里面的代码。
要添加的代码如下:
<key>NSCameraUsageDescription</key>
<string>Camera permission is required for barcode scanning.</string>
在需要用到的该插件的文件中引入插件包。
import 'package:barcode_scan_fix/barcode_scan.dart';
String barcode;
Future _scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() {
this.barcode = barcode;
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
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 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
// 引入插件
import 'package:barcode_scan_fix/barcode_scan.dart';
class ScanPage extends StatefulWidget {
ScanPage({Key key}) : super(key: key);
_ScanPageState createState() => _ScanPageState();
}
class _ScanPageState extends State<ScanPage> {
// 定义变量存储扫描结果
String barcode;
// 扫描方法
Future _scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() {
this.barcode = barcode;
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
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.photo_camera),
// 点击扫描
onPressed: _scan,
),
appBar: AppBar(
title: Text("扫码"),
),
body: Text("${barcode}")
);
}
}
在项目编译时,可能会出现如下所示的错误。
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。
解决方案如下:
参考: