Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit be33cb1

Browse files
committed
add getDirectoriesPath implementation
add dart and native tests add example apply feedback
1 parent 19920f9 commit be33cb1

File tree

11 files changed

+199
-16
lines changed

11 files changed

+199
-16
lines changed

packages/file_selector/file_selector_linux/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
.flutter-plugins
44
.flutter-plugins-dependencies
55
pubspec.lock
6+
7+
# Linux specific
8+
linux/CMakeFiles
9+
linux/CMakeCache.txt

packages/file_selector/file_selector_linux/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.0+2
2+
3+
* Adds `getDirectoryPaths` implementation.
4+
15
## 0.9.0+1
26

37
* Changes XTypeGroup initialization from final to const.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
6+
import 'package:flutter/material.dart';
7+
8+
/// Screen that allows the user to select one or more directories using `getDirectoryPaths`,
9+
/// then displays the selected directories in a dialog.
10+
class GetMultipleDirectoriesPage extends StatelessWidget {
11+
/// Default Constructor
12+
const GetMultipleDirectoriesPage({Key? key}) : super(key: key);
13+
14+
Future<void> _getDirectoryPaths(BuildContext context) async {
15+
const String confirmButtonText = 'Choose';
16+
final List<String> directoryPaths =
17+
await FileSelectorPlatform.instance.getDirectoryPaths(
18+
confirmButtonText: confirmButtonText,
19+
);
20+
if (directoryPaths.isEmpty) {
21+
// Operation was canceled by the user.
22+
return;
23+
}
24+
String paths = '';
25+
for (final String? path in directoryPaths) {
26+
paths += '${path!} \n';
27+
}
28+
await showDialog<void>(
29+
context: context,
30+
builder: (BuildContext context) => TextDisplay(paths),
31+
);
32+
}
33+
34+
@override
35+
Widget build(BuildContext context) {
36+
return Scaffold(
37+
appBar: AppBar(
38+
title: const Text('Select multiple directories'),
39+
),
40+
body: Center(
41+
child: Column(
42+
mainAxisAlignment: MainAxisAlignment.center,
43+
children: <Widget>[
44+
ElevatedButton(
45+
style: ElevatedButton.styleFrom(
46+
// TODO(darrenaustin): Migrate to new API once it lands in stable: https://github.com/flutter/flutter/issues/105724
47+
// ignore: deprecated_member_use
48+
primary: Colors.blue,
49+
// ignore: deprecated_member_use
50+
onPrimary: Colors.white,
51+
),
52+
child: const Text(
53+
'Press to ask user to choose multiple directories'),
54+
onPressed: () => _getDirectoryPaths(context),
55+
),
56+
],
57+
),
58+
),
59+
);
60+
}
61+
}
62+
63+
/// Widget that displays a text file in a dialog.
64+
class TextDisplay extends StatelessWidget {
65+
/// Creates a `TextDisplay`.
66+
const TextDisplay(this.directoriesPaths, {Key? key}) : super(key: key);
67+
68+
/// The path selected in the dialog.
69+
final String directoriesPaths;
70+
71+
@override
72+
Widget build(BuildContext context) {
73+
return AlertDialog(
74+
title: const Text('Selected Directories'),
75+
content: Scrollbar(
76+
child: SingleChildScrollView(
77+
child: Text(directoriesPaths),
78+
),
79+
),
80+
actions: <Widget>[
81+
TextButton(
82+
child: const Text('Close'),
83+
onPressed: () => Navigator.pop(context),
84+
),
85+
],
86+
);
87+
}
88+
}

packages/file_selector/file_selector_linux/example/lib/home_page.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ class HomePage extends StatelessWidget {
5555
child: const Text('Open a get directory dialog'),
5656
onPressed: () => Navigator.pushNamed(context, '/directory'),
5757
),
58+
const SizedBox(height: 10),
59+
ElevatedButton(
60+
style: style,
61+
child: const Text('Open a get multi directories dialog'),
62+
onPressed: () =>
63+
Navigator.pushNamed(context, '/multi-directories'),
64+
),
5865
],
5966
),
6067
),

packages/file_selector/file_selector_linux/example/lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:flutter/material.dart';
66

77
import 'get_directory_page.dart';
8+
import 'get_multiple_directories_page.dart';
89
import 'home_page.dart';
910
import 'open_image_page.dart';
1011
import 'open_multiple_images_page.dart';
@@ -36,6 +37,8 @@ class MyApp extends StatelessWidget {
3637
'/open/text': (BuildContext context) => const OpenTextPage(),
3738
'/save/text': (BuildContext context) => SaveTextPage(),
3839
'/directory': (BuildContext context) => const GetDirectoryPage(),
40+
'/multi-directories': (BuildContext context) =>
41+
const GetMultipleDirectoriesPage()
3942
},
4043
);
4144
}

packages/file_selector/file_selector_linux/example/pubspec.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: file_selector_linux_example
22
description: Local testbed for Linux file_selector implementation.
3-
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
3+
publish_to: 'none'
44
version: 1.0.0+1
55

66
environment:
@@ -9,7 +9,9 @@ environment:
99
dependencies:
1010
file_selector_linux:
1111
path: ../
12-
file_selector_platform_interface: ^2.2.0
12+
# TODO(adpinola): This should be 2.4.0 once it is published.
13+
file_selector_platform_interface:
14+
path: ../../file_selector_platform_interface/
1315
flutter:
1416
sdk: flutter
1517

packages/file_selector/file_selector_linux/lib/file_selector_linux.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,26 @@ class FileSelectorLinux extends FileSelectorPlatform {
102102
String? initialDirectory,
103103
String? confirmButtonText,
104104
}) async {
105-
return _channel.invokeMethod<String>(
106-
_getDirectoryPathMethod,
107-
<String, dynamic>{
108-
_initialDirectoryKey: initialDirectory,
109-
_confirmButtonTextKey: confirmButtonText,
110-
},
111-
);
105+
final List<String>? path = await _channel
106+
.invokeListMethod<String>(_getDirectoryPathMethod, <String, dynamic>{
107+
_initialDirectoryKey: initialDirectory,
108+
_confirmButtonTextKey: confirmButtonText,
109+
});
110+
return path?.first;
111+
}
112+
113+
@override
114+
Future<List<String>> getDirectoryPaths({
115+
String? initialDirectory,
116+
String? confirmButtonText,
117+
}) async {
118+
final List<String>? pathList = await _channel
119+
.invokeListMethod<String>(_getDirectoryPathMethod, <String, dynamic>{
120+
_initialDirectoryKey: initialDirectory,
121+
_confirmButtonTextKey: confirmButtonText,
122+
_multipleKey: true,
123+
});
124+
return pathList ?? <String>[];
112125
}
113126
}
114127

packages/file_selector/file_selector_linux/linux/file_selector_plugin.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
192192
FlValue* args = fl_method_call_get_args(method_call);
193193

194194
g_autoptr(FlMethodResponse) response = nullptr;
195-
if (strcmp(method, kOpenFileMethod) == 0) {
195+
if (strcmp(method, kOpenFileMethod) == 0 ||
196+
strcmp(method, kGetDirectoryPathMethod) == 0) {
196197
response = show_dialog(self, method, args, true);
197-
} else if (strcmp(method, kGetDirectoryPathMethod) == 0 ||
198-
strcmp(method, kGetSavePathMethod) == 0) {
198+
} else if (strcmp(method, kGetSavePathMethod) == 0) {
199199
response = show_dialog(self, method, args, false);
200200
} else {
201201
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());

packages/file_selector/file_selector_linux/linux/test/file_selector_plugin_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ TEST(FileSelectorPlugin, TestGetDirectory) {
169169
EXPECT_EQ(gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(dialog)),
170170
false);
171171
}
172+
173+
TEST(FileSelectorPlugin, TestGetMultipleDirectories) {
174+
g_autoptr(FlValue) args = fl_value_new_map();
175+
fl_value_set_string_take(args, "multiple", fl_value_new_bool(true));
176+
177+
g_autoptr(GtkFileChooserNative) dialog =
178+
create_dialog_for_method(nullptr, "getDirectoryPath", args);
179+
180+
ASSERT_NE(dialog, nullptr);
181+
EXPECT_EQ(gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)),
182+
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
183+
EXPECT_EQ(gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(dialog)),
184+
true);
185+
}

packages/file_selector/file_selector_linux/pubspec.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: file_selector_linux
22
description: Liunx implementation of the file_selector plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector_linux
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
5-
version: 0.9.0+1
5+
version: 0.9.0+2
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"
@@ -18,7 +18,9 @@ flutter:
1818

1919
dependencies:
2020
cross_file: ^0.3.1
21-
file_selector_platform_interface: ^2.2.0
21+
# TODO(adpinola): This should be 2.4.0 once it is published.
22+
file_selector_platform_interface:
23+
path: ../file_selector_platform_interface
2224
flutter:
2325
sdk: flutter
2426

packages/file_selector/file_selector_linux/test/file_selector_linux_test.dart

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,60 @@ void main() {
373373
);
374374
});
375375
test('passes confirmButtonText correctly', () async {
376-
await plugin.getDirectoryPath(confirmButtonText: 'Open File');
376+
await plugin.getDirectoryPath(confirmButtonText: 'Select Folder');
377377

378378
expect(
379379
log,
380380
<Matcher>[
381381
isMethodCall('getDirectoryPath', arguments: <String, dynamic>{
382382
'initialDirectory': null,
383-
'confirmButtonText': 'Open File',
383+
'confirmButtonText': 'Select Folder',
384+
}),
385+
],
386+
);
387+
});
388+
});
389+
390+
group('#getDirectoryPaths', () {
391+
test('passes initialDirectory correctly', () async {
392+
await plugin.getDirectoryPaths(initialDirectory: '/example/directory');
393+
394+
expect(
395+
log,
396+
<Matcher>[
397+
isMethodCall('getDirectoryPath', arguments: <String, dynamic>{
398+
'initialDirectory': '/example/directory',
399+
'confirmButtonText': null,
400+
'multiple': true,
401+
}),
402+
],
403+
);
404+
});
405+
test('passes confirmButtonText correctly', () async {
406+
await plugin.getDirectoryPaths(
407+
confirmButtonText: 'Select one or mode folders');
408+
409+
expect(
410+
log,
411+
<Matcher>[
412+
isMethodCall('getDirectoryPath', arguments: <String, dynamic>{
413+
'initialDirectory': null,
414+
'confirmButtonText': 'Select one or mode folders',
415+
'multiple': true,
416+
}),
417+
],
418+
);
419+
});
420+
test('passes multiple flag correctly', () async {
421+
await plugin.getDirectoryPaths();
422+
423+
expect(
424+
log,
425+
<Matcher>[
426+
isMethodCall('getDirectoryPath', arguments: <String, dynamic>{
427+
'initialDirectory': null,
428+
'confirmButtonText': null,
429+
'multiple': true,
384430
}),
385431
],
386432
);

0 commit comments

Comments
 (0)