|
| 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 | +} |
0 commit comments