QR扫描仪在重新启动时显示黑屏



我正在开发一个qr扫描仪应用程序。我有一个问题,当我在手机上重新启动应用程序时,它会显示黑屏,但当我执行重新加载时,它工作正常。这个错误只发生在手机上。它在模拟器中运行良好。

@override
Widget build(BuildContext context) {
if (controller != null && mounted) {
controller!.pauseCamera();
controller!.resumeCamera();
}
...
}

您使用的是qr_code_scanner plugIN现在,在这个插件中,没有更多的更新和维护模式。

注释(来自插头IN(由于该软件包的底层框架,android版的zxing和iOS版的MTBBarcodescanner都不再是主流,因此该插件不再是最新的,仅处于维护模式。只考虑错误修复和小的增强功能。

这个插件的替代方案是mobile_scanner 2.1.0

但是你还想使用qr_code_scanner插件吗?然后按照这种方式

覆盖此方法

//为了使热重新加载工作,如果平台//是android,或者如果平台是iOS,则恢复相机。

QRViewController? controller;
@override
void reassemble() async {
super.reassemble();
if (controller != null) {
debugPrint('reassemble : $controller');
if (Platform.isAndroid) {
await controller!.pauseCamera();
} else if (Platform.isIOS) {
await controller!.resumeCamera();
}
}
}

在自动热装的侧建方法中,做这个

注意:这是为Android(我不尝试在IOS上可能工作(

if (controller != null && mounted) {
setState(() {
controller!.resumeCamera();
});
}

查看我的完整代码

此页面是从另一个页面导航的,扫描仪将在此处打开。

class ScanQrCode extends StatefulWidget {
static const routeName = 'scanQrCode';
const ScanQrCode({Key? key}) : super(key: key);
@override
State<ScanQrCode> createState() => _ScanQrCodeState();
}
class _ScanQrCodeState extends State<ScanQrCode> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
Barcode? result;
QRViewController? controller;
// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@override
void reassemble() async {
super.reassemble();
if (controller != null) {
debugPrint('reassemble : $controller');
if (Platform.isAndroid) {
await controller!.pauseCamera();
} else if (Platform.isIOS) {
await controller!.resumeCamera();
}
}
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// for auto hot reload 
if (controller != null && mounted) {
setState(() {
controller!.resumeCamera();
});
}
return Scaffold(
appBar: AppBar(
elevation: 0.0,
systemOverlayStyle: const SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
centerTitle: true,
automaticallyImplyLeading: false,
title: Text(
'Scan Any QR',
style: UPITextStyle.boldOblique.copyWith(
fontSize: 18,
fontWeight: FontWeight.w900,
),
),
actions: [
Container(
width: 24,
height: 24,
margin: const EdgeInsets.only(right: 15),
child: FloatingActionButton(
elevation: 0.0,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
autofocus: false,
disabledElevation: 0,
focusElevation: 0,
highlightElevation: 3,
hoverElevation: 0,
backgroundColor: Colors.white.withOpacity(0.5),
onPressed: () => Navigator.maybePop(context),
child: Icon(
Icons.close,
size: 14,
color: Colors.black.withOpacity(0.7),
),
),
),
],
),
body: Column(
children: [
Flexible(
flex: 5,
child: buildQrView(context),
),
Flexible(
flex: 2,
child: SizedBox(
child: Text('${controller?.getCameraInfo()}'),
),
)
],
),
);
}
Widget buildQrView(BuildContext context) {
// if (controller != null && mounted) {
//   setState(() {
//     controller!.resumeCamera();
//   });
// }
return QRView(
key: qrKey,
onQRViewCreated: onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: upiMidNightBlue,
borderWidth: 8,
borderLength: 25,
borderRadius: 15,
overlayColor: Colors.black.withOpacity(0.5),
),
);
}
void onQRViewCreated(QRViewController control) {
setState(
() {
controller = control;
debugPrint('controller : $controller');
},
);
}
}

希望这能帮助你

相关内容

最新更新