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

Commit 03f9b49

Browse files
authored
[image_picker]Update example app (#4103)
1 parent 54082e0 commit 03f9b49

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

packages/image_picker/image_picker/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.8.1+2
2+
3+
* Update the example app to support the multi-image feature.
14

25
## 0.8.1+1
36

packages/image_picker/image_picker/example/lib/main.dart

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ class MyHomePage extends StatefulWidget {
3636
}
3737

3838
class _MyHomePageState extends State<MyHomePage> {
39-
PickedFile? _imageFile;
39+
List<PickedFile>? _imageFileList;
40+
41+
set _imageFile(PickedFile? value) {
42+
_imageFileList = value == null ? null : [value];
43+
}
44+
4045
dynamic _pickImageError;
4146
bool isVideo = false;
47+
4248
VideoPlayerController? _controller;
4349
VideoPlayerController? _toBeDisposed;
4450
String? _retrieveDataError;
@@ -73,14 +79,32 @@ class _MyHomePageState extends State<MyHomePage> {
7379
}
7480

7581
void _onImageButtonPressed(ImageSource source,
76-
{BuildContext? context}) async {
82+
{BuildContext? context, bool isMultiImage = false}) async {
7783
if (_controller != null) {
7884
await _controller!.setVolume(0.0);
7985
}
8086
if (isVideo) {
8187
final PickedFile? file = await _picker.getVideo(
8288
source: source, maxDuration: const Duration(seconds: 10));
8389
await _playVideo(file);
90+
} else if (isMultiImage) {
91+
await _displayPickImageDialog(context!,
92+
(double? maxWidth, double? maxHeight, int? quality) async {
93+
try {
94+
final pickedFileList = await _picker.getMultiImage(
95+
maxWidth: maxWidth,
96+
maxHeight: maxHeight,
97+
imageQuality: quality,
98+
);
99+
setState(() {
100+
_imageFileList = pickedFileList;
101+
});
102+
} catch (e) {
103+
setState(() {
104+
_pickImageError = e;
105+
});
106+
}
107+
});
84108
} else {
85109
await _displayPickImageDialog(context!,
86110
(double? maxWidth, double? maxHeight, int? quality) async {
@@ -146,21 +170,28 @@ class _MyHomePageState extends State<MyHomePage> {
146170
);
147171
}
148172

149-
Widget _previewImage() {
173+
Widget _previewImages() {
150174
final Text? retrieveError = _getRetrieveErrorWidget();
151175
if (retrieveError != null) {
152176
return retrieveError;
153177
}
154-
if (_imageFile != null) {
155-
if (kIsWeb) {
156-
// Why network?
157-
// See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform
158-
return Image.network(_imageFile!.path);
159-
} else {
160-
return Semantics(
161-
child: Image.file(File(_imageFile!.path)),
162-
label: 'image_picker_example_picked_image');
163-
}
178+
if (_imageFileList != null) {
179+
return Semantics(
180+
child: ListView.builder(
181+
key: UniqueKey(),
182+
itemBuilder: (context, index) {
183+
// Why network for web?
184+
// See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform
185+
return Semantics(
186+
label: 'image_picker_example_picked_image',
187+
child: kIsWeb
188+
? Image.network(_imageFileList![index].path)
189+
: Image.file(File(_imageFileList![index].path)),
190+
);
191+
},
192+
itemCount: _imageFileList!.length,
193+
),
194+
label: 'image_picker_example_picked_images');
164195
} else if (_pickImageError != null) {
165196
return Text(
166197
'Pick image error: $_pickImageError',
@@ -174,6 +205,14 @@ class _MyHomePageState extends State<MyHomePage> {
174205
}
175206
}
176207

208+
Widget _handlePreview() {
209+
if (isVideo) {
210+
return _previewVideo();
211+
} else {
212+
return _previewImages();
213+
}
214+
}
215+
177216
Future<void> retrieveLostData() async {
178217
final LostData response = await _picker.getLostData();
179218
if (response.isEmpty) {
@@ -213,7 +252,7 @@ class _MyHomePageState extends State<MyHomePage> {
213252
textAlign: TextAlign.center,
214253
);
215254
case ConnectionState.done:
216-
return isVideo ? _previewVideo() : _previewImage();
255+
return _handlePreview();
217256
default:
218257
if (snapshot.hasError) {
219258
return Text(
@@ -229,7 +268,7 @@ class _MyHomePageState extends State<MyHomePage> {
229268
}
230269
},
231270
)
232-
: (isVideo ? _previewVideo() : _previewImage()),
271+
: _handlePreview(),
233272
),
234273
floatingActionButton: Column(
235274
mainAxisAlignment: MainAxisAlignment.end,
@@ -243,6 +282,22 @@ class _MyHomePageState extends State<MyHomePage> {
243282
},
244283
heroTag: 'image0',
245284
tooltip: 'Pick Image from gallery',
285+
child: const Icon(Icons.photo),
286+
),
287+
),
288+
Padding(
289+
padding: const EdgeInsets.only(top: 16.0),
290+
child: FloatingActionButton(
291+
onPressed: () {
292+
isVideo = false;
293+
_onImageButtonPressed(
294+
ImageSource.gallery,
295+
context: context,
296+
isMultiImage: true,
297+
);
298+
},
299+
heroTag: 'image1',
300+
tooltip: 'Pick Multiple Image from gallery',
246301
child: const Icon(Icons.photo_library),
247302
),
248303
),
@@ -253,7 +308,7 @@ class _MyHomePageState extends State<MyHomePage> {
253308
isVideo = false;
254309
_onImageButtonPressed(ImageSource.camera, context: context);
255310
},
256-
heroTag: 'image1',
311+
heroTag: 'image2',
257312
tooltip: 'Take a Photo',
258313
child: const Icon(Icons.camera_alt),
259314
),

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/plugins/tree/master/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.1+1
6+
version: 0.8.1+2
77

88
environment:
99
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)