Skip to content

[file_selector] Add getDirectoryPaths #3871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.3

* Adds `getDirectoryPaths` for selecting multiple directories.

## 0.9.2+5

* Updates references to the deprecated `macUTIs`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2013 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:file_selector/file_selector.dart';
import 'package:flutter/material.dart';

/// Screen that allows the user to select one or more directories using `getDirectoryPaths`,
/// then displays the selected directories in a dialog.
class GetMultipleDirectoriesPage extends StatelessWidget {
/// Returns a new instance of the page.
const GetMultipleDirectoriesPage({super.key});

Future<void> _getDirectoryPaths(BuildContext context) async {
const String confirmButtonText = 'Choose';
final List<String?> directoryPaths = await getDirectoryPaths(
confirmButtonText: confirmButtonText,
);
if (directoryPaths.isEmpty) {
// Operation was canceled by the user.
return;
}
String paths = '';
for (final String? path in directoryPaths) {
paths += '${path!} \n';
}
if (context.mounted) {
await showDialog<void>(
context: context,
builder: (BuildContext context) => TextDisplay(paths),
);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Select multiple directories'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
// TODO(darrenaustin): Migrate to new API once it lands in stable: https://github.com/flutter/flutter/issues/105724
// ignore: deprecated_member_use
primary: Colors.blue,
// ignore: deprecated_member_use
onPrimary: Colors.white,
),
child: const Text(
'Press to ask user to choose multiple directories'),
onPressed: () => _getDirectoryPaths(context),
),
],
),
),
);
}
}

/// Widget that displays a text file in a dialog.
class TextDisplay extends StatelessWidget {
/// Creates a `TextDisplay`.
const TextDisplay(this.directoriesPaths, {super.key});

/// The path selected in the dialog.
final String directoriesPaths;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Selected Directories'),
content: Scrollbar(
child: SingleChildScrollView(
child: Text(directoriesPaths),
),
),
actions: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () => Navigator.pop(context),
),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class HomePage extends StatelessWidget {
child: const Text('Open a get directory dialog'),
onPressed: () => Navigator.pushNamed(context, '/directory'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open a get multi directories dialog'),
onPressed: () =>
Navigator.pushNamed(context, '/multi-directories'),
),
],
),
),
Expand Down
3 changes: 3 additions & 0 deletions packages/file_selector/file_selector/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/material.dart';

import 'get_directory_page.dart';
import 'get_multiple_directories_page.dart';
import 'home_page.dart';
import 'open_image_page.dart';
import 'open_multiple_images_page.dart';
Expand Down Expand Up @@ -36,6 +37,8 @@ class MyApp extends StatelessWidget {
'/open/text': (BuildContext context) => const OpenTextPage(),
'/save/text': (BuildContext context) => SaveTextPage(),
'/directory': (BuildContext context) => GetDirectoryPage(),
'/multi-directories': (BuildContext context) =>
const GetMultipleDirectoriesPage()
},
);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/file_selector/file_selector/lib/file_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Future<String?> getSavePath({
}

/// Opens a directory selection dialog and returns the path chosen by the user.
///
/// This always returns `null` on the web.
///
/// [initialDirectory] is the full path to the directory that will be displayed
Expand All @@ -123,3 +124,24 @@ Future<String?> getDirectoryPath({
return FileSelectorPlatform.instance.getDirectoryPath(
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
}

/// Opens a directory selection dialog and returns a list of the paths chosen
/// by the user.
///
/// This always returns an empty array on the web.
///
/// [initialDirectory] is the full path to the directory that will be displayed
/// when the dialog is opened. When not provided, the platform will pick an
/// initial location.
///
/// [confirmButtonText] is the text in the confirmation button of the dialog.
/// When not provided, the default OS label is used (for example, "Open").
///
/// Returns an empty array if the user cancels the operation.
Future<List<String?>> getDirectoryPaths({
String? initialDirectory,
String? confirmButtonText,
}) async {
return FileSelectorPlatform.instance.getDirectoryPaths(
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
}
12 changes: 6 additions & 6 deletions packages/file_selector/file_selector/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ description: Flutter plugin for opening and saving files, or selecting
directories, using native file selection UI.
repository: https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.2+5
version: 0.9.3

environment:
sdk: ">=2.17.0 <4.0.0"
flutter: ">=3.0.0"
sdk: ">=2.18.0 <4.0.0"
flutter: ">=3.3.0"

flutter:
plugin:
Expand All @@ -25,11 +25,11 @@ flutter:

dependencies:
file_selector_ios: ^0.5.0
file_selector_linux: ^0.9.0
file_selector_macos: ^0.9.0
file_selector_linux: ^0.9.1
file_selector_macos: ^0.9.1
file_selector_platform_interface: ^2.3.0
file_selector_web: ^0.9.0
file_selector_windows: ^0.9.0
file_selector_windows: ^0.9.2
flutter:
sdk: flutter

Expand Down
90 changes: 75 additions & 15 deletions packages/file_selector/file_selector/test/file_selector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void main() {
confirmButtonText: confirmButtonText,
acceptedTypeGroups: acceptedTypeGroups,
suggestedName: suggestedName)
..setPathResponse(expectedSavePath);
..setPathsResponse(<String>[expectedSavePath]);

final String? savePath = await getSavePath(
initialDirectory: initialDirectory,
Expand All @@ -167,7 +167,7 @@ void main() {
});

test('works with no arguments', () async {
fakePlatformImplementation.setPathResponse(expectedSavePath);
fakePlatformImplementation.setPathsResponse(<String>[expectedSavePath]);

final String? savePath = await getSavePath();
expect(savePath, expectedSavePath);
Expand All @@ -176,7 +176,7 @@ void main() {
test('sets the initial directory', () async {
fakePlatformImplementation
..setExpectations(initialDirectory: initialDirectory)
..setPathResponse(expectedSavePath);
..setPathsResponse(<String>[expectedSavePath]);

final String? savePath =
await getSavePath(initialDirectory: initialDirectory);
Expand All @@ -186,7 +186,7 @@ void main() {
test('sets the button confirmation label', () async {
fakePlatformImplementation
..setExpectations(confirmButtonText: confirmButtonText)
..setPathResponse(expectedSavePath);
..setPathsResponse(<String>[expectedSavePath]);

final String? savePath =
await getSavePath(confirmButtonText: confirmButtonText);
Expand All @@ -196,7 +196,7 @@ void main() {
test('sets the accepted type groups', () async {
fakePlatformImplementation
..setExpectations(acceptedTypeGroups: acceptedTypeGroups)
..setPathResponse(expectedSavePath);
..setPathsResponse(<String>[expectedSavePath]);

final String? savePath =
await getSavePath(acceptedTypeGroups: acceptedTypeGroups);
Expand All @@ -206,7 +206,7 @@ void main() {
test('sets the suggested name', () async {
fakePlatformImplementation
..setExpectations(suggestedName: suggestedName)
..setPathResponse(expectedSavePath);
..setPathsResponse(<String>[expectedSavePath]);

final String? savePath = await getSavePath(suggestedName: suggestedName);
expect(savePath, expectedSavePath);
Expand All @@ -221,7 +221,7 @@ void main() {
..setExpectations(
initialDirectory: initialDirectory,
confirmButtonText: confirmButtonText)
..setPathResponse(expectedDirectoryPath);
..setPathsResponse(<String>[expectedDirectoryPath]);

final String? directoryPath = await getDirectoryPath(
initialDirectory: initialDirectory,
Expand All @@ -232,7 +232,8 @@ void main() {
});

test('works with no arguments', () async {
fakePlatformImplementation.setPathResponse(expectedDirectoryPath);
fakePlatformImplementation
.setPathsResponse(<String>[expectedDirectoryPath]);

final String? directoryPath = await getDirectoryPath();
expect(directoryPath, expectedDirectoryPath);
Expand All @@ -241,7 +242,7 @@ void main() {
test('sets the initial directory', () async {
fakePlatformImplementation
..setExpectations(initialDirectory: initialDirectory)
..setPathResponse(expectedDirectoryPath);
..setPathsResponse(<String>[expectedDirectoryPath]);

final String? directoryPath =
await getDirectoryPath(initialDirectory: initialDirectory);
Expand All @@ -251,13 +252,62 @@ void main() {
test('sets the button confirmation label', () async {
fakePlatformImplementation
..setExpectations(confirmButtonText: confirmButtonText)
..setPathResponse(expectedDirectoryPath);
..setPathsResponse(<String>[expectedDirectoryPath]);

final String? directoryPath =
await getDirectoryPath(confirmButtonText: confirmButtonText);
expect(directoryPath, expectedDirectoryPath);
});
});

group('getDirectoryPaths', () {
const List<String> expectedDirectoryPaths = <String>[
'/example/path',
'/example/2/path'
];

test('works', () async {
fakePlatformImplementation
..setExpectations(
initialDirectory: initialDirectory,
confirmButtonText: confirmButtonText)
..setPathsResponse(expectedDirectoryPaths);

final List<String?> directoryPaths = await getDirectoryPaths(
initialDirectory: initialDirectory,
confirmButtonText: confirmButtonText,
);

expect(directoryPaths, expectedDirectoryPaths);
});

test('works with no arguments', () async {
fakePlatformImplementation.setPathsResponse(expectedDirectoryPaths);

final List<String?> directoryPaths = await getDirectoryPaths();
expect(directoryPaths, expectedDirectoryPaths);
});

test('sets the initial directory', () async {
fakePlatformImplementation
..setExpectations(initialDirectory: initialDirectory)
..setPathsResponse(expectedDirectoryPaths);

final List<String?> directoryPaths =
await getDirectoryPaths(initialDirectory: initialDirectory);
expect(directoryPaths, expectedDirectoryPaths);
});

test('sets the button confirmation label', () async {
fakePlatformImplementation
..setExpectations(confirmButtonText: confirmButtonText)
..setPathsResponse(expectedDirectoryPaths);

final List<String?> directoryPaths =
await getDirectoryPaths(confirmButtonText: confirmButtonText);
expect(directoryPaths, expectedDirectoryPaths);
});
});
}

class FakeFileSelector extends Fake
Expand All @@ -270,7 +320,7 @@ class FakeFileSelector extends Fake
String? suggestedName;
// Return values.
List<XFile>? files;
String? path;
List<String>? paths;

void setExpectations({
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
Expand All @@ -290,8 +340,8 @@ class FakeFileSelector extends Fake
}

// ignore: use_setters_to_change_properties
void setPathResponse(String path) {
this.path = path;
void setPathsResponse(List<String> paths) {
this.paths = paths;
}

@override
Expand Down Expand Up @@ -329,7 +379,7 @@ class FakeFileSelector extends Fake
expect(initialDirectory, this.initialDirectory);
expect(suggestedName, this.suggestedName);
expect(confirmButtonText, this.confirmButtonText);
return path;
return paths?[0];
}

@override
Expand All @@ -339,6 +389,16 @@ class FakeFileSelector extends Fake
}) async {
expect(initialDirectory, this.initialDirectory);
expect(confirmButtonText, this.confirmButtonText);
return path;
return paths?[0];
}

@override
Future<List<String>> getDirectoryPaths({
String? initialDirectory,
String? confirmButtonText,
}) async {
expect(initialDirectory, this.initialDirectory);
expect(confirmButtonText, this.confirmButtonText);
return paths!;
}
}