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

Commit 667f525

Browse files
committed
add getDirectoriesPath implementation
add dart and native tests add example
1 parent 3982f4c commit 667f525

File tree

11 files changed

+197
-5
lines changed

11 files changed

+197
-5
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 `getDirectoriesPaths` 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 `getDirectoriesPaths`,
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.getDirectoriesPaths(
18+
confirmButtonText: confirmButtonText,
19+
);
20+
if (directoryPaths == null) {
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.3.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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const String _typeGroupMimeTypesKey = 'mimeTypes';
1616
const String _openFileMethod = 'openFile';
1717
const String _getSavePathMethod = 'getSavePath';
1818
const String _getDirectoryPathMethod = 'getDirectoryPath';
19+
const String _getDirectoriesPathsMethod = 'getDirectoriesPaths';
1920

2021
const String _acceptedTypeGroupsKey = 'acceptedTypeGroups';
2122
const String _confirmButtonTextKey = 'confirmButtonText';
@@ -110,6 +111,19 @@ class FileSelectorLinux extends FileSelectorPlatform {
110111
},
111112
);
112113
}
114+
115+
@override
116+
Future<List<String>?> getDirectoriesPaths({
117+
String? initialDirectory,
118+
String? confirmButtonText,
119+
}) async {
120+
return _channel
121+
.invokeListMethod<String>(_getDirectoriesPathsMethod, <String, dynamic>{
122+
_initialDirectoryKey: initialDirectory,
123+
_confirmButtonTextKey: confirmButtonText,
124+
_multipleKey: true,
125+
});
126+
}
113127
}
114128

115129
List<Map<String, Object>> _serializeTypeGroups(List<XTypeGroup>? groups) {

packages/file_selector/file_selector_linux/linux/file_selector_plugin.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const char kChannelName[] = "plugins.flutter.dev/file_selector_linux";
1515
const char kOpenFileMethod[] = "openFile";
1616
const char kGetSavePathMethod[] = "getSavePath";
1717
const char kGetDirectoryPathMethod[] = "getDirectoryPath";
18+
const char kGetDirectoriesPathsMethod[] = "getDirectoriesPaths";
1819

1920
const char kAcceptedTypeGroupsKey[] = "acceptedTypeGroups";
2021
const char kConfirmButtonTextKey[] = "confirmButtonText";
@@ -131,6 +132,9 @@ GtkFileChooserNative* create_dialog_for_method(GtkWindow* window,
131132
} else if (strcmp(method, kGetDirectoryPathMethod) == 0) {
132133
return create_dialog(window, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
133134
"Choose Directory", "_Open", properties);
135+
} else if (strcmp(method, kGetDirectoriesPathsMethod) == 0) {
136+
return create_dialog(window, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
137+
"Choose one or more Directories", "_Open", properties);
134138
} else if (strcmp(method, kGetSavePathMethod) == 0) {
135139
return create_dialog(window, GTK_FILE_CHOOSER_ACTION_SAVE, "Save File",
136140
"_Save", properties);
@@ -192,7 +196,8 @@ static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
192196
FlValue* args = fl_method_call_get_args(method_call);
193197

194198
g_autoptr(FlMethodResponse) response = nullptr;
195-
if (strcmp(method, kOpenFileMethod) == 0) {
199+
if (strcmp(method, kOpenFileMethod) == 0 ||
200+
strcmp(method, kGetDirectoriesPathsMethod) == 0) {
196201
response = show_dialog(self, method, args, true);
197202
} else if (strcmp(method, kGetDirectoryPathMethod) == 0 ||
198203
strcmp(method, kGetSavePathMethod) == 0) {

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, "getDirectoriesPaths", 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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ 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
6+
7+
# TODO(adpinola): remove this once file_selector_platform_interface version is updated to 2.3.0
8+
# with getDirectoriesPaths method.
9+
publish_to: none
610

711
environment:
812
sdk: ">=2.12.0 <3.0.0"
@@ -18,7 +22,9 @@ flutter:
1822

1923
dependencies:
2024
cross_file: ^0.3.1
21-
file_selector_platform_interface: ^2.2.0
25+
# TODO(adpinola): This should be 2.3.0 once it is published.
26+
file_selector_platform_interface:
27+
path: ../file_selector_platform_interface
2228
flutter:
2329
sdk: flutter
2430

packages/file_selector/file_selector_linux/test/file_selector_linux_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,49 @@ void main() {
386386
);
387387
});
388388
});
389+
390+
group('#getDirectoriesPaths', () {
391+
test('passes initialDirectory correctly', () async {
392+
await plugin.getDirectoriesPaths(initialDirectory: '/example/directory');
393+
394+
expect(
395+
log,
396+
<Matcher>[
397+
isMethodCall('getDirectoriesPaths', 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.getDirectoriesPaths(confirmButtonText: 'Open File');
407+
408+
expect(
409+
log,
410+
<Matcher>[
411+
isMethodCall('getDirectoriesPaths', arguments: <String, dynamic>{
412+
'initialDirectory': null,
413+
'confirmButtonText': 'Open File',
414+
'multiple': true,
415+
}),
416+
],
417+
);
418+
});
419+
test('passes multiple flag correctly', () async {
420+
await plugin.getDirectoriesPaths();
421+
422+
expect(
423+
log,
424+
<Matcher>[
425+
isMethodCall('getDirectoriesPaths', arguments: <String, dynamic>{
426+
'initialDirectory': null,
427+
'confirmButtonText': null,
428+
'multiple': true,
429+
}),
430+
],
431+
);
432+
});
433+
});
389434
}

0 commit comments

Comments
 (0)