@@ -36,9 +36,15 @@ class MyHomePage extends StatefulWidget {
36
36
}
37
37
38
38
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
+
40
45
dynamic _pickImageError;
41
46
bool isVideo = false ;
47
+
42
48
VideoPlayerController ? _controller;
43
49
VideoPlayerController ? _toBeDisposed;
44
50
String ? _retrieveDataError;
@@ -73,14 +79,32 @@ class _MyHomePageState extends State<MyHomePage> {
73
79
}
74
80
75
81
void _onImageButtonPressed (ImageSource source,
76
- {BuildContext ? context}) async {
82
+ {BuildContext ? context, bool isMultiImage = false }) async {
77
83
if (_controller != null ) {
78
84
await _controller! .setVolume (0.0 );
79
85
}
80
86
if (isVideo) {
81
87
final PickedFile ? file = await _picker.getVideo (
82
88
source: source, maxDuration: const Duration (seconds: 10 ));
83
89
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
+ });
84
108
} else {
85
109
await _displayPickImageDialog (context! ,
86
110
(double ? maxWidth, double ? maxHeight, int ? quality) async {
@@ -146,21 +170,28 @@ class _MyHomePageState extends State<MyHomePage> {
146
170
);
147
171
}
148
172
149
- Widget _previewImage () {
173
+ Widget _previewImages () {
150
174
final Text ? retrieveError = _getRetrieveErrorWidget ();
151
175
if (retrieveError != null ) {
152
176
return retrieveError;
153
177
}
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' );
164
195
} else if (_pickImageError != null ) {
165
196
return Text (
166
197
'Pick image error: $_pickImageError ' ,
@@ -174,6 +205,14 @@ class _MyHomePageState extends State<MyHomePage> {
174
205
}
175
206
}
176
207
208
+ Widget _handlePreview () {
209
+ if (isVideo) {
210
+ return _previewVideo ();
211
+ } else {
212
+ return _previewImages ();
213
+ }
214
+ }
215
+
177
216
Future <void > retrieveLostData () async {
178
217
final LostData response = await _picker.getLostData ();
179
218
if (response.isEmpty) {
@@ -213,7 +252,7 @@ class _MyHomePageState extends State<MyHomePage> {
213
252
textAlign: TextAlign .center,
214
253
);
215
254
case ConnectionState .done:
216
- return isVideo ? _previewVideo () : _previewImage ();
255
+ return _handlePreview ();
217
256
default :
218
257
if (snapshot.hasError) {
219
258
return Text (
@@ -229,7 +268,7 @@ class _MyHomePageState extends State<MyHomePage> {
229
268
}
230
269
},
231
270
)
232
- : (isVideo ? _previewVideo () : _previewImage () ),
271
+ : _handlePreview ( ),
233
272
),
234
273
floatingActionButton: Column (
235
274
mainAxisAlignment: MainAxisAlignment .end,
@@ -243,6 +282,22 @@ class _MyHomePageState extends State<MyHomePage> {
243
282
},
244
283
heroTag: 'image0' ,
245
284
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' ,
246
301
child: const Icon (Icons .photo_library),
247
302
),
248
303
),
@@ -253,7 +308,7 @@ class _MyHomePageState extends State<MyHomePage> {
253
308
isVideo = false ;
254
309
_onImageButtonPressed (ImageSource .camera, context: context);
255
310
},
256
- heroTag: 'image1 ' ,
311
+ heroTag: 'image2 ' ,
257
312
tooltip: 'Take a Photo' ,
258
313
child: const Icon (Icons .camera_alt),
259
314
),
0 commit comments