Flutter/突跃:连续扫描少量条码后如何关闭摄像头

djmepvbi  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(289)

我用过flutter_barcode_scanner:^2.0.0包扫描条形码它的工作很好,但我连续扫描,几个条形码后,我想关闭相机,并返回到屏幕现在我的股票,所以相机是如何关闭后,扫描几个条形码.下面是一个场景,我不会实现

scanBarcode() {
                  FlutterBarcodeScanner.getBarcodeStreamReceiver(
                    "#ff6666",
                    "Cancel",
                    false,
                    ScanMode.DEFAULT,
                  )!
                      .listen(
                    (barcode) {
                      setState(() {
                        list.add(barcode);
                      });

                      if (list.length == 10) {
                        // I want to close camera here and show page again?
                      }
                    },
                  );
                }
7d7tgy0s

7d7tgy0s1#

我遇到了同样的问题。解决方法是在单次扫描上循环。因此,您仍然可以继续使用flutter_barcode_scanner包:)。
在我的情况下,我不需要调用setState或类似的东西来更新UI,但如果需要,您可以这样做。

Future<String?> _scanBarcodeNormal() async {
    String? barcodeScanRes;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      barcodeScanRes = await FlutterBarcodeScanner.scanBarcode('#ff6666', 'Cancel', true, ScanMode.BARCODE);
    } on PlatformException {
      barcodeScanRes = 'Failed to get platform version.';
    }
    return barcodeScanRes;
  }

void _scanBarCodeListAndFetchData() async{
    String? barcode = await _scanBarcodeNormal();
    final List<String> barcodeList = [];
    // continue scanning
    while(barcode != null){
      if(barcode == "-1"){ // cancel button -> -1
          barcode = null;
      }
      // add the barcode
      else {
        // avoid duplicates
        if(!barcodeList.contains(barcode)){
          barcodeList.add(barcode);
        }
        // you can also call setState(() {....} here
        if (barcodeList.length == 10) {
            barcode = null; // out !
        }
        else{ // next scan
            barcode = await _scanBarcodeNormal();
        }
      }
    }

    if(barcodeList.isEmpty) {
      UiUtils.showToast(message: "No barcode scanned, abort", toastLength: Toast.LENGTH_SHORT);
    }
    else{
      UiUtils.showToast(message: "${barcodeList.length} barcodes scanned", toastLength: Toast.LENGTH_SHORT);
      // your work
    }
  }

相关问题