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
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
2018-11-30 16:15:09 +00:00
|
|
|
|
2019-03-12 01:25:23 +00:00
|
|
|
enum FileType {
|
2020-03-14 17:32:05 +00:00
|
|
|
any,
|
|
|
|
image,
|
|
|
|
video,
|
|
|
|
audio,
|
|
|
|
custom,
|
2019-03-12 01:25:23 +00:00
|
|
|
}
|
2018-06-23 01:22:04 +00:00
|
|
|
|
|
|
|
class FilePicker {
|
2019-03-08 01:42:07 +00:00
|
|
|
FilePicker._();
|
2020-03-14 17:32:05 +00:00
|
|
|
static const MethodChannel _channel = const MethodChannel('miguelruivo.flutter.plugins.file_picker');
|
|
|
|
static const String _tag = 'FilePicker';
|
2019-03-08 01:42:07 +00:00
|
|
|
|
2019-03-12 01:25:23 +00:00
|
|
|
/// Returns an iterable `Map<String,String>` where the `key` is the name of the file
|
|
|
|
/// and the `value` the path.
|
|
|
|
///
|
|
|
|
/// A [fileExtension] can be provided to filter the picking results.
|
|
|
|
/// If provided, it will be use the `FileType.CUSTOM` for that [fileExtension].
|
|
|
|
/// If not, `FileType.ANY` will be used and any combination of files can be multi picked at once.
|
2020-03-14 17:32:05 +00:00
|
|
|
static Future<Map<String, String>> getMultiFilePath({FileType type = FileType.any, String fileExtension}) async =>
|
2019-03-12 01:25:23 +00:00
|
|
|
await _getPath(_handleType(type, fileExtension), true);
|
|
|
|
|
|
|
|
/// Returns an absolute file path from the calling platform.
|
|
|
|
///
|
|
|
|
/// A [type] must be provided to filter the picking results.
|
|
|
|
/// Can be used a custom file type with `FileType.CUSTOM`. A [fileExtension] must be provided (e.g. PDF, SVG, etc.)
|
|
|
|
/// Defaults to `FileType.ANY` which will display all file types.
|
2020-03-14 17:32:05 +00:00
|
|
|
static Future<String> getFilePath({FileType type = FileType.any, String fileExtension}) async =>
|
2019-03-12 01:25:23 +00:00
|
|
|
await _getPath(_handleType(type, fileExtension), false);
|
|
|
|
|
|
|
|
/// Returns a `File` object from the selected file path.
|
|
|
|
///
|
|
|
|
/// 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-03-14 17:32:05 +00:00
|
|
|
static Future<File> getFile({FileType type = FileType.any, String fileExtension}) async {
|
|
|
|
final String filePath = await _getPath(_handleType(type, fileExtension), false);
|
2019-03-16 12:18:10 +00:00
|
|
|
return filePath != null ? File(filePath) : null;
|
2019-03-12 01:25:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 14:14:28 +00:00
|
|
|
/// Returns a `List<File>` object from the selected files paths.
|
|
|
|
///
|
|
|
|
/// 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-03-14 17:32:05 +00:00
|
|
|
static Future<List<File>> getMultiFile({FileType type = FileType.any, String fileExtension}) async {
|
|
|
|
final Map<String, String> paths = await _getPath(_handleType(type, fileExtension), true);
|
|
|
|
return paths != null && paths.isNotEmpty ? paths.values.map((path) => File(path)).toList() : null;
|
2019-08-31 14:14:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 01:25:23 +00:00
|
|
|
static Future<dynamic> _getPath(String type, bool multipleSelection) async {
|
2018-12-27 14:02:24 +00:00
|
|
|
try {
|
2019-03-06 01:16:35 +00:00
|
|
|
dynamic result = await _channel.invokeMethod(type, multipleSelection);
|
2019-03-08 01:42:07 +00:00
|
|
|
if (result != null && multipleSelection) {
|
2019-03-06 01:16:35 +00:00
|
|
|
if (result is String) {
|
|
|
|
result = [result];
|
|
|
|
}
|
2020-03-14 17:32:05 +00:00
|
|
|
return Map<String, String>.fromIterable(result, key: (path) => path.split('/').last, value: (path) => path);
|
2019-03-06 01:16:35 +00:00
|
|
|
}
|
|
|
|
return result;
|
2018-12-27 14:02:24 +00:00
|
|
|
} on PlatformException catch (e) {
|
2019-06-24 10:10:19 +00:00
|
|
|
print('[$_tag] Platform exception: $e');
|
|
|
|
rethrow;
|
2018-12-27 14:02:24 +00:00
|
|
|
} catch (e) {
|
2020-03-14 17:32:05 +00:00
|
|
|
print('[$_tag] Unsupported operation. Method not found. The exception thrown was: $e');
|
2019-06-24 10:10:19 +00:00
|
|
|
rethrow;
|
2019-03-11 00:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:25:23 +00:00
|
|
|
static String _handleType(FileType type, String fileExtension) {
|
2020-03-29 22:22:40 +00:00
|
|
|
if (type != FileType.custom && (fileExtension?.isNotEmpty ?? false)) {
|
|
|
|
throw Exception('If you are using a custom extension filter, please use the FileType.custom instead.');
|
|
|
|
}
|
2018-09-11 14:59:35 +00:00
|
|
|
switch (type) {
|
2020-03-14 17:32:05 +00:00
|
|
|
case FileType.image:
|
2019-03-12 01:25:23 +00:00
|
|
|
return 'IMAGE';
|
2020-03-14 17:32:05 +00:00
|
|
|
case FileType.audio:
|
2019-03-12 01:25:23 +00:00
|
|
|
return 'AUDIO';
|
2020-03-14 17:32:05 +00:00
|
|
|
case FileType.video:
|
2019-03-12 01:25:23 +00:00
|
|
|
return 'VIDEO';
|
2020-03-14 17:32:05 +00:00
|
|
|
case FileType.any:
|
2019-03-12 01:25:23 +00:00
|
|
|
return 'ANY';
|
2020-03-14 17:32:05 +00:00
|
|
|
case FileType.custom:
|
2019-03-12 01:25:23 +00:00
|
|
|
return '__CUSTOM_' + (fileExtension ?? '');
|
2018-11-30 16:15:09 +00:00
|
|
|
default:
|
2019-03-12 01:25:23 +00:00
|
|
|
return 'ANY';
|
2018-09-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|