Scarg
Scarg

Reputation: 53

Flutter - How to pause qr scan reading

I'm developing a qr scan app. And using qr_code_scanner 0.5.2.

When i scan a qr code, my app jump to another page with the scanned code, but the app still scanning the qr codes and new pages appears until you d not focus on any qr code.

How can i scan codes one by one and pause scanning while on the another page and resume scanning when the new page is closed?

Thanks

code:

  void _onQRViewCreated(QRViewController controller) {
setState(() {
  this.controller = controller;
});
controller.scannedDataStream.listen((scanData) {
  setState(() {
    controller.pauseCamera();
    Navigator.push(context,
      MaterialPageRoute(builder: (context) {
        return ReadData(data: scanData.code);
      }),
    );
    controller.resumeCamera();
  });
});

}

edit: solution code:

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

controller.scannedDataStream.listen((scanData) async {
 
  controller.pauseCamera();
  await Navigator.push(
    context,
    MaterialPageRoute(builder: (context) {
      return ReadData(data: scanData.code);
    }),
  );
  
  controller.resumeCamera();
});

}

Upvotes: 5

Views: 3686

Answers (1)

HasilT
HasilT

Reputation: 2609

qr_code_scanner controller got a method called pauseCamera use that to pause camera whenever a valid barcode is detected.

edit: if pauseCamera is not working then you can add boolean variable and switch it to true whenever you get valid QR code. And, navigate to new page only if this variable is false.

I've updated the code, Please take a look.

...
bool gotValidQR = false; //toggle this value when you got a new qr code
...
controller.scannedDataStream.listen((scanData)async {
    if(gotValidQR) {
      return;
    }
    gotValidQR = true;
    dynamic pop = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) {
        return ScannedData(data: scanData.code);
      }),
    );
    gotValidQR  = false;
});
...

Upvotes: 7

Related Questions