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
744 stars 444 forks source link

Please create a way for the autostart to be se to not final and we can change it to false or true. #1011

Open kate-abracosa opened 1 month ago

kate-abracosa commented 1 month ago

The reason is the permission setting you made on the package was not flawless. if camera is not permitted then nothing already it cannot be read also externally the status of the permission when it was permanently denied. So just make the autostart to be customizable so on initstate i can make my own getpermission then when the app was set to paused state it wont turns white since i will set it to autstart = true if permission is granted

cameraController = MobileScannerController( facing: CameraFacing.back, autoStart: false, ); getPermission();

diegonc commented 1 month ago

Can't you just call the start() method on cameraController according to the getPermission() result?

EDIT: BTW I just noticed the beta for verison 5.0.0 removed the autoStart parameter, so the issue is probably no longer relevant

kate-abracosa commented 1 month ago

Yes, I called cameracontroller.start() if permission is granted. However the problem was if the app state is set to paused or inactive. I am seeing white and the scanner is not working. This is because i set the autostart to false. If I set it to true. I wont have an issue even if the app is set to paused or inactive. However, if i did not set the autostart to false, I will get an issue with permission setting since on initstate the mobilescanner has its own permission but if the permission has been denied then. the permission_handler which is another package does not seem to recognize the event so I cannot show a bottomsheet which says We need the permission to access your camera allow (open appsetting) or later and close the full screen . When will the pre release version be stable? I can try on it on our project but we are near UAT testing.

diegonc commented 1 month ago

Yeah, I also faced that issue. I copied some bits of the old mobile_scanner version (the one that used to have autoStart) Something like the code below should work. As you can see, I'm using the MobileScanner widget from a StatefulWidget but I guess the overall approach should work in other setups.

class _ScannerState extends State<Scanner> with WidgetsBindingObserver {
  MobileScannerController? _controller;
  bool _resumeFromBackground = false;

  @override
  void onInit() {
    super.onInit();
    _controller = MobileScannerController(
        detectionSpeed: DetectionSpeed.normal, autoStart: false);
     WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    _controller?.dispose();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (_controller != null && _controller!.isStarting) {
      return;
    }

    switch (state) {
      case AppLifecycleState.resumed:
        if (_resumeFromBackground) {
          _controller.start();
        }
      case AppLifecycleState.inactive:
        _resumeFromBackground = true;
        _controller.stop();
      default:
        break;
    }
  }
}
kate-abracosa commented 1 month ago

Hi Diego,

Thank you so much, I also did the same yesterday on my code and it worked.

navaronbracke commented 1 month ago

Can't you just call the start() method on cameraController according to the getPermission() result?

EDIT: BTW I just noticed the beta for verison 5.0.0 removed the autoStart parameter, so the issue is probably no longer relevant

I do not understand the original intention here? Indeed, we removed the autoStart parameter, because doing lifecycle things inside the MobileScanner widget caused issues with widget tree reparenting.