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

[file_selector]Improve API docs and examples #4824

Merged
merged 27 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6c71647
Add detail comment in file_selector
TowaYamashita Feb 13, 2022
b021530
change sample code for file_selector
TowaYamashita Feb 13, 2022
a6cc58f
Update CHAGELOG.md
TowaYamashita Feb 13, 2022
08a3dea
Fix CHANGELOG version
TowaYamashita Feb 13, 2022
2b391d6
Remove const from MyApp() in widget test
TowaYamashita Feb 13, 2022
fe10180
Sort directive sections alphabetically
TowaYamashita Feb 13, 2022
09d9bf8
Update Authors
TowaYamashita Feb 13, 2022
a3a820d
Fix my mailaddress
TowaYamashita Feb 13, 2022
422b133
remove accidentally created test
TowaYamashita Feb 17, 2022
63bbfe8
fix comment of function in file_selector.dart
TowaYamashita Mar 27, 2022
55bd1a5
install path_provider
TowaYamashita Mar 27, 2022
d51b01c
change initial directory to useful example
TowaYamashita Mar 27, 2022
cbed80f
fix not to regress web
TowaYamashita Mar 27, 2022
d9dc0bc
fix typo
TowaYamashita Mar 27, 2022
229657d
fix button labels not to change in example
TowaYamashita Mar 27, 2022
d190b51
fix CHANGELOG.md to follow CHANGELOG style
TowaYamashita Mar 27, 2022
c31eea7
Merge branch 'main' into towayamashita-patch-1
TowaYamashita Mar 27, 2022
750f8b7
Merge branch 'main' into towayamashita-patch-1
stuartmorgan Jun 7, 2022
ef47a7a
Remove some use of initialDirectory, and add comments on the others
stuartmorgan Jun 7, 2022
e0eac1a
Clean up comments
stuartmorgan Jun 7, 2022
bbe266d
Missing newline
stuartmorgan Jun 7, 2022
bd4e16b
Accidentally missed changes
stuartmorgan Jun 7, 2022
fda0ed5
Fix bug in singe-image example
stuartmorgan Jun 7, 2022
e982e74
Convert to code-excerpt
stuartmorgan Jun 7, 2022
28fe8a4
Apply suggestions from code review
stuartmorgan Jun 9, 2022
93a87a5
Merge branch 'main' into towayamashita-patch-1
stuartmorgan Jun 9, 2022
73d596e
Update save example
stuartmorgan Jun 9, 2022
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
1 change: 1 addition & 0 deletions packages/file_selector/file_selector/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ Aleksandr Yurkovskiy <[email protected]>
Anton Borries <[email protected]>
Alex Li <[email protected]>
Rahul Raj <[email protected]>
TowaYamashita <[email protected]>
3 changes: 2 additions & 1 deletion packages/file_selector/file_selector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.8.4+3

* Improves API docs and examples.
* Minor fixes for new analysis options.

## 0.8.4+2
Expand Down
47 changes: 37 additions & 10 deletions packages/file_selector/file_selector/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# file_selector

<?code-excerpt path-base="excerpts/packages/file_selector_example"?>
stuartmorgan marked this conversation as resolved.
Show resolved Hide resolved

[![pub package](https://img.shields.io/pub/v/file_selector.svg)](https://pub.dartlang.org/packages/file_selector)

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

#### Open a single file
<?code-excerpt "open_image_page.dart (SingleOpen)"?>
``` dart
final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 'png']);
final file = await openFile(acceptedTypeGroups: [typeGroup]);
final XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: <String>['jpg', 'png'],
);
final XFile? file =
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
```

#### Open multiple files at once
<?code-excerpt "open_multiple_images_page.dart (MultiOpen)"?>
``` dart
final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 'png']);
final files = await openFiles(acceptedTypeGroups: [typeGroup]);
final XTypeGroup jpgsTypeGroup = XTypeGroup(
label: 'JPEGs',
extensions: <String>['jpg', 'jpeg'],
);
final XTypeGroup pngTypeGroup = XTypeGroup(
label: 'PNGs',
extensions: <String>['png'],
);
final List<XFile> files = await openFiles(acceptedTypeGroups: <XTypeGroup>[
jpgsTypeGroup,
pngTypeGroup,
]);
```

#### Saving a file
<?code-excerpt "readme_standalone_excerpts.dart (Save)"?>
```dart
final path = await getSavePath();
final name = "hello_file_selector.txt";
final data = Uint8List.fromList("Hello World!".codeUnits);
final mimeType = "text/plain";
final file = XFile.fromData(data, name: name, mimeType: mimeType);
await file.saveTo(path);
import 'dart:typed_data';
// ···
stuartmorgan marked this conversation as resolved.
Show resolved Hide resolved
const String fileName = 'suggested_name.txt';
final String? path = await getSavePath(suggestedName: fileName);
if (path == null) {
// Operation was canceled by the user.
return;
}

final Uint8List fileData = Uint8List.fromList('Hello World!'.codeUnits);
const String mimeType = 'text/plain';
final XFile textFile =
XFile.fromData(fileData, mimeType: mimeType, name: fileName);
await textFile.saveTo(path);
```

[example]:./example
Expand Down
15 changes: 15 additions & 0 deletions packages/file_selector/file_selector/example/build.excerpt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
targets:
$default:
sources:
include:
- lib/**
# Some default includes that aren't really used here but will prevent
# false-negative warnings:
- $package$
- lib/$lib$
exclude:
- '**/.*/**'
- '**/build/**'
builders:
code_excerpter|code_excerpter:
enabled: true
13 changes: 7 additions & 6 deletions packages/file_selector/file_selector/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:example/get_directory_page.dart';
import 'package:example/home_page.dart';
import 'package:example/open_image_page.dart';
import 'package:example/open_multiple_images_page.dart';
import 'package:example/open_text_page.dart';
import 'package:example/save_text_page.dart';
import 'package:flutter/material.dart';

import 'get_directory_page.dart';
import 'home_page.dart';
import 'open_image_page.dart';
import 'open_multiple_images_page.dart';
import 'open_text_page.dart';
import 'save_text_page.dart';

void main() {
runApp(const MyApp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ class OpenImagePage extends StatelessWidget {
const OpenImagePage({Key? key}) : super(key: key);

Future<void> _openImageFile(BuildContext context) async {
// #docregion SingleOpen
final XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: <String>['jpg', 'png'],
);
final List<XFile> files =
await openFiles(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
if (files.isEmpty) {
final XFile? file =
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
// #enddocregion SingleOpen
if (file == null) {
// Operation was canceled by the user.
return;
}
final XFile file = files[0];
final String fileName = file.name;
final String filePath = file.path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class OpenMultipleImagesPage extends StatelessWidget {
const OpenMultipleImagesPage({Key? key}) : super(key: key);

Future<void> _openImageFile(BuildContext context) async {
// #docregion MultiOpen
final XTypeGroup jpgsTypeGroup = XTypeGroup(
label: 'JPEGs',
extensions: <String>['jpg', 'jpeg'],
Expand All @@ -26,6 +27,7 @@ class OpenMultipleImagesPage extends StatelessWidget {
jpgsTypeGroup,
pngTypeGroup,
]);
// #enddocregion MultiOpen
if (files.isEmpty) {
// Operation was canceled by the user.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

/// Screen that shows an example of openFile
class OpenTextPage extends StatelessWidget {
Expand All @@ -15,8 +16,15 @@ class OpenTextPage extends StatelessWidget {
label: 'text',
extensions: <String>['txt', 'json'],
);
final XFile? file =
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
// This demonstrates using an initial directory for the prompt, which should
// only be done in cases where the application can likely predict where the
// file would be. In most cases, this parameter should not be provided.
final String initialDirectory =
(await getApplicationDocumentsDirectory()).path;
final XFile? file = await openFile(
acceptedTypeGroups: <XTypeGroup>[typeGroup],
initialDirectory: initialDirectory,
);
if (file == null) {
// Operation was canceled by the user.
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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.

// This file exists solely to host compiled excerpts for README.md, and is not
// intended for use as an actual example application.

// ignore_for_file: public_member_api_docs

// #docregion Save
import 'dart:typed_data';
stuartmorgan marked this conversation as resolved.
Show resolved Hide resolved
// #enddocregion Save

import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('README snippet app'),
),
body: const Text('See example in main.dart'),
),
);
}

Future<void> saveFile() async {
// #docregion Save
const String fileName = 'suggested_name.txt';
final String? path = await getSavePath(suggestedName: fileName);
if (path == null) {
// Operation was canceled by the user.
return;
}

final Uint8List fileData = Uint8List.fromList('Hello World!'.codeUnits);
const String mimeType = 'text/plain';
final XFile textFile =
XFile.fromData(fileData, mimeType: mimeType, name: fileName);
await textFile.saveTo(path);
// #enddocregion Save
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:typed_data';
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

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

Future<void> _saveFile() async {
final String? path = await getSavePath();
final String fileName = _nameController.text;
// This demonstrates using an initial directory for the prompt, which should
// only be done in cases where the application can likely predict where the
// file will be saved. In most cases, this parameter should not be provided.
final String initialDirectory =
(await getApplicationDocumentsDirectory()).path;
final String? path = await getSavePath(
initialDirectory: initialDirectory,
suggestedName: fileName,
);
if (path == null) {
// Operation was canceled by the user.
return;
}
stuartmorgan marked this conversation as resolved.
Show resolved Hide resolved

final String text = _contentController.text;
final String fileName = _nameController.text;
final Uint8List fileData = Uint8List.fromList(text.codeUnits);
const String fileMimeType = 'text/plain';
final XFile textFile =
XFile.fromData(fileData, mimeType: fileMimeType, name: fileName);

await textFile.saveTo(path);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/file_selector/file_selector/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: example
name: file_selector_example
description: A new Flutter project.
publish_to: none

Expand All @@ -17,8 +17,10 @@ dependencies:
path: ../
flutter:
sdk: flutter
path_provider: ^2.0.9

dev_dependencies:
build_runner: ^2.1.10
flutter_test:
sdk: flutter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ list(APPEND FLUTTER_PLUGIN_LIST
file_selector_windows
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
)

set(PLUGIN_BUNDLED_LIBRARIES)

foreach(plugin ${FLUTTER_PLUGIN_LIST})
Expand All @@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST})
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
endforeach(plugin)

foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin})
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
endforeach(ffi_plugin)
Loading