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

[file_selector] add getDirectoryPaths implementation on Linux #6573

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_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.1

* Adds `getDirectoryPaths` implementation.

## 0.9.0+1

* Changes XTypeGroup initialization from final to const.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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_platform_interface/file_selector_platform_interface.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 {
/// Default Constructor
const GetMultipleDirectoriesPage({Key? key}) : super(key: key);

Future<void> _getDirectoryPaths(BuildContext context) async {
const String confirmButtonText = 'Choose';
final List<String> directoryPaths =
await FileSelectorPlatform.instance.getDirectoryPaths(
confirmButtonText: confirmButtonText,
);
if (directoryPaths.isEmpty) {
// Operation was canceled by the user.
return;
}
await showDialog<void>(
context: context,
builder: (BuildContext context) => TextDisplay(directoryPaths.join('\n')),
);
}

@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, {Key? key}) : super(key: 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 directories dialog'),
onPressed: () =>
Navigator.pushNamed(context, '/multi-directories'),
),
],
),
),
Expand Down
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) => const GetDirectoryPage(),
'/multi-directories': (BuildContext context) =>
const GetMultipleDirectoriesPage()
},
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: file_selector_linux_example
description: Local testbed for Linux file_selector implementation.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: 'none'
version: 1.0.0+1

environment:
Expand All @@ -9,7 +9,7 @@ environment:
dependencies:
file_selector_linux:
path: ../
file_selector_platform_interface: ^2.2.0
file_selector_platform_interface: ^2.4.0
flutter:
sdk: flutter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,26 @@ class FileSelectorLinux extends FileSelectorPlatform {
String? initialDirectory,
String? confirmButtonText,
}) async {
return _channel.invokeMethod<String>(
_getDirectoryPathMethod,
<String, dynamic>{
_initialDirectoryKey: initialDirectory,
_confirmButtonTextKey: confirmButtonText,
},
);
final List<String>? path = await _channel
.invokeListMethod<String>(_getDirectoryPathMethod, <String, dynamic>{
_initialDirectoryKey: initialDirectory,
_confirmButtonTextKey: confirmButtonText,
});
return path?.first;
}

@override
Future<List<String>> getDirectoryPaths({
String? initialDirectory,
String? confirmButtonText,
}) async {
final List<String>? pathList = await _channel
.invokeListMethod<String>(_getDirectoryPathMethod, <String, dynamic>{
_initialDirectoryKey: initialDirectory,
_confirmButtonTextKey: confirmButtonText,
_multipleKey: true,
});
return pathList ?? <String>[];
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/file_selector/file_selector_linux/linux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CMakeCache.txt
CMakeFiles/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely pedantic nitpick: add a trailing newline. :)

Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
FlValue* args = fl_method_call_get_args(method_call);

g_autoptr(FlMethodResponse) response = nullptr;
if (strcmp(method, kOpenFileMethod) == 0) {
if (strcmp(method, kOpenFileMethod) == 0 ||
strcmp(method, kGetDirectoryPathMethod) == 0) {
response = show_dialog(self, method, args, true);
} else if (strcmp(method, kGetDirectoryPathMethod) == 0 ||
strcmp(method, kGetSavePathMethod) == 0) {
} else if (strcmp(method, kGetSavePathMethod) == 0) {
response = show_dialog(self, method, args, false);
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,17 @@ TEST(FileSelectorPlugin, TestGetDirectory) {
EXPECT_EQ(gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(dialog)),
false);
}

TEST(FileSelectorPlugin, TestGetMultipleDirectories) {
g_autoptr(FlValue) args = fl_value_new_map();
fl_value_set_string_take(args, "multiple", fl_value_new_bool(true));

g_autoptr(GtkFileChooserNative) dialog =
create_dialog_for_method(nullptr, "getDirectoryPath", args);

ASSERT_NE(dialog, nullptr);
EXPECT_EQ(gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)),
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
EXPECT_EQ(gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(dialog)),
true);
}
4 changes: 2 additions & 2 deletions packages/file_selector/file_selector_linux/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: file_selector_linux
description: Liunx implementation of the file_selector plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector_linux
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.0+1
version: 0.9.1

environment:
sdk: ">=2.12.0 <3.0.0"
Expand All @@ -18,7 +18,7 @@ flutter:

dependencies:
cross_file: ^0.3.1
file_selector_platform_interface: ^2.2.0
file_selector_platform_interface: ^2.4.0
flutter:
sdk: flutter

Expand Down
Loading