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

Commit be428c5

Browse files
committed
Add example for getDirectoriesPaths.
1 parent 09925d9 commit be428c5

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
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 `getDirectoriesPaths`,
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 getDirectoriesPaths(
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
}

0 commit comments

Comments
 (0)