onCameraPicked static method

Future<File?> onCameraPicked({
  1. required BuildContext context,
  2. int? imageQuality,
})

Opens the device camera to take a photo. Checks for camera permissions first, and shows a settings permission dialog if permanently denied. Returns the picked File or null if the user cancelled or denied permission.

Implementation

static Future<File?> onCameraPicked({
  required BuildContext context,
  int? imageQuality,
}) async {
  try {
    final status = await Permission.camera.status;

    if (status.isPermanentlyDenied) {
      // Check if context is still mounted before using it across an async gap
      if (!context.mounted) return null;
      await PermissionAccessUtils.showPermissionDialog(context, 'Camera');
      return null;
    }

    final picker = ImagePicker();
    // Use the built-in imageQuality parameter to save memory and processing time
    final pickedFile = await picker.pickImage(
      source: ImageSource.camera,
      imageQuality: imageQuality,
    );

    if (pickedFile == null) return null;

    return File(pickedFile.path);
  } catch (e) {
    // Handle or log potential platform exceptions
    debugPrint('Error picking image from camera: $e');
    return null;
  }
}