Skip to content

Commit 1ce89ad

Browse files
[image_picker] Adopt readme excerpts (flutter#3507)
[image_picker] Adopt readme excerpts
1 parent 3347a31 commit 1ce89ad

File tree

8 files changed

+157
-24
lines changed

8 files changed

+157
-24
lines changed

packages/image_picker/image_picker/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.7+1
2+
3+
* Updates README to use code excerpts.
4+
15
## 0.8.7
26

37
* Adds `usePhotoPickerAndroid` options.

packages/image_picker/image_picker/README.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Image Picker plugin for Flutter
2+
<?code-excerpt path-base="excerpts/packages/image_picker_example"?>
23

34
[![pub package](https://img.shields.io/pub/v/image_picker.svg)](https://pub.dev/packages/image_picker)
45

@@ -42,22 +43,20 @@ If you require your picked image to be stored permanently, it is your responsibi
4243

4344
### Example
4445

46+
<?code-excerpt "readme_excerpts.dart (Pick)"?>
4547
``` dart
46-
import 'package:image_picker/image_picker.dart';
47-
48-
...
49-
final ImagePicker _picker = ImagePicker();
50-
// Pick an image
51-
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
52-
// Capture a photo
53-
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
54-
// Pick a video
55-
final XFile? image = await _picker.pickVideo(source: ImageSource.gallery);
56-
// Capture a video
57-
final XFile? video = await _picker.pickVideo(source: ImageSource.camera);
58-
// Pick multiple images
59-
final List<XFile>? images = await _picker.pickMultiImage();
60-
...
48+
final ImagePicker picker = ImagePicker();
49+
// Pick an image.
50+
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
51+
// Capture a photo.
52+
final XFile? photo = await picker.pickImage(source: ImageSource.camera);
53+
// Pick a video.
54+
final XFile? galleryVideo =
55+
await picker.pickVideo(source: ImageSource.gallery);
56+
// Capture a video.
57+
final XFile? cameraVideo = await picker.pickVideo(source: ImageSource.camera);
58+
// Pick multiple images.
59+
final List<XFile> images = await picker.pickMultiImage();
6160
```
6261

6362
### Handling MainActivity destruction on Android
@@ -71,17 +70,17 @@ low on memory. When the intent finishes executing, Android will restart the
7170
application. Since the data is never returned to the original call use the
7271
`ImagePicker.retrieveLostData()` method to retrieve the lost data. For example:
7372

73+
<?code-excerpt "readme_excerpts.dart (LostData)"?>
7474
```dart
7575
Future<void> getLostData() async {
76-
final LostDataResponse response =
77-
await picker.retrieveLostData();
76+
final ImagePicker picker = ImagePicker();
77+
final LostDataResponse response = await picker.retrieveLostData();
7878
if (response.isEmpty) {
7979
return;
8080
}
81-
if (response.files != null) {
82-
for (final XFile file in response.files) {
83-
_handleFile(file);
84-
}
81+
final List<XFile>? files = response.files;
82+
if (files != null) {
83+
_handleLostFiles(files);
8584
} else {
8685
_handleError(response.exception);
8786
}
@@ -95,7 +94,7 @@ complete example of handling this flow.
9594

9695
### Android Photo Picker
9796

98-
This package has optional [Android Photo Picker](https://developer.android.com/training/data-storage/shared/photopicker) functionality.
97+
This package has optional [Android Photo Picker](https://developer.android.com/training/data-storage/shared/photopicker) functionality.
9998
[Learn how to use it](https://pub.dev/packages/image_picker_android).
10099

101100
## Migrating to 0.8.2+
Lines changed: 15 additions & 0 deletions
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
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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:flutter/services.dart';
6+
import 'package:image_picker/image_picker.dart';
7+
8+
/// Example function for README demonstration of various pick* calls.
9+
Future<List<XFile?>> readmePickExample() async {
10+
// #docregion Pick
11+
final ImagePicker picker = ImagePicker();
12+
// Pick an image.
13+
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
14+
// Capture a photo.
15+
final XFile? photo = await picker.pickImage(source: ImageSource.camera);
16+
// Pick a video.
17+
final XFile? galleryVideo =
18+
await picker.pickVideo(source: ImageSource.gallery);
19+
// Capture a video.
20+
final XFile? cameraVideo = await picker.pickVideo(source: ImageSource.camera);
21+
// Pick multiple images.
22+
final List<XFile> images = await picker.pickMultiImage();
23+
// #enddocregion Pick
24+
25+
// Return everything for the sanity check test.
26+
return <XFile?>[
27+
image,
28+
photo,
29+
galleryVideo,
30+
cameraVideo,
31+
if (images.isEmpty) null else images.first
32+
];
33+
}
34+
35+
/// Example function for README demonstration of getting lost data.
36+
// #docregion LostData
37+
Future<void> getLostData() async {
38+
final ImagePicker picker = ImagePicker();
39+
final LostDataResponse response = await picker.retrieveLostData();
40+
if (response.isEmpty) {
41+
return;
42+
}
43+
final List<XFile>? files = response.files;
44+
if (files != null) {
45+
_handleLostFiles(files);
46+
} else {
47+
_handleError(response.exception);
48+
}
49+
}
50+
// #enddocregion LostData
51+
52+
// Stubs for the getLostData function.
53+
void _handleLostFiles(List<XFile> file) {}
54+
void _handleError(PlatformException? exception) {}

packages/image_picker/image_picker/example/pubspec.yaml

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ dependencies:
1717
# The example app is bundled with the plugin so we use a path dependency on
1818
# the parent directory to use the current plugin's version.
1919
path: ../
20+
image_picker_platform_interface: ^2.6.1
2021
video_player: ^2.1.4
2122

2223
dev_dependencies:
24+
build_runner: ^2.1.10
2325
espresso: ^0.2.0
2426
flutter_driver:
2527
sdk: flutter
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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:flutter_test/flutter_test.dart';
6+
import 'package:image_picker_example/readme_excerpts.dart';
7+
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
8+
9+
void main() {
10+
TestWidgetsFlutterBinding.ensureInitialized();
11+
12+
setUpAll(() async {
13+
ImagePickerPlatform.instance = FakeImagePicker();
14+
});
15+
16+
test('sanity check readmePickExample', () async {
17+
// Ensure that the snippet code runs successfully.
18+
final List<XFile?> results = await readmePickExample();
19+
// It should demonstrate a variety of calls.
20+
expect(results.length, greaterThan(4));
21+
// And the calls should all be different. This works since each fake call
22+
// returns a different result.
23+
expect(results.map((XFile? file) => file?.path).toSet().length,
24+
results.length);
25+
});
26+
27+
test('sanity check getLostData', () async {
28+
// Ensure that the snippet code runs successfully.
29+
await getLostData();
30+
});
31+
}
32+
33+
class FakeImagePicker extends ImagePickerPlatform {
34+
@override
35+
Future<XFile?> getImageFromSource(
36+
{required ImageSource source,
37+
ImagePickerOptions options = const ImagePickerOptions()}) async {
38+
return XFile(source == ImageSource.camera ? 'cameraImage' : 'galleryImage');
39+
}
40+
41+
@override
42+
Future<LostDataResponse> getLostData() async {
43+
return LostDataResponse.empty();
44+
}
45+
46+
@override
47+
Future<List<XFile>> getMultiImageWithOptions(
48+
{MultiImagePickerOptions options =
49+
const MultiImagePickerOptions()}) async {
50+
return <XFile>[XFile('multiImage')];
51+
}
52+
53+
@override
54+
Future<XFile?> getVideo(
55+
{required ImageSource source,
56+
CameraDevice preferredCameraDevice = CameraDevice.rear,
57+
Duration? maxDuration}) async {
58+
return XFile(source == ImageSource.camera ? 'cameraVideo' : 'galleryVideo');
59+
}
60+
}

packages/image_picker/image_picker/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
33
library, and taking new pictures with the camera.
44
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
6-
version: 0.8.7
6+
version: 0.8.7+1
77

88
environment:
99
sdk: ">=2.17.0 <4.0.0"

script/configs/temp_exclude_excerpt.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
- google_maps_flutter/google_maps_flutter
1818
- google_sign_in/google_sign_in
1919
- google_sign_in_web
20-
- image_picker/image_picker
2120
- image_picker_for_web
2221
- in_app_purchase/in_app_purchase
2322
- ios_platform_images

0 commit comments

Comments
 (0)