gpt4 book ai didi

android - Flutter:如何暂停和重新启动 `Stream` 监听器?

转载 作者:行者123 更新时间:2023-12-04 03:37:43 25 4
gpt4 key购买 nike

我正在开发一个 flutter 应用程序来读取二维码。我正在使用 qr_code_scanner: ^0.3.5图书馆。下面是我的代码。

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

class ScanQRCodeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text("Scan QR Code"),
),
body: _ScanQRCodeUI());
}
}

class _ScanQRCodeUI extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ScanQRCodeUIState();
}
}

class _ScanQRCodeUIState extends State<_ScanQRCodeUI> {
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() {
super.reassemble();
if (Platform.isAndroid) {
controller.pauseCamera();
} else if (Platform.isIOS) {
controller.resumeCamera();
}
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(flex: 4, child: _buildQrView(context)),
Expanded(flex: 1, child: _dataDisplayUI())
],
);
}

Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
var scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 200.0
: 400.0;
// To ensure the Scanner view is properly sizes after rotation
// we need to listen for Flutter SizeChanged notification and update controller
return QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.red,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: scanArea),
);
}

void _onQRViewCreated(QRViewController controller) {
setState(() {
this.controller = controller;
});

controller.scannedDataStream.listen((scanData) async {
print("Hello0");
setState(() {
result = scanData;
print(result.code);
});

// await controller.pauseCamera();
});
}

Widget _dataDisplayUI() {
const yellowColor = const Color(0xffEDE132);

return Column(
children: [
Row(
children: [
Expanded(
flex: 7,
child: Container(
margin:
EdgeInsets.only(top: 30, bottom: 30, left: 10, right: 10),
child:
Text("You have added 12 products. Click here to publish.",
style: GoogleFonts.poppins(
textStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.normal,
fontSize: 14,
))),
)),
Expanded(
flex: 3,
child: Container(
width: 60,
height: 60,
child: Center(
child: Text("12",
style: GoogleFonts.poppins(
textStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 21,
)))),
decoration:
BoxDecoration(shape: BoxShape.circle, color: yellowColor),
))
],
)
],
);
}

@override
void dispose() {
controller?.dispose();
super.dispose();
}
}
我正在使用这个应用程序逐个扫描产品,就像收银员使用条形码扫描仪在 super 标记中所做的那样。
问题是,这个扫描仪正在监听 stream它继续运行。关注 _onQRViewCreated方法。结果,在我们将相机移至下一个 QR 码之前,同一个 QR 码已被多次读取。
如何确保 2 次扫描之间存在延迟?例如,当我扫描一个二维码时,我必须再等待 2 秒才能扫描下一个二维码。
如果我在 2 次扫描之间创建延迟的想法是错误的,我也愿意接受其他想法。

最佳答案

您可以使用

controller.scannedDataStream.first
停止监听流中的其他事件。
另一种解决方案是设置一个内部状态属性,如下所示:
bool QrBeingProcessed = false;
当您扫描第一个 qr 时,将其设置为 true,直到您完成。

关于android - Flutter:如何暂停和重新启动 `Stream` 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66565755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com