juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
807 stars 471 forks source link

toggleTorch() is out of sync with the actual torch state #833

Closed edwintsai closed 9 months ago

edwintsai commented 10 months ago

I had upgrade to mobile_scanner 3.5.1, If I set torchEnabled : true, it's working in start the camera, but can't stop torch if I call toggleTorch.

IconButton( color: Colors.white, icon: ValueListenableBuilder( valueListenable: cameraController.torchState, builder: (context, state, child) { switch (state) { case TorchState.off: return const Icon(Icons.flash_off, color: Colors.grey); case TorchState.on: return const Icon(Icons.flash_on, color: Colors.yellow); } }, ), iconSize: 32.0, onPressed: () =>cameraController.toggleTorch(), ) ... controller: MobileScannerController( formats: [BarcodeFormat.qrCode], detectionSpeed: DetectionSpeed.noDuplicates, //DetectionSpeed.normal, facing: CameraFacing.back, detectionTimeoutMs: 1000, returnImage: false, torchEnabled: false, ),

navaronbracke commented 10 months ago

I think I know what the problem might be here.

We turn on the torch if torchEnabled is true. However, we do not forward the torch state to the notifier. And in toggleTorch() we do not check the current state either.

As a workaround you could try to set the torch after the controller was started.

navaronbracke commented 10 months ago

What we should do is the following:

navaronbracke commented 9 months ago

@edwintsai This should have been fixed on master now. I'm currently looking into a small MLKit related issue on Android, but there should be a hotfix later today with this bug fixed.