flutter 扫码
实现 flutter 扫码功能可以借助第三方 flutter_easy_permission 实现,该组件基于华为开发的 flutter-scankit 组件的二次封装, 华为针对 flutter-scankit 组件的扫一扫功能进行识别优化。可快速识别复杂环境下的条码识别,组件在 flutter 3.0 中验证可以使用
安装插件
flutter_easy_permission: ^1.1.2
flutter_scankit: ^1.2.4
安卓配置权限
在 android / app / src / main / AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
在文件中使用
import 'package:flutter/services.dart';
import 'package:flutter_easy_permission/constants.dart';
import 'package:flutter_easy_permission/easy_permissions.dart';
import 'package:flutter_scankit/flutter_scankit.dart';
class HomeController extends GetxController {
/* 实例化扫码变量 */
late FlutterScankit scanKit;
/**扫一扫相关配置 */
/* 相机和外部存储权限(获取安卓权限) */
final permissions = const [
Permissions.READ_EXTERNAL_STORAGE,
Permissions.CAMERA
];
/* 相机和外部存储权限(获取iod权限) */
final permissionGroup = const [
PermissionGroup.Camera,
PermissionGroup.Photos
];
@override
void onInit() {
super.onInit();
/*监听扫描结果*/
scanKit = FlutterScankit();
scanKit.addResultListen((val) {
print(val);
});
}
/*扫一扫*/
searchCodeFn() async{
if (!await FlutterEasyPermission.has(perms: permissions,permsGroup:permissionGroup)) {
FlutterEasyPermission.request(perms:permissions,permsGroup:permissionGroup);
} else {
try {
await scanKit.startScan(scanTypes: [ScanTypes.ALL]);
} on PlatformException {
print("扫一扫出错")
}
}
}
生成二维码
安装插件
qr_flutter: ^4.1.0
使用
import 'package:qr_flutter/qr_flutter.dart';
QrImageView(
data: '123456789', /*生成码的内容*/
version: QrVersions.auto,
padding:EdgeInsets.zero,/*内边距*/
size: 400.0, /*二维码尺寸*/
gapless: false,
eyeStyle:const QrEyeStyle(eyeShape: QrEyeShape.square, color: MyTheme.themeColor),/*大色块的颜色及样式(可以是圆形和方形)*/
dataModuleStyle:const QrDataModuleStyle(dataModuleShape:QrDataModuleShape.square, color: MyTheme.themeColor),/*小色块的颜色及样式(可以是圆形和方形)*/
)