1、需在pubspec.yaml中導(dǎo)入如下紅色箭頭的庫,并執(zhí)行flutter packages get命令

2、在需要生成二維碼的界面導(dǎo)入以下頭文件

3、在觸發(fā)掃描二維碼的方法中調(diào)用以下代碼即可
Futurescan()async {
try {
// 此處為掃碼結(jié)果,barcode為二維碼的內(nèi)容
? ? String barcode =await BarcodeScanner.scan();
? ? print('掃碼結(jié)果: '+barcode);
? ? if (barcode.contains('target=%2Fusers%2F')) {
//掃描到了用戶uid
? ? }else if(barcode.contains('m/invitation.html')) {
//跳轉(zhuǎn)到H5
? ? }
}on PlatformExceptioncatch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
// 未授予APP相機權(quán)限
? ? ? print('未授予APP相機權(quán)限');
? ? ? //未授予APP相機權(quán)限--打開設(shè)置頁面去設(shè)置
? ? ? bool isOpened =await PermissionHandler().openAppSettings();
? ? }else {
// 掃碼錯誤
? ? ? print('掃碼錯誤: $e');
? ? }
}on FormatException {
// 進入掃碼頁面后未掃碼就返回
? ? print('進入掃碼頁面后未掃碼就返回');
? }catch (e) {
// 掃碼錯誤
? ? print('掃碼錯誤: $e');
? }
}