showPhotoSelectionSheet function

Future<PickedFile> showPhotoSelectionSheet (
  1. {@required BuildContext context,
  2. PhotoSelectionL10n l10n: const PhotoSelectionL10n(),
  3. VoidCallback onSettingAppOpenRequested}
)

Implementation

Future<PickedFile> showPhotoSelectionSheet({
  @required BuildContext context,
  PhotoSelectionL10n l10n = const PhotoSelectionL10n(),
  VoidCallback onSettingAppOpenRequested,
}) 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 null;
  }

  final picker = ImagePicker();
  try {
    return await picker.getImage(source: source);
  } on PlatformException catch (e) {
    logger.warning(e);
    if (![
      ImagePickerErrorCodes.cameraAccessRestricted,
      ImagePickerErrorCodes.cameraAccessDenied,
      ImagePickerErrorCodes.photoAccessRestricted,
      ImagePickerErrorCodes.photoAccessDenied,
    ].contains(e.code)) {
      showErrorDialog(context: context, error: e);
      return null;
    }

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

    const okKey = 'ok';
    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 null;
}