2018-06-23 01:22:04 +00:00
import ' dart:async ' ;
import ' package:flutter/services.dart ' ;
2018-11-30 16:15:09 +00:00
2019-03-06 01:16:35 +00:00
String _kCustomType = ' __CUSTOM_ ' ;
2019-03-11 00:01:44 +00:00
enum FileType { ANY , IMAGE , VIDEO , AUDIO , CUSTOM , DIRECTORY_ONLY }
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
2019-03-08 01:42:07 +00:00
FilePicker . _ ( ) ;
2019-03-06 01:16:35 +00:00
static Future < dynamic > _getPath ( String type , [ bool multipleSelection = false ] ) 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 ] ;
}
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-03-11 00:01:44 +00:00
print ( ' [ $ _tag ] Platform exception: ' + e . toString ( ) ) ;
2018-12-27 14:02:24 +00:00
} catch ( e ) {
2019-03-06 01:16:35 +00:00
print ( e . toString ( ) ) ;
2018-12-27 14:02:24 +00:00
print (
2019-03-11 00:01:44 +00:00
' [ $ _tag ] Unsupported operation. This probably have happened because [ ${ type . split ( ' _ ' ) . last } ] is an unsupported file type. You may want to try FileType.ALL instead. ' ) ;
}
return null ;
}
static Future < String > getDirectoryPath ( ) async {
try {
final String result = await _channel . invokeMethod ( " getDirectoryPath " ) ;
return result ;
} on PlatformException catch ( e ) {
print ( ' [ $ _tag ] Platform exception: ' + e . toString ( ) ) ;
} catch ( e ) {
print ( e . toString ( ) ) ;
print ( ' [ $ _tag ] Unsupported operation. ' ) ;
2018-12-27 14:02:24 +00:00
}
return null ;
}
2018-09-11 14:59:35 +00:00
2019-03-06 01:16:35 +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.
static Future < Map < String , String > > getMultiFilePath ( { String fileExtension } ) async = >
2019-03-08 01:42:07 +00:00
await _getPath ( fileExtension ! = null & & fileExtension ! = ' ' ? ( _kCustomType + fileExtension ) : ' ANY ' , true ) ;
2018-09-11 14:59:35 +00:00
2018-12-27 14:02:24 +00:00
/// 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.
static Future < String > getFilePath ( { FileType type = FileType . ANY , String fileExtension } ) async {
2019-03-06 01:16:35 +00:00
var path ;
2018-09-11 14:59:35 +00:00
switch ( type ) {
case FileType . IMAGE:
2019-03-06 01:16:35 +00:00
path = _getPath ( ' IMAGE ' ) ;
break ;
2019-03-05 19:02:14 +00:00
case FileType . AUDIO:
2019-03-06 01:16:35 +00:00
path = _getPath ( ' AUDIO ' ) ;
break ;
2018-12-05 15:32:02 +00:00
case FileType . VIDEO:
2019-03-06 01:16:35 +00:00
path = _getPath ( ' VIDEO ' ) ;
break ;
2018-12-06 15:21:34 +00:00
case FileType . ANY:
2019-03-06 01:16:35 +00:00
path = _getPath ( ' ANY ' ) ;
break ;
2018-12-27 14:02:24 +00:00
case FileType . CUSTOM:
2019-03-06 01:16:35 +00:00
path = _getPath ( _kCustomType + ( fileExtension ? ? ' ' ) ) ;
break ;
2018-11-30 16:15:09 +00:00
default :
2019-03-06 01:16:35 +00:00
break ;
2018-09-11 14:59:35 +00:00
}
2019-03-06 01:16:35 +00:00
return await path ;
2018-09-11 14:59:35 +00:00
}
}