Skip to content

Commit 9af50d4

Browse files
[file_selector] Add file group to save return value - platform interface (#4254)
Platform interface portion of #4222 Part of flutter/flutter#107093
1 parent de1f29e commit 9af50d4

File tree

12 files changed

+143
-11
lines changed

12 files changed

+143
-11
lines changed

packages/file_selector/file_selector/lib/file_selector.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Future<String?> getSavePath({
9898
String? suggestedName,
9999
String? confirmButtonText,
100100
}) async {
101+
// TODO(stuartmorgan): Update this to getSaveLocation in the next federated
102+
// change PR.
103+
// ignore: deprecated_member_use
101104
return FileSelectorPlatform.instance.getSavePath(
102105
acceptedTypeGroups: acceptedTypeGroups,
103106
initialDirectory: initialDirectory,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class SaveTextPage extends StatelessWidget {
1717

1818
Future<void> _saveFile() async {
1919
final String fileName = _nameController.text;
20+
// TODO(stuartmorgan): Update this to getSaveLocation in the next federated
21+
// change PR.
22+
// ignore: deprecated_member_use
2023
final String? path = await FileSelectorPlatform.instance.getSavePath(
2124
// Operation was canceled by the user.
2225
suggestedName: fileName,

packages/file_selector/file_selector_macos/example/lib/save_text_page.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class SaveTextPage extends StatelessWidget {
1717

1818
Future<void> _saveFile() async {
1919
final String fileName = _nameController.text;
20+
// TODO(stuartmorgan): Update this to getSaveLocation in the next federated
21+
// change PR.
22+
// ignore: deprecated_member_use
2023
final String? path = await FileSelectorPlatform.instance.getSavePath(
2124
suggestedName: fileName,
2225
);

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.6.0
2+
3+
* Adds `getSaveLocation` and deprecates `getSavePath`.
4+
15
## 2.5.1
26

37
* Adds compatibility with `http` 1.0.

packages/file_selector/file_selector_platform_interface/lib/src/platform_interface/file_selector_interface.dart

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:async';
6-
75
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
86

97
import '../../file_selector_platform_interface.dart';
@@ -37,7 +35,10 @@ abstract class FileSelectorPlatform extends PlatformInterface {
3735
}
3836

3937
/// Opens a file dialog for loading files and returns a file path.
40-
/// Returns `null` if user cancels the operation.
38+
///
39+
/// Returns `null` if the user cancels the operation.
40+
// TODO(stuartmorgan): Switch to FileDialogOptions if we ever need to
41+
// duplicate this to add a parameter.
4142
Future<XFile?> openFile({
4243
List<XTypeGroup>? acceptedTypeGroups,
4344
String? initialDirectory,
@@ -47,6 +48,10 @@ abstract class FileSelectorPlatform extends PlatformInterface {
4748
}
4849

4950
/// Opens a file dialog for loading files and returns a list of file paths.
51+
///
52+
/// Returns an empty list if the user cancels the operation.
53+
// TODO(stuartmorgan): Switch to FileDialogOptions if we ever need to
54+
// duplicate this to add a parameter.
5055
Future<List<XFile>> openFiles({
5156
List<XTypeGroup>? acceptedTypeGroups,
5257
String? initialDirectory,
@@ -55,8 +60,13 @@ abstract class FileSelectorPlatform extends PlatformInterface {
5560
throw UnimplementedError('openFiles() has not been implemented.');
5661
}
5762

58-
/// Opens a file dialog for saving files and returns a file path at which to save.
59-
/// Returns `null` if user cancels the operation.
63+
/// Opens a file dialog for saving files and returns a file path at which to
64+
/// save.
65+
///
66+
/// Returns `null` if the user cancels the operation.
67+
// TODO(stuartmorgan): Switch to FileDialogOptions if we ever need to
68+
// duplicate this to add a parameter.
69+
@Deprecated('Use getSaveLocation instead')
6070
Future<String?> getSavePath({
6171
List<XTypeGroup>? acceptedTypeGroups,
6272
String? initialDirectory,
@@ -66,16 +76,41 @@ abstract class FileSelectorPlatform extends PlatformInterface {
6676
throw UnimplementedError('getSavePath() has not been implemented.');
6777
}
6878

79+
/// Opens a file dialog for saving files and returns a file location at which
80+
/// to save.
81+
///
82+
/// Returns `null` if the user cancels the operation.
83+
Future<FileSaveLocation?> getSaveLocation({
84+
List<XTypeGroup>? acceptedTypeGroups,
85+
SaveDialogOptions options = const SaveDialogOptions(),
86+
}) async {
87+
final String? path = await getSavePath(
88+
acceptedTypeGroups: acceptedTypeGroups,
89+
initialDirectory: options.initialDirectory,
90+
suggestedName: options.suggestedName,
91+
confirmButtonText: options.confirmButtonText,
92+
);
93+
return path == null ? null : FileSaveLocation(path);
94+
}
95+
6996
/// Opens a file dialog for loading directories and returns a directory path.
70-
/// Returns `null` if user cancels the operation.
97+
///
98+
/// Returns `null` if the user cancels the operation.
99+
// TODO(stuartmorgan): Switch to FileDialogOptions if we ever need to
100+
// duplicate this to add a parameter.
71101
Future<String?> getDirectoryPath({
72102
String? initialDirectory,
73103
String? confirmButtonText,
74104
}) {
75105
throw UnimplementedError('getDirectoryPath() has not been implemented.');
76106
}
77107

78-
/// Opens a file dialog for loading directories and returns multiple directory paths.
108+
/// Opens a file dialog for loading directories and returns multiple directory
109+
/// paths.
110+
///
111+
/// Returns an empty list if the user cancels the operation.
112+
// TODO(stuartmorgan): Switch to FileDialogOptions if we ever need to
113+
// duplicate this to add a parameter.
79114
Future<List<String>> getDirectoryPaths({
80115
String? initialDirectory,
81116
String? confirmButtonText,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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:flutter/foundation.dart' show immutable;
6+
7+
/// Configuration options for any file selector dialog.
8+
@immutable
9+
class FileDialogOptions {
10+
/// Creates a new options set with the given settings.
11+
const FileDialogOptions({this.initialDirectory, this.confirmButtonText});
12+
13+
/// The initial directory the dialog should open with.
14+
final String? initialDirectory;
15+
16+
/// The label for the button that confirms selection.
17+
final String? confirmButtonText;
18+
}
19+
20+
/// Configuration options for a save dialog.
21+
@immutable
22+
class SaveDialogOptions extends FileDialogOptions {
23+
/// Creates a new options set with the given settings.
24+
const SaveDialogOptions(
25+
{super.initialDirectory, super.confirmButtonText, this.suggestedName});
26+
27+
/// The suggested name of the file to save or open.
28+
final String? suggestedName;
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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:flutter/foundation.dart' show immutable;
6+
7+
import 'x_type_group.dart';
8+
9+
export 'x_type_group.dart';
10+
11+
/// The response from a save dialog.
12+
@immutable
13+
class FileSaveLocation {
14+
/// Creates a result with the given [path] and optional other dialog state.
15+
const FileSaveLocation(this.path, {this.activeFilter});
16+
17+
/// The path to save to.
18+
final String path;
19+
20+
/// The currently active filter group, if any.
21+
///
22+
/// This is null on platforms that do not support user-selectable filter
23+
/// groups in save dialogs (for example, macOS), or when no filter groups
24+
/// were provided when showing the dialog.
25+
final XTypeGroup? activeFilter;
26+
}

packages/file_selector/file_selector_platform_interface/lib/src/types/types.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
// found in the LICENSE file.
44

55
export 'package:cross_file/cross_file.dart';
6-
export 'x_type_group/x_type_group.dart';
6+
export 'file_dialog_options.dart';
7+
export 'file_save_location.dart';
8+
export 'x_type_group.dart';

packages/file_selector/file_selector_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/file_selector
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.5.1
7+
version: 2.6.0
88

99
environment:
1010
sdk: ">=2.18.0 <4.0.0"

0 commit comments

Comments
 (0)