2018-06-23 01:22:04 +00:00
|
|
|
import 'dart:async';
|
2019-03-12 01:25:23 +00:00
|
|
|
import 'dart:io';
|
2018-06-23 01:22:04 +00:00
|
|
|
|
2020-06-11 18:29:13 +00:00
|
|
|
import 'package:file_picker_platform_interface/file_picker_platform_interface.dart';
|
2020-07-23 16:25:21 +00:00
|
|
|
import 'package:file_picker_platform_interface/method_channel_file_picker.dart';
|
2018-11-30 16:15:09 +00:00
|
|
|
|
2020-06-12 01:19:25 +00:00
|
|
|
export 'package:file_picker_platform_interface/file_picker_platform_interface.dart'
|
|
|
|
show FileType;
|
2020-06-11 18:29:13 +00:00
|
|
|
|
2020-07-23 16:25:21 +00:00
|
|
|
final MethodChannelFilePicker _filePickerPlatform = FilePickerPlatform.instance;
|
2018-06-23 01:22:04 +00:00
|
|
|
|
|
|
|
class FilePicker {
|
2019-03-08 01:42:07 +00:00
|
|
|
FilePicker._();
|
2020-06-11 18:29:13 +00:00
|
|
|
|
|
|
|
/// Returns an absolute file path from the calling platform.
|
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// Extension filters are allowed with [FileType.custom], when used, make sure to provide a [List]
|
|
|
|
/// of `allowedExtensions` (e.g. [`pdf`, `svg`, `jpg`].).
|
2020-06-12 01:19:25 +00:00
|
|
|
///
|
|
|
|
/// If you want to track picking status, for example, because some files may take some time to be
|
|
|
|
/// cached (particularly those picked from cloud providers), you may want to set [onFileLoading] handler
|
|
|
|
/// that will give you the current status of picking.
|
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// If you plan on picking images/videos and don't want them to be compressed automatically by OS,
|
|
|
|
/// you should set `allowCompression` to [false]. Calling this on Android won't have any effect, as
|
|
|
|
/// it already provides you the original file (or integral copy).
|
|
|
|
///
|
|
|
|
/// Defaults to [FileType.any] which will display all file types.
|
2020-06-12 01:19:25 +00:00
|
|
|
static Future<String> getFilePath({
|
|
|
|
FileType type = FileType.any,
|
|
|
|
List<String> allowedExtensions,
|
|
|
|
Function(FilePickerStatus) onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
bool allowCompression,
|
2020-06-12 01:19:25 +00:00
|
|
|
}) async =>
|
2020-06-11 18:29:13 +00:00
|
|
|
await _filePickerPlatform.getFiles(
|
|
|
|
type: type,
|
|
|
|
allowedExtensions: allowedExtensions,
|
2020-06-12 01:19:25 +00:00
|
|
|
onFileLoading: onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
allowCompression: allowCompression,
|
2020-06-11 18:29:13 +00:00
|
|
|
);
|
2019-03-08 01:42:07 +00:00
|
|
|
|
2020-07-20 14:31:14 +00:00
|
|
|
/// Returns an iterable [Map<String,String>] where the `key` is the name of the file
|
2019-03-12 01:25:23 +00:00
|
|
|
/// and the `value` the path.
|
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// A [List] with `allowedExtensions` can be provided to filter the allowed files to picked.
|
|
|
|
/// If provided, make sure you select [FileType.custom] as type.
|
2020-06-12 01:19:25 +00:00
|
|
|
///
|
|
|
|
/// If you want to track picking status, for example, because some files may take some time to be
|
2020-07-20 14:31:14 +00:00
|
|
|
/// cached (particularly those picked from cloud providers), you may want to set `onFileLoading` handler
|
2020-06-12 01:19:25 +00:00
|
|
|
/// that will give you the current status of picking.
|
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// If you plan on picking images/videos and don't want them to be compressed automatically by OS,
|
|
|
|
/// you should set `allowCompression` to [false]. Calling this on Android won't have any effect, as
|
|
|
|
/// it already provides you the original file (or integral copy).
|
|
|
|
///
|
2020-04-05 16:01:31 +00:00
|
|
|
/// Defaults to `FileType.any`, which allows any combination of files to be multi selected at once.
|
2020-06-12 01:19:25 +00:00
|
|
|
static Future<Map<String, String>> getMultiFilePath({
|
|
|
|
FileType type = FileType.any,
|
|
|
|
List<String> allowedExtensions,
|
|
|
|
Function(FilePickerStatus) onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
bool allowCompression,
|
2020-06-12 01:19:25 +00:00
|
|
|
}) async =>
|
2020-06-11 18:29:13 +00:00
|
|
|
await _filePickerPlatform.getFiles(
|
|
|
|
type: type,
|
|
|
|
allowMultiple: true,
|
|
|
|
allowedExtensions: allowedExtensions,
|
2020-06-12 01:19:25 +00:00
|
|
|
onFileLoading: onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
allowCompression: allowCompression,
|
2020-06-11 18:29:13 +00:00
|
|
|
);
|
2019-03-12 01:25:23 +00:00
|
|
|
|
2020-07-20 14:31:14 +00:00
|
|
|
/// Returns a [File] object from the selected file path.
|
2019-03-12 01:25:23 +00:00
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// This is an utility method that does the same of [getFilePath] but saving some boilerplate if
|
|
|
|
/// you are planing to create a [File] for the returned path.
|
2020-06-12 01:19:25 +00:00
|
|
|
static Future<File> getFile({
|
|
|
|
FileType type = FileType.any,
|
|
|
|
List<String> allowedExtensions,
|
|
|
|
Function(FilePickerStatus) onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
bool allowCompression,
|
2020-06-12 01:19:25 +00:00
|
|
|
}) async {
|
2020-06-11 18:29:13 +00:00
|
|
|
final String filePath = await _filePickerPlatform.getFiles(
|
|
|
|
type: type,
|
|
|
|
allowedExtensions: allowedExtensions,
|
2020-06-12 01:19:25 +00:00
|
|
|
onFileLoading: onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
allowCompression: allowCompression,
|
2020-06-11 18:29:13 +00:00
|
|
|
);
|
2019-03-16 12:18:10 +00:00
|
|
|
return filePath != null ? File(filePath) : null;
|
2019-03-12 01:25:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 14:31:14 +00:00
|
|
|
/// Returns a [List<File>] object from the selected files paths.
|
2019-08-31 14:14:28 +00:00
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// This is an utility method that does the same of [getMultiFilePath] but saving some boilerplate if
|
|
|
|
/// you are planing to create a list of [File]`s for the returned paths.
|
2020-06-12 01:19:25 +00:00
|
|
|
static Future<List<File>> getMultiFile({
|
|
|
|
FileType type = FileType.any,
|
|
|
|
List<String> allowedExtensions,
|
|
|
|
Function(FilePickerStatus) onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
bool allowCompression,
|
2020-06-12 01:19:25 +00:00
|
|
|
}) async {
|
2020-06-11 18:29:13 +00:00
|
|
|
final Map<String, String> paths = await _filePickerPlatform.getFiles(
|
|
|
|
type: type,
|
|
|
|
allowMultiple: true,
|
|
|
|
allowedExtensions: allowedExtensions,
|
2020-06-12 01:19:25 +00:00
|
|
|
onFileLoading: onFileLoading,
|
2020-07-20 14:31:14 +00:00
|
|
|
allowCompression: allowCompression,
|
2020-06-11 18:29:13 +00:00
|
|
|
);
|
|
|
|
|
2020-06-12 01:19:25 +00:00
|
|
|
return paths != null && paths.isNotEmpty
|
|
|
|
? paths.values.map((path) => File(path)).toList()
|
|
|
|
: null;
|
2019-08-31 14:14:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:49:39 +00:00
|
|
|
/// Selects a directory and returns its absolute path.
|
|
|
|
///
|
|
|
|
/// On Android, this requires to be running on SDK 21 or above, else won't work.
|
2020-07-20 14:31:14 +00:00
|
|
|
/// Returns [null] if folder path couldn't be resolved.
|
2020-06-04 21:49:39 +00:00
|
|
|
static Future<String> getDirectoryPath() async {
|
2020-06-11 18:29:13 +00:00
|
|
|
return _filePickerPlatform.getDirectoryPath();
|
2020-06-04 21:49:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 18:29:13 +00:00
|
|
|
/// Asks the underlying platform to remove any temporary files created by this plugin.
|
|
|
|
///
|
|
|
|
/// This typically relates to cached files that are stored in the cache directory of
|
|
|
|
/// each platform and it isn't required to invoke this as the system should take care
|
|
|
|
/// of it whenever needed. However, this will force the cleanup if you want to manage those on your own.
|
|
|
|
///
|
2020-07-20 14:31:14 +00:00
|
|
|
/// Returns [true] if the files were removed with success, [false] otherwise.
|
2020-06-11 18:29:13 +00:00
|
|
|
static Future<bool> clearTemporaryFiles() async {
|
|
|
|
return _filePickerPlatform.clearTemporaryFiles();
|
2019-03-11 00:01:44 +00:00
|
|
|
}
|
2018-09-11 14:59:35 +00:00
|
|
|
}
|