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

Commit db4d20c

Browse files
[file_selector] add getDirectoryPaths implementation on Linux (#6573)
* Add getDirectoriesPaths method to the file_selector_platform_interface Add getDirectoriesPaths to method channel. Increment version to 2.3.0 apply feedback extract assertion method * add getDirectoryPaths Linux implementation * apply rebase * update version to 0.9.1 Co-authored-by: eugerossetto <[email protected]>
1 parent d610a21 commit db4d20c

File tree

11 files changed

+343
-179
lines changed

11 files changed

+343
-179
lines changed

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.1
2+
3+
* Adds `getDirectoryPaths` implementation.
4+
15
## 0.9.0+1
26

37
* Changes XTypeGroup initialization from final to const.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
await showDialog<void>(
25+
context: context,
26+
builder: (BuildContext context) => TextDisplay(directoryPaths.join('\n')),
27+
);
28+
}
29+
30+
@override
31+
Widget build(BuildContext context) {
32+
return Scaffold(
33+
appBar: AppBar(
34+
title: const Text('Select multiple directories'),
35+
),
36+
body: Center(
37+
child: Column(
38+
mainAxisAlignment: MainAxisAlignment.center,
39+
children: <Widget>[
40+
ElevatedButton(
41+
style: ElevatedButton.styleFrom(
42+
// TODO(darrenaustin): Migrate to new API once it lands in stable: https://github.com/flutter/flutter/issues/105724
43+
// ignore: deprecated_member_use
44+
primary: Colors.blue,
45+
// ignore: deprecated_member_use
46+
onPrimary: Colors.white,
47+
),
48+
child: const Text(
49+
'Press to ask user to choose multiple directories'),
50+
onPressed: () => _getDirectoryPaths(context),
51+
),
52+
],
53+
),
54+
),
55+
);
56+
}
57+
}
58+
59+
/// Widget that displays a text file in a dialog.
60+
class TextDisplay extends StatelessWidget {
61+
/// Creates a `TextDisplay`.
62+
const TextDisplay(this.directoriesPaths, {Key? key}) : super(key: key);
63+
64+
/// The path selected in the dialog.
65+
final String directoriesPaths;
66+
67+
@override
68+
Widget build(BuildContext context) {
69+
return AlertDialog(
70+
title: const Text('Selected Directories'),
71+
content: Scrollbar(
72+
child: SingleChildScrollView(
73+
child: Text(directoriesPaths),
74+
),
75+
),
76+
actions: <Widget>[
77+
TextButton(
78+
child: const Text('Close'),
79+
onPressed: () => Navigator.pop(context),
80+
),
81+
],
82+
);
83+
}
84+
}

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 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: 2 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,7 @@ environment:
99
dependencies:
1010
file_selector_linux:
1111
path: ../
12-
file_selector_platform_interface: ^2.2.0
12+
file_selector_platform_interface: ^2.4.0
1313
flutter:
1414
sdk: flutter
1515

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CMakeCache.txt
2+
CMakeFiles/

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: 2 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.1
66

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

1919
dependencies:
2020
cross_file: ^0.3.1
21-
file_selector_platform_interface: ^2.2.0
21+
file_selector_platform_interface: ^2.4.0
2222
flutter:
2323
sdk: flutter
2424

0 commit comments

Comments
 (0)