1:barcode_scan: ^3.0.1
优点:集成快,不需要过多的配置即可兼容。 缺点:暂时无FLutter2.0之后的空安全版本。且mac的M1芯片使用该插件无法打包。
import 'package:flutter/services.dart';
import 'package:barcode_scan/platform_wrapper.dart';
class Barcode {
String _qrCode = '';
Future scan() async {
try {
var result = await BarcodeScanner.scan();
String scanData = result.rawContent;
_qrCode = scanData;
print('扫码结果: ' + _qrCode.toString());
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.cameraAccessDenied) {
print('未授予APP相机权限');
} else {
print('扫码错误: $e');
}
} on FormatException {
print('进入扫码页面后未扫码就返回');
} catch (e) {
print('扫码错误: $e');
}
return _qrCode;
}
String get qrCode => _qrCode;
set qrCode(String value) {
_qrCode = value;
}
}
使用
Barcode barcode = new Barcode();
barcode.scan();
2:scan: ^0.0.7(flutter2.0前的最后一个版本)
优点:版本更新快,Flutter2.0有空安全版本。可以自定义扫码界面,可扩展性强。 缺点:可能低版本的在android的个别机型会出现一些问题。
仅供参考
import 'package:flutter/material.dart';
import '../styles/app_style.dart';
import 'package:scan/scan.dart';
class BarcodeScan extends StatefulWidget {
final Function scanCode;
const BarcodeScan({Key key, this.scanCode}) : super(key: key);
@override
_BarcodeScanState createState() => _BarcodeScanState();
}
class _BarcodeScanState extends State<BarcodeScan> {
ScanController controller = ScanController();
String qrcode = '';
bool flashFlag = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('扫描二维码'),
backgroundColor: KColor.primaryColor,
actions: [
Container(
margin: EdgeInsets.only(right: 5.px),
width: 27.px,
child: InkWell(
child: Image.asset(flashFlag?'images/icon-flash-on.png':'images/icon-flash-off.png',color: Colors.white,),
onTap: (){
setState(() {
controller.toggleTorchMode();
flashFlag = !flashFlag;
});
},
),
)
],
leading:Padding(
padding: EdgeInsets.only(left: 15.px,top: 20.px),
child: InkWell(child: Text('退出',style: TextStyle(color: Colors.white)),onTap: (){Navigator.pop(context);}),
),
),
body: Column(
children: [
Container(
width: 375.px,
height: 712.px,
child: ScanView(
controller: controller,
scanAreaScale: .7,
scanLineColor: KColor.primaryColor,
onCapture: (data) {
setState(() {
qrcode = data;
Navigator.pop(context);
widget.scanCode(qrcode);
});
},
),
),
],
),
),
);
}
}
闪光灯图片: icon-flash-off.png icon-flash-on.png
若不喜欢上面图片的话可以上图库自行搜索下载 https://www.iconfont.cn/home/index
关于选择本地图片扫描之类的可以参考官方文档进行查阅 https://pub.dev/packages/scan/versions/0.0.7
|