pick method

Future<List<File?>> pick({
  1. required Source source,
  2. BuildContext? context,
  3. String? fileUrl,
  4. bool allowMultiple = true,
  5. FileType fileType = FileType.any,
  6. List<String>? allowedExtensions,
  7. CameraDevice? preferredCameraDevice,
})

Opens the device camera, gallery, file explorer or a file based on the provided source, and allows the user to pick one or more files according to the specified parameters.

Returns a List of File objects picked by the user.

source is the source from which to pick the files. Valid values are Source.camera, Source.gallery, Source.file, and Source.url.

context is the BuildContext object used to show the dialogs to the user. Required when picking files from camera or gallery.

fileUrl is the URL of the file to pick when source is Source.url.

allowMultiple is whether or not to allow the user to select multiple files.

fileType is the type of files to show when source is Source.file.

allowedExtensions is a list of file extensions to show when source is Source.file and fileType is FileType.custom. The extensions should be in lowercase without the leading period.

preferredCameraDevice is the preferred camera device to use when source is Source.camera.

Example usage:

List<File?> pickedFiles = await RhFiles.instance.pick(
  source: Source.file,
  allowMultiple: true,
  fileType: FileType.custom,
  allowedExtensions: ['jpg', 'png'],
);

Implementation

Future<List<File?>> pick({
  required Source source,
  BuildContext? context,
  String? fileUrl,
  bool allowMultiple = true,
  FileType fileType = FileType.any,
  List<String>? allowedExtensions,
  CameraDevice? preferredCameraDevice,
}) async {
  final utils = RhUtils.instance;
  switch (source) {
    case Source.camera:
      return utils.pickImagesFromDevice(
          source: ImageSource.camera,
          preferredCameraDevice: preferredCameraDevice);
    case Source.gallery:
      return utils.pickImagesFromDevice(
          source: ImageSource.gallery,
          preferredCameraDevice: preferredCameraDevice);
    case Source.file:
      return utils.pickFilesFromDevice(
          allowMultiple: allowMultiple,
          fileType: fileType,
          allowedExtensions: allowedExtensions ?? []);
    case Source.url:
      assert(fileUrl != null && fileUrl != "");
      return utils.getFileFromUrl(source: fileUrl!);
  }
}