forked from flutter/plugins
/
method_channel_file_selector.dart
94 lines (86 loc) · 2.9 KB
/
method_channel_file_selector.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:cross_file/cross_file.dart';
import 'package:flutter/services.dart';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:meta/meta.dart';
const MethodChannel _channel =
MethodChannel('plugins.flutter.io/file_selector');
/// An implementation of [FileSelectorPlatform] that uses method channels.
class MethodChannelFileSelector extends FileSelectorPlatform {
/// The MethodChannel that is being used by this implementation of the plugin.
@visibleForTesting
MethodChannel get channel => _channel;
/// Load a file from user's computer and return it as an XFile
@override
Future<XFile> openFile({
List<XTypeGroup> acceptedTypeGroups,
String initialDirectory,
String confirmButtonText,
}) async {
final List<String> path = await _channel.invokeListMethod<String>(
'openFile',
<String, dynamic>{
'acceptedTypeGroups':
acceptedTypeGroups?.map((group) => group.toJSON())?.toList(),
'initialDirectory': initialDirectory,
'confirmButtonText': confirmButtonText,
'multiple': false,
},
);
return path == null ? null : XFile(path?.first);
}
/// Load multiple files from user's computer and return it as an XFile
@override
Future<List<XFile>> openFiles({
List<XTypeGroup> acceptedTypeGroups,
String initialDirectory,
String confirmButtonText,
}) async {
final List<String> pathList = await _channel.invokeListMethod<String>(
'openFile',
<String, dynamic>{
'acceptedTypeGroups':
acceptedTypeGroups?.map((group) => group.toJSON())?.toList(),
'initialDirectory': initialDirectory,
'confirmButtonText': confirmButtonText,
'multiple': true,
},
);
return pathList?.map((path) => XFile(path))?.toList() ?? [];
}
/// Gets the path from a save dialog
@override
Future<String> getSavePath({
List<XTypeGroup> acceptedTypeGroups,
String initialDirectory,
String suggestedName,
String confirmButtonText,
}) async {
return _channel.invokeMethod<String>(
'getSavePath',
<String, dynamic>{
'acceptedTypeGroups':
acceptedTypeGroups?.map((group) => group.toJSON())?.toList(),
'initialDirectory': initialDirectory,
'suggestedName': suggestedName,
'confirmButtonText': confirmButtonText,
},
);
}
/// Gets a directory path from a dialog
@override
Future<String> getDirectoryPath({
String initialDirectory,
String confirmButtonText,
}) async {
return _channel.invokeMethod<String>(
'getDirectoryPath',
<String, dynamic>{
'initialDirectory': initialDirectory,
'confirmButtonText': confirmButtonText,
},
);
}
}