@@ -36,9 +36,15 @@ class MyHomePage extends StatefulWidget {
3636}
3737
3838class _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 ),
0 commit comments