flutter_file_picker/lib/file_picker.dart

88 lines
2.9 KiB
Dart
Raw Normal View History

2018-06-23 01:22:04 +00:00
import 'dart:async';
import 'dart:io';
2018-06-23 01:22:04 +00:00
import 'package:flutter/services.dart';
enum FileType {
ANY,
IMAGE,
VIDEO,
AUDIO,
CUSTOM,
}
2018-06-23 01:22:04 +00:00
class FilePicker {
static const MethodChannel _channel = const MethodChannel('file_picker');
2018-12-27 14:02:24 +00:00
static const String _tag = 'FilePicker';
2018-06-23 01:22:04 +00:00
FilePicker._();
/// 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.
2019-06-24 10:10:19 +00:00
static Future<Map<String, String>> getMultiFilePath(
{FileType type = FileType.ANY, String fileExtension}) async =>
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.
2019-06-24 10:10:19 +00:00
static Future<String> getFilePath(
{FileType type = FileType.ANY, String fileExtension}) async =>
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.
2019-06-24 10:10:19 +00:00
static Future<File> getFile(
{FileType type = FileType.ANY, String fileExtension}) async {
final String filePath =
await _getPath(_handleType(type, fileExtension), false);
return filePath != null ? File(filePath) : null;
}
static Future<dynamic> _getPath(String type, bool multipleSelection) async {
2018-12-27 14:02:24 +00:00
try {
dynamic result = await _channel.invokeMethod(type, multipleSelection);
if (result != null && multipleSelection) {
if (result is String) {
result = [result];
}
2019-06-24 10:10:19 +00:00
return Map<String, String>.fromIterable(result,
key: (path) => path.split('/').last, value: (path) => path);
}
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) {
2019-06-24 10:10:19 +00:00
print(
'[$_tag] Unsupported operation. Method not found. The exception thrown was: $e');
rethrow;
2019-03-11 00:01:44 +00:00
}
}
static String _handleType(FileType type, String fileExtension) {
switch (type) {
case FileType.IMAGE:
return 'IMAGE';
case FileType.AUDIO:
return 'AUDIO';
2018-12-05 15:32:02 +00:00
case FileType.VIDEO:
return 'VIDEO';
2018-12-06 15:21:34 +00:00
case FileType.ANY:
return 'ANY';
2018-12-27 14:02:24 +00:00
case FileType.CUSTOM:
return '__CUSTOM_' + (fileExtension ?? '');
default:
return 'ANY';
}
}
}