showPhotoSelectionSheet function

Future<List<XFile>> showPhotoSelectionSheet({
  1. required BuildContext context,
  2. PhotoSelectionL10n l10n = const PhotoSelectionL10n(),
  3. VoidCallback? onSettingAppOpenRequested,
  4. bool requestFullMetadata = false,
  5. bool allowMultiple = false,
})

Implementation

Future<List<XFile>> showPhotoSelectionSheet({
  required BuildContext context,
  PhotoSelectionL10n l10n = const PhotoSelectionL10n(),
  VoidCallback? onSettingAppOpenRequested,
  bool requestFullMetadata = false,
  bool allowMultiple = false,
}) async {
  final source = await showModalActionSheet<ImageSource>(
    context: context,
    title: l10n.title,
    actions: [
      SheetAction(
        icon: Icons.perm_media,
        label: l10n.chooseFromLibrary,
        key: ImageSource.gallery,
      ),
      SheetAction(
        icon: Icons.camera_alt,
        label: l10n.takePicture,
        key: ImageSource.camera,
      ),
    ],
  );

  if (source == null) {
    return [];
  }

  final picker = ImagePicker();
  try {
    if (source == ImageSource.gallery && allowMultiple) {
      return await picker.pickMultiImage(
        requestFullMetadata: requestFullMetadata,
      );
    } else {
      final file = await picker.pickImage(
        source: source,
        requestFullMetadata: requestFullMetadata,
      );
      return file == null ? [] : [file];
    }
  } on PlatformException catch (e) {
    logger.warning(e);
    if (![
      ImagePickerErrorCodes.cameraAccessRestricted,
      ImagePickerErrorCodes.cameraAccessDenied,
      ImagePickerErrorCodes.photoAccessRestricted,
      ImagePickerErrorCodes.photoAccessDenied,
    ].contains(e.code)) {
      // ignore: use_build_context_synchronously
      showErrorDialog(context: context, error: e);
      return [];
    }

    if (onSettingAppOpenRequested == null) {
      return [];
    }

    const okKey = 'ok';
    // ignore: use_build_context_synchronously
    final result = await showModalActionSheet<String>(
      context: context,
      title: l10n.accessRestrictedTitle,
      message: l10n.accessRestrictedMessage,
      actions: [
        SheetAction(
          label: l10n.accessRestrictedOk,
          key: okKey,
        ),
      ],
    );
    if (result == okKey) {
      onSettingAppOpenRequested();
    }
  }
  return [];
}