Showing
2 changed files
with
17 additions
and
2 deletions
| @@ -9,6 +9,9 @@ class MobileScanner extends StatefulWidget { | @@ -9,6 +9,9 @@ class MobileScanner extends StatefulWidget { | ||
| 9 | /// The controller of the camera. | 9 | /// The controller of the camera. |
| 10 | final MobileScannerController? controller; | 10 | final MobileScannerController? controller; |
| 11 | 11 | ||
| 12 | + /// Calls the provided [onPermissionSet] callback when the permission is set. | ||
| 13 | + final Function(bool permissionGranted)? onPermissionSet; | ||
| 14 | + | ||
| 12 | /// Function that gets called when a Barcode is detected. | 15 | /// Function that gets called when a Barcode is detected. |
| 13 | /// | 16 | /// |
| 14 | /// [barcode] The barcode object with all information about the scanned code. | 17 | /// [barcode] The barcode object with all information about the scanned code. |
| @@ -34,6 +37,7 @@ class MobileScanner extends StatefulWidget { | @@ -34,6 +37,7 @@ class MobileScanner extends StatefulWidget { | ||
| 34 | this.controller, | 37 | this.controller, |
| 35 | this.fit = BoxFit.cover, | 38 | this.fit = BoxFit.cover, |
| 36 | this.allowDuplicates = false, | 39 | this.allowDuplicates = false, |
| 40 | + this.onPermissionSet, | ||
| 37 | }) : super(key: key); | 41 | }) : super(key: key); |
| 38 | 42 | ||
| 39 | @override | 43 | @override |
| @@ -48,7 +52,7 @@ class _MobileScannerState extends State<MobileScanner> | @@ -48,7 +52,7 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 48 | void initState() { | 52 | void initState() { |
| 49 | super.initState(); | 53 | super.initState(); |
| 50 | WidgetsBinding.instance.addObserver(this); | 54 | WidgetsBinding.instance.addObserver(this); |
| 51 | - controller = widget.controller ?? MobileScannerController(); | 55 | + controller = widget.controller ?? MobileScannerController(onPermissionSet: widget.onPermissionSet); |
| 52 | } | 56 | } |
| 53 | 57 | ||
| 54 | @override | 58 | @override |
| @@ -121,7 +125,7 @@ class _MobileScannerState extends State<MobileScanner> | @@ -121,7 +125,7 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 121 | } | 125 | } |
| 122 | } else { | 126 | } else { |
| 123 | if (widget.controller == null) { | 127 | if (widget.controller == null) { |
| 124 | - controller = MobileScannerController(); | 128 | + controller = MobileScannerController(onPermissionSet: widget.onPermissionSet); |
| 125 | } else if (oldWidget.controller != widget.controller) { | 129 | } else if (oldWidget.controller != widget.controller) { |
| 126 | controller = widget.controller!; | 130 | controller = widget.controller!; |
| 127 | } | 131 | } |
| @@ -38,6 +38,7 @@ class MobileScannerController { | @@ -38,6 +38,7 @@ class MobileScannerController { | ||
| 38 | int? _controllerHashcode; | 38 | int? _controllerHashcode; |
| 39 | StreamSubscription? events; | 39 | StreamSubscription? events; |
| 40 | 40 | ||
| 41 | + Function(bool permissionGranted)? onPermissionSet; | ||
| 41 | final ValueNotifier<MobileScannerArguments?> args = ValueNotifier(null); | 42 | final ValueNotifier<MobileScannerArguments?> args = ValueNotifier(null); |
| 42 | final ValueNotifier<TorchState> torchState = ValueNotifier(TorchState.off); | 43 | final ValueNotifier<TorchState> torchState = ValueNotifier(TorchState.off); |
| 43 | late final ValueNotifier<CameraFacing> cameraFacingState; | 44 | late final ValueNotifier<CameraFacing> cameraFacingState; |
| @@ -60,6 +61,7 @@ class MobileScannerController { | @@ -60,6 +61,7 @@ class MobileScannerController { | ||
| 60 | this.ratio, | 61 | this.ratio, |
| 61 | this.torchEnabled, | 62 | this.torchEnabled, |
| 62 | this.formats, | 63 | this.formats, |
| 64 | + this.onPermissionSet, | ||
| 63 | }) { | 65 | }) { |
| 64 | // In case a new instance is created before calling dispose() | 66 | // In case a new instance is created before calling dispose() |
| 65 | if (_controllerHashcode != null) { | 67 | if (_controllerHashcode != null) { |
| @@ -142,11 +144,14 @@ class MobileScannerController { | @@ -142,11 +144,14 @@ class MobileScannerController { | ||
| 142 | state = result | 144 | state = result |
| 143 | ? MobileScannerState.authorized | 145 | ? MobileScannerState.authorized |
| 144 | : MobileScannerState.denied; | 146 | : MobileScannerState.denied; |
| 147 | + onPermissionSet?.call(result); | ||
| 145 | break; | 148 | break; |
| 146 | case MobileScannerState.denied: | 149 | case MobileScannerState.denied: |
| 147 | isStarting = false; | 150 | isStarting = false; |
| 151 | + onPermissionSet?.call(false); | ||
| 148 | throw PlatformException(code: 'NO ACCESS'); | 152 | throw PlatformException(code: 'NO ACCESS'); |
| 149 | case MobileScannerState.authorized: | 153 | case MobileScannerState.authorized: |
| 154 | + onPermissionSet?.call(true); | ||
| 150 | break; | 155 | break; |
| 151 | } | 156 | } |
| 152 | } | 157 | } |
| @@ -177,6 +182,9 @@ class MobileScannerController { | @@ -177,6 +182,9 @@ class MobileScannerController { | ||
| 177 | } on PlatformException catch (error) { | 182 | } on PlatformException catch (error) { |
| 178 | debugPrint('${error.code}: ${error.message}'); | 183 | debugPrint('${error.code}: ${error.message}'); |
| 179 | isStarting = false; | 184 | isStarting = false; |
| 185 | + if (error.code == "MobileScannerWeb") { | ||
| 186 | + onPermissionSet?.call(false); | ||
| 187 | + } | ||
| 180 | // setAnalyzeMode(AnalyzeMode.none.index); | 188 | // setAnalyzeMode(AnalyzeMode.none.index); |
| 181 | return; | 189 | return; |
| 182 | } | 190 | } |
| @@ -189,6 +197,8 @@ class MobileScannerController { | @@ -189,6 +197,8 @@ class MobileScannerController { | ||
| 189 | hasTorch = startResult['torchable'] as bool; | 197 | hasTorch = startResult['torchable'] as bool; |
| 190 | 198 | ||
| 191 | if (kIsWeb) { | 199 | if (kIsWeb) { |
| 200 | + onPermissionSet?.call(true); // If we reach this line, it means camera permission has been granted | ||
| 201 | + | ||
| 192 | args.value = MobileScannerArguments( | 202 | args.value = MobileScannerArguments( |
| 193 | webId: startResult['ViewID'] as String?, | 203 | webId: startResult['ViewID'] as String?, |
| 194 | size: Size( | 204 | size: Size( |
| @@ -272,6 +282,7 @@ class MobileScannerController { | @@ -272,6 +282,7 @@ class MobileScannerController { | ||
| 272 | events?.cancel(); | 282 | events?.cancel(); |
| 273 | events = null; | 283 | events = null; |
| 274 | _controllerHashcode = null; | 284 | _controllerHashcode = null; |
| 285 | + onPermissionSet = null; | ||
| 275 | } | 286 | } |
| 276 | barcodesController.close(); | 287 | barcodesController.close(); |
| 277 | } | 288 | } |
-
Please register or login to post a comment