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

Commit 57561d1

Browse files
eugerossettoadpinola
authored andcommitted
Add getDirectoryPaths to main example
1 parent 5c11747 commit 57561d1

File tree

22 files changed

+300
-18
lines changed

22 files changed

+300
-18
lines changed

packages/file_selector/file_selector/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 0.9.2+2
22

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

packages/file_selector/file_selector/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/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) => GetDirectoryPage(),
40+
'/multi-directories': (BuildContext context) =>
41+
const GetMultipleDirectoriesPage()
3942
},
4043
);
4144
}

packages/file_selector/file_selector/lib/file_selector.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,22 @@ Future<String?> getDirectoryPath({
123123
return FileSelectorPlatform.instance.getDirectoryPath(
124124
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
125125
}
126+
127+
/// Opens a directory selection dialog and returns a list of the paths chosen by the user.
128+
/// This always returns `null` on the web.
129+
///
130+
/// [initialDirectory] is the full path to the directory that will be displayed
131+
/// when the dialog is opened. When not provided, the platform will pick an
132+
/// initial location.
133+
///
134+
/// [confirmButtonText] is the text in the confirmation button of the dialog.
135+
/// When not provided, the default OS label is used (for example, "Open").
136+
///
137+
/// Returns `null` if the user cancels the operation.
138+
Future<List<String?>?> getDirectoryPaths({
139+
String? initialDirectory,
140+
String? confirmButtonText,
141+
}) async {
142+
return FileSelectorPlatform.instance.getDirectoryPaths(
143+
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
144+
}

packages/file_selector/file_selector/pubspec.yaml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ description: Flutter plugin for opening and saving files, or selecting
44
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
66
version: 0.9.2+2
7+
# TODO(eugerossetto): This should be reverted once file_selector_platform_interface 2.3.0 is published.
8+
publish_to: 'none'
79

810
environment:
911
sdk: ">=2.12.0 <3.0.0"
@@ -23,13 +25,20 @@ flutter:
2325
windows:
2426
default_package: file_selector_windows
2527

28+
# TODO(eugerossetto): This should be reverted once file_selector_platform_interface 2.3.0 is published.
2629
dependencies:
27-
file_selector_ios: ^0.5.0
28-
file_selector_linux: ^0.9.0
29-
file_selector_macos: ^0.9.0
30-
file_selector_platform_interface: ^2.2.0
31-
file_selector_web: ^0.9.0
32-
file_selector_windows: ^0.9.0
30+
file_selector_ios:
31+
path: ../file_selector_ios
32+
file_selector_linux:
33+
path: ../file_selector_linux
34+
file_selector_macos:
35+
path: ../file_selector_macos
36+
file_selector_platform_interface:
37+
path: ../file_selector_platform_interface
38+
file_selector_web:
39+
path: ../file_selector_web
40+
file_selector_windows:
41+
path: ../file_selector_windows
3342
flutter:
3443
sdk: flutter
3544

packages/file_selector/file_selector/test/file_selector_test.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,55 @@ void main() {
258258
expect(directoryPath, expectedDirectoryPath);
259259
});
260260
});
261+
262+
group('getDirectoryPaths', () {
263+
const List<String> expectedDirectoryPaths = <String>[
264+
'/example/path',
265+
'/example/2/path'
266+
];
267+
268+
test('works', () async {
269+
fakePlatformImplementation
270+
..setExpectations(
271+
initialDirectory: initialDirectory,
272+
confirmButtonText: confirmButtonText)
273+
..setPathsResponse(expectedDirectoryPaths);
274+
275+
final List<String?>? directoryPaths = await getDirectoryPaths(
276+
initialDirectory: initialDirectory,
277+
confirmButtonText: confirmButtonText,
278+
);
279+
280+
expect(directoryPaths, expectedDirectoryPaths);
281+
});
282+
283+
test('works with no arguments', () async {
284+
fakePlatformImplementation.setPathsResponse(expectedDirectoryPaths);
285+
286+
final List<String?>? directoryPaths = await getDirectoryPaths();
287+
expect(directoryPaths, expectedDirectoryPaths);
288+
});
289+
290+
test('sets the initial directory', () async {
291+
fakePlatformImplementation
292+
..setExpectations(initialDirectory: initialDirectory)
293+
..setPathsResponse(expectedDirectoryPaths);
294+
295+
final List<String?>? directoryPaths =
296+
await getDirectoryPaths(initialDirectory: initialDirectory);
297+
expect(directoryPaths, expectedDirectoryPaths);
298+
});
299+
300+
test('sets the button confirmation label', () async {
301+
fakePlatformImplementation
302+
..setExpectations(confirmButtonText: confirmButtonText)
303+
..setPathsResponse(expectedDirectoryPaths);
304+
305+
final List<String?>? directoryPaths =
306+
await getDirectoryPaths(confirmButtonText: confirmButtonText);
307+
expect(directoryPaths, expectedDirectoryPaths);
308+
});
309+
});
261310
}
262311

263312
class FakeFileSelector extends Fake
@@ -271,6 +320,7 @@ class FakeFileSelector extends Fake
271320
// Return values.
272321
List<XFile>? files;
273322
String? path;
323+
List<String>? paths;
274324

275325
void setExpectations({
276326
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
@@ -294,6 +344,11 @@ class FakeFileSelector extends Fake
294344
this.path = path;
295345
}
296346

347+
// ignore: use_setters_to_change_properties
348+
void setPathsResponse(List<String> paths) {
349+
this.paths = paths;
350+
}
351+
297352
@override
298353
Future<XFile?> openFile({
299354
List<XTypeGroup>? acceptedTypeGroups,
@@ -341,4 +396,14 @@ class FakeFileSelector extends Fake
341396
expect(confirmButtonText, this.confirmButtonText);
342397
return path;
343398
}
399+
400+
@override
401+
Future<List<String>?> getDirectoryPaths({
402+
String? initialDirectory,
403+
String? confirmButtonText,
404+
}) async {
405+
expect(initialDirectory, this.initialDirectory);
406+
expect(confirmButtonText, this.confirmButtonText);
407+
return paths;
408+
}
344409
}

packages/file_selector/file_selector_ios/example/pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ dependencies:
1717
# The example app is bundled with the plugin so we use a path dependency on
1818
# the parent directory to use the current plugin's version.
1919
path: ..
20-
file_selector_platform_interface: ^2.2.0
20+
# TODO(eugerossetto): This should be 2.4.0 once it is published.
21+
file_selector_platform_interface:
22+
path: ../../file_selector_platform_interface
2123
flutter:
2224
sdk: flutter
2325

packages/file_selector/file_selector_ios/pubspec.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ description: iOS implementation of the file_selector plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector_ios
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
55
version: 0.5.0+2
6+
# TODO(eugerossetto): remove this once file_selector_platform_interface version is updated to 2.4.0
7+
publish_to: 'none'
68

79
environment:
810
sdk: ">=2.14.4 <3.0.0"
@@ -17,7 +19,9 @@ flutter:
1719
pluginClass: FFSFileSelectorPlugin
1820

1921
dependencies:
20-
file_selector_platform_interface: ^2.2.0
22+
# TODO(eugerossetto): This should be 2.4.0 once it is published.
23+
file_selector_platform_interface:
24+
path: ../file_selector_platform_interface
2125
flutter:
2226
sdk: flutter
2327

@@ -27,4 +31,4 @@ dev_dependencies:
2731
sdk: flutter
2832
mockito: ^5.1.0
2933
pigeon: ^3.2.5
30-
34+

packages/file_selector/file_selector_linux/example/pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ environment:
99
dependencies:
1010
file_selector_linux:
1111
path: ../
12-
file_selector_platform_interface: ^2.2.0
12+
# TODO(eugerossetto): 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/pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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
55
version: 0.9.0+1
6+
# TODO(eugerossetto): remove this once file_selector_platform_interface version is updated to 2.4.0
7+
publish_to: 'none'
68

79
environment:
810
sdk: ">=2.12.0 <3.0.0"
@@ -18,7 +20,9 @@ flutter:
1820

1921
dependencies:
2022
cross_file: ^0.3.1
21-
file_selector_platform_interface: ^2.2.0
23+
# TODO(eugerossetto): This should be 2.4.0 once it is published.
24+
file_selector_platform_interface:
25+
path: ../file_selector_platform_interface
2226
flutter:
2327
sdk: flutter
2428

packages/file_selector/file_selector_macos/example/pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ dependencies:
1515
# The example app is bundled with the plugin so we use a path dependency on
1616
# the parent directory to use the current plugin's version.
1717
path: ..
18-
file_selector_platform_interface: ^2.2.0
18+
# TODO(eugerossetto): This should be 2.4.0 once it is published.
19+
file_selector_platform_interface:
20+
path: ../../file_selector_platform_interface
1921
flutter:
2022
sdk: flutter
2123

packages/file_selector/file_selector_macos/pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ description: macOS implementation of the file_selector plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector_macos
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
55
version: 0.9.0+3
6+
# TODO(eugerossetto): remove this once file_selector_platform_interface version is updated to 2.4.0
7+
publish_to: 'none'
68

79
environment:
810
sdk: ">=2.12.0 <3.0.0"
@@ -18,7 +20,9 @@ flutter:
1820

1921
dependencies:
2022
cross_file: ^0.3.1
21-
file_selector_platform_interface: ^2.2.0
23+
# TODO(eugerossetto): This should be 2.4.0 once it is published.
24+
file_selector_platform_interface:
25+
path: ../file_selector_platform_interface
2226
flutter:
2327
sdk: flutter
2428

packages/file_selector/file_selector_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.0
2+
3+
* Adds `getDirectoryPaths` method to the interface.
4+
15
## 2.3.0
26

37
* Replaces `macUTIs` with `uniformTypeIdentifiers`. `macUTIs` is available as an alias, but will be deprecated in a future release.

0 commit comments

Comments
 (0)