Skip to content

Commit

Permalink
add hasCameraPermission getter
Browse files Browse the repository at this point in the history
  • Loading branch information
navaronbracke committed Sep 18, 2024
1 parent 93e38b4 commit 1ab7f5a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/lib/barcode_scanner_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class _BarcodeScannerWithControllerState

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (!controller.value.isInitialized) {
if (!controller.value.hasCameraPermission) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/mobile_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ class _MobileScannerState extends State<MobileScanner>

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (widget.controller != null) return;
if (!controller.value.isInitialized) {
if (widget.controller != null || !controller.value.hasCameraPermission) {
return;
}

Expand Down
10 changes: 9 additions & 1 deletion lib/src/objects/mobile_scanner_state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:ui';

import 'package:mobile_scanner/src/enums/camera_facing.dart';
import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart';
import 'package:mobile_scanner/src/enums/torch_state.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';

Expand Down Expand Up @@ -43,7 +44,8 @@ class MobileScannerState {

/// Whether the mobile scanner has initialized successfully.
///
/// This is `true` if the camera is ready to be used.
/// This does not indicate that the camera permission was granted.
/// To check if the camera permission was granted, use [hasCameraPermission].
final bool isInitialized;

/// Whether the mobile scanner is currently running.
Expand All @@ -60,6 +62,12 @@ class MobileScannerState {
/// The current zoom scale of the camera.
final double zoomScale;

/// Whether permission to access the camera was granted.
bool get hasCameraPermission {
return isInitialized &&
error?.errorCode != MobileScannerErrorCode.permissionDenied;
}

/// Create a copy of this state with the given parameters.
MobileScannerState copyWith({
int? availableCameras,
Expand Down

0 comments on commit 1ab7f5a

Please sign in to comment.