Skip to content

Commit f864bc7

Browse files
TowaYamashitayutaaraki-toydium
authored andcommitted
[file_selector]Improve API docs and examples (flutter#4824)
1 parent 880edfb commit f864bc7

15 files changed

+225
-32
lines changed

packages/file_selector/file_selector/AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ Aleksandr Yurkovskiy <[email protected]>
6363
Anton Borries <[email protected]>
6464
6565
Rahul Raj <[email protected]>
66+
TowaYamashita <[email protected]>

packages/file_selector/file_selector/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 0.8.4+3
22

3+
* Improves API docs and examples.
34
* Minor fixes for new analysis options.
45

56
## 0.8.4+2

packages/file_selector/file_selector/README.md

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# file_selector
22

3+
<?code-excerpt path-base="excerpts/packages/file_selector_example"?>
4+
35
[![pub package](https://img.shields.io/pub/v/file_selector.svg)](https://pub.dartlang.org/packages/file_selector)
46

57
A Flutter plugin that manages files and interactions with file dialogs.
@@ -30,25 +32,48 @@ Here are small examples that show you how to use the API.
3032
Please also take a look at our [example][example] app.
3133

3234
#### Open a single file
35+
<?code-excerpt "open_image_page.dart (SingleOpen)"?>
3336
``` dart
34-
final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 'png']);
35-
final file = await openFile(acceptedTypeGroups: [typeGroup]);
37+
final XTypeGroup typeGroup = XTypeGroup(
38+
label: 'images',
39+
extensions: <String>['jpg', 'png'],
40+
);
41+
final XFile? file =
42+
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
3643
```
3744

3845
#### Open multiple files at once
46+
<?code-excerpt "open_multiple_images_page.dart (MultiOpen)"?>
3947
``` dart
40-
final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 'png']);
41-
final files = await openFiles(acceptedTypeGroups: [typeGroup]);
48+
final XTypeGroup jpgsTypeGroup = XTypeGroup(
49+
label: 'JPEGs',
50+
extensions: <String>['jpg', 'jpeg'],
51+
);
52+
final XTypeGroup pngTypeGroup = XTypeGroup(
53+
label: 'PNGs',
54+
extensions: <String>['png'],
55+
);
56+
final List<XFile> files = await openFiles(acceptedTypeGroups: <XTypeGroup>[
57+
jpgsTypeGroup,
58+
pngTypeGroup,
59+
]);
4260
```
4361

4462
#### Saving a file
63+
<?code-excerpt "readme_standalone_excerpts.dart (Save)"?>
4564
```dart
46-
final path = await getSavePath();
47-
final name = "hello_file_selector.txt";
48-
final data = Uint8List.fromList("Hello World!".codeUnits);
49-
final mimeType = "text/plain";
50-
final file = XFile.fromData(data, name: name, mimeType: mimeType);
51-
await file.saveTo(path);
65+
const String fileName = 'suggested_name.txt';
66+
final String? path = await getSavePath(suggestedName: fileName);
67+
if (path == null) {
68+
// Operation was canceled by the user.
69+
return;
70+
}
71+
72+
final Uint8List fileData = Uint8List.fromList('Hello World!'.codeUnits);
73+
const String mimeType = 'text/plain';
74+
final XFile textFile =
75+
XFile.fromData(fileData, mimeType: mimeType, name: fileName);
76+
await textFile.saveTo(path);
5277
```
5378

5479
[example]:./example
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
targets:
2+
$default:
3+
sources:
4+
include:
5+
- lib/**
6+
# Some default includes that aren't really used here but will prevent
7+
# false-negative warnings:
8+
- $package$
9+
- lib/$lib$
10+
exclude:
11+
- '**/.*/**'
12+
- '**/build/**'
13+
builders:
14+
code_excerpter|code_excerpter:
15+
enabled: true

packages/file_selector/file_selector/example/lib/main.dart

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
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 'package:example/get_directory_page.dart';
6-
import 'package:example/home_page.dart';
7-
import 'package:example/open_image_page.dart';
8-
import 'package:example/open_multiple_images_page.dart';
9-
import 'package:example/open_text_page.dart';
10-
import 'package:example/save_text_page.dart';
115
import 'package:flutter/material.dart';
126

7+
import 'get_directory_page.dart';
8+
import 'home_page.dart';
9+
import 'open_image_page.dart';
10+
import 'open_multiple_images_page.dart';
11+
import 'open_text_page.dart';
12+
import 'save_text_page.dart';
13+
1314
void main() {
1415
runApp(const MyApp());
1516
}

packages/file_selector/file_selector/example/lib/open_image_page.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ class OpenImagePage extends StatelessWidget {
1414
const OpenImagePage({Key? key}) : super(key: key);
1515

1616
Future<void> _openImageFile(BuildContext context) async {
17+
// #docregion SingleOpen
1718
final XTypeGroup typeGroup = XTypeGroup(
1819
label: 'images',
1920
extensions: <String>['jpg', 'png'],
2021
);
21-
final List<XFile> files =
22-
await openFiles(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
23-
if (files.isEmpty) {
22+
final XFile? file =
23+
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
24+
// #enddocregion SingleOpen
25+
if (file == null) {
2426
// Operation was canceled by the user.
2527
return;
2628
}
27-
final XFile file = files[0];
2829
final String fileName = file.name;
2930
final String filePath = file.path;
3031

packages/file_selector/file_selector/example/lib/open_multiple_images_page.dart

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class OpenMultipleImagesPage extends StatelessWidget {
1414
const OpenMultipleImagesPage({Key? key}) : super(key: key);
1515

1616
Future<void> _openImageFile(BuildContext context) async {
17+
// #docregion MultiOpen
1718
final XTypeGroup jpgsTypeGroup = XTypeGroup(
1819
label: 'JPEGs',
1920
extensions: <String>['jpg', 'jpeg'],
@@ -26,6 +27,7 @@ class OpenMultipleImagesPage extends StatelessWidget {
2627
jpgsTypeGroup,
2728
pngTypeGroup,
2829
]);
30+
// #enddocregion MultiOpen
2931
if (files.isEmpty) {
3032
// Operation was canceled by the user.
3133
return;

packages/file_selector/file_selector/example/lib/open_text_page.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:file_selector/file_selector.dart';
66
import 'package:flutter/material.dart';
7+
import 'package:path_provider/path_provider.dart';
78

89
/// Screen that shows an example of openFile
910
class OpenTextPage extends StatelessWidget {
@@ -15,8 +16,15 @@ class OpenTextPage extends StatelessWidget {
1516
label: 'text',
1617
extensions: <String>['txt', 'json'],
1718
);
18-
final XFile? file =
19-
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
19+
// This demonstrates using an initial directory for the prompt, which should
20+
// only be done in cases where the application can likely predict where the
21+
// file would be. In most cases, this parameter should not be provided.
22+
final String initialDirectory =
23+
(await getApplicationDocumentsDirectory()).path;
24+
final XFile? file = await openFile(
25+
acceptedTypeGroups: <XTypeGroup>[typeGroup],
26+
initialDirectory: initialDirectory,
27+
);
2028
if (file == null) {
2129
// Operation was canceled by the user.
2230
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// This file exists solely to host compiled excerpts for README.md, and is not
6+
// intended for use as an actual example application.
7+
8+
// ignore_for_file: public_member_api_docs
9+
10+
// TODO(a14n): remove this import once Flutter 3.1 or later reaches stable (including flutter/flutter#104231)
11+
// ignore: unnecessary_import
12+
import 'dart:typed_data';
13+
14+
import 'package:file_selector/file_selector.dart';
15+
import 'package:flutter/material.dart';
16+
17+
void main() {
18+
runApp(const MyApp());
19+
}
20+
21+
class MyApp extends StatefulWidget {
22+
const MyApp({Key? key}) : super(key: key);
23+
24+
@override
25+
State<MyApp> createState() => _MyAppState();
26+
}
27+
28+
class _MyAppState extends State<MyApp> {
29+
@override
30+
Widget build(BuildContext context) {
31+
return MaterialApp(
32+
home: Scaffold(
33+
appBar: AppBar(
34+
title: const Text('README snippet app'),
35+
),
36+
body: const Text('See example in main.dart'),
37+
),
38+
);
39+
}
40+
41+
Future<void> saveFile() async {
42+
// #docregion Save
43+
const String fileName = 'suggested_name.txt';
44+
final String? path = await getSavePath(suggestedName: fileName);
45+
if (path == null) {
46+
// Operation was canceled by the user.
47+
return;
48+
}
49+
50+
final Uint8List fileData = Uint8List.fromList('Hello World!'.codeUnits);
51+
const String mimeType = 'text/plain';
52+
final XFile textFile =
53+
XFile.fromData(fileData, mimeType: mimeType, name: fileName);
54+
await textFile.saveTo(path);
55+
// #enddocregion Save
56+
}
57+
}

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:typed_data';
66
import 'package:file_selector/file_selector.dart';
77
import 'package:flutter/material.dart';
8+
import 'package:path_provider/path_provider.dart';
89

910
/// Page for showing an example of saving with file_selector
1011
class SaveTextPage extends StatelessWidget {
@@ -15,17 +16,27 @@ class SaveTextPage extends StatelessWidget {
1516
final TextEditingController _contentController = TextEditingController();
1617

1718
Future<void> _saveFile() async {
18-
final String? path = await getSavePath();
19+
final String fileName = _nameController.text;
20+
// This demonstrates using an initial directory for the prompt, which should
21+
// only be done in cases where the application can likely predict where the
22+
// file will be saved. In most cases, this parameter should not be provided.
23+
final String initialDirectory =
24+
(await getApplicationDocumentsDirectory()).path;
25+
final String? path = await getSavePath(
26+
initialDirectory: initialDirectory,
27+
suggestedName: fileName,
28+
);
1929
if (path == null) {
2030
// Operation was canceled by the user.
2131
return;
2232
}
33+
2334
final String text = _contentController.text;
24-
final String fileName = _nameController.text;
2535
final Uint8List fileData = Uint8List.fromList(text.codeUnits);
2636
const String fileMimeType = 'text/plain';
2737
final XFile textFile =
2838
XFile.fromData(fileData, mimeType: fileMimeType, name: fileName);
39+
2940
await textFile.saveTo(path);
3041
}
3142

packages/file_selector/file_selector/example/pubspec.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: example
1+
name: file_selector_example
22
description: A new Flutter project.
33
publish_to: none
44

@@ -17,8 +17,10 @@ dependencies:
1717
path: ../
1818
flutter:
1919
sdk: flutter
20+
path_provider: ^2.0.9
2021

2122
dev_dependencies:
23+
build_runner: ^2.1.10
2224
flutter_test:
2325
sdk: flutter
2426

packages/file_selector/file_selector/example/windows/flutter/generated_plugins.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ list(APPEND FLUTTER_PLUGIN_LIST
66
file_selector_windows
77
)
88

9+
list(APPEND FLUTTER_FFI_PLUGIN_LIST
10+
)
11+
912
set(PLUGIN_BUNDLED_LIBRARIES)
1013

1114
foreach(plugin ${FLUTTER_PLUGIN_LIST})
@@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST})
1417
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
1518
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
1619
endforeach(plugin)
20+
21+
foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
22+
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin})
23+
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
24+
endforeach(ffi_plugin)

0 commit comments

Comments
 (0)