简体   繁体   English

Flutter - 在 flutter 中扫描后关闭条码扫描器

[英]Flutter - Close barcode scanner after scan in flutter

I am using this package package for scanning barcode.我正在使用这个 package package扫描条码。 It works fine but after successfully scan I want to close the camera.它工作正常但在成功扫描后我想关闭相机。 I searched for the solution but no solution worked in my case.我搜索了解决方案,但没有解决方案适用于我的情况。 Here is my code.这是我的代码。

    try {
      FlutterBarcodeScanner.getBarcodeStreamReceiver(
              '#ff6666', 'Cancel', true, ScanMode.BARCODE)!
          .listen((data) {
        print(data);
       
      });
    } on PlatformException {
      // barcodeScanRes = 'Failed to get platform version.';
    }
  }

you can close scan page by adding this line to your code您可以通过将此行添加到您的代码来关闭扫描页面

if (barcodeScanRes != null){
   print(barcodeScanRes);
   // this will send your scan result to previous page
   // or you can navigate to other page after scan success
   Navigator.pop(context, barcodeScanRes); 
}
String barcodeScanRes; // put this variable in statefull widget
Future<void> scanQR() async {
  try {
    barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
        '#ff6666', 'Cancel', true, ScanMode.QR);
    // add this line to close scanner or naivigate to other page
    if (barcodeScanRes != null){
       print(barcodeScanRes);
       Navigator.pop(context, barcodeScanRes);
    }
  } on PlatformException {
    barcodeScanRes = 'Failed to get platform version.';
  }

  if (!mounted) return;
  setState(() {
    _scanBarcode = barcodeScanRes;
  });
}

or if you want to pause the camera after scan, you can use this package https://pub.dev/packages/qr_code_scanner或者如果你想在扫描后暂停相机,你可以使用这个 package https://pub.dev/packages/qr_code_scanner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM