diff --git a/packages/image_picker/image_picker/CHANGELOG.md b/packages/image_picker/image_picker/CHANGELOG.md index 428203b0327a..8385aff7e48c 100644 --- a/packages/image_picker/image_picker/CHANGELOG.md +++ b/packages/image_picker/image_picker/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.8.1+2 + +* Update the example app to support the multi-image feature. ## 0.8.1+1 diff --git a/packages/image_picker/image_picker/example/lib/main.dart b/packages/image_picker/image_picker/example/lib/main.dart index 698de1d98898..71388ef5db2f 100755 --- a/packages/image_picker/image_picker/example/lib/main.dart +++ b/packages/image_picker/image_picker/example/lib/main.dart @@ -36,9 +36,15 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - PickedFile? _imageFile; + List? _imageFileList; + + set _imageFile(PickedFile? value) { + _imageFileList = value == null ? null : [value]; + } + dynamic _pickImageError; bool isVideo = false; + VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; @@ -73,7 +79,7 @@ class _MyHomePageState extends State { } void _onImageButtonPressed(ImageSource source, - {BuildContext? context}) async { + {BuildContext? context, bool isMultiImage = false}) async { if (_controller != null) { await _controller!.setVolume(0.0); } @@ -81,6 +87,24 @@ class _MyHomePageState extends State { final PickedFile? file = await _picker.getVideo( source: source, maxDuration: const Duration(seconds: 10)); await _playVideo(file); + } else if (isMultiImage) { + await _displayPickImageDialog(context!, + (double? maxWidth, double? maxHeight, int? quality) async { + try { + final pickedFileList = await _picker.getMultiImage( + maxWidth: maxWidth, + maxHeight: maxHeight, + imageQuality: quality, + ); + setState(() { + _imageFileList = pickedFileList; + }); + } catch (e) { + setState(() { + _pickImageError = e; + }); + } + }); } else { await _displayPickImageDialog(context!, (double? maxWidth, double? maxHeight, int? quality) async { @@ -146,21 +170,28 @@ class _MyHomePageState extends State { ); } - Widget _previewImage() { + Widget _previewImages() { final Text? retrieveError = _getRetrieveErrorWidget(); if (retrieveError != null) { return retrieveError; } - if (_imageFile != null) { - if (kIsWeb) { - // Why network? - // See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform - return Image.network(_imageFile!.path); - } else { - return Semantics( - child: Image.file(File(_imageFile!.path)), - label: 'image_picker_example_picked_image'); - } + if (_imageFileList != null) { + return Semantics( + child: ListView.builder( + key: UniqueKey(), + itemBuilder: (context, index) { + // Why network for web? + // See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform + return Semantics( + label: 'image_picker_example_picked_image', + child: kIsWeb + ? Image.network(_imageFileList![index].path) + : Image.file(File(_imageFileList![index].path)), + ); + }, + itemCount: _imageFileList!.length, + ), + label: 'image_picker_example_picked_images'); } else if (_pickImageError != null) { return Text( 'Pick image error: $_pickImageError', @@ -174,6 +205,14 @@ class _MyHomePageState extends State { } } + Widget _handlePreview() { + if (isVideo) { + return _previewVideo(); + } else { + return _previewImages(); + } + } + Future retrieveLostData() async { final LostData response = await _picker.getLostData(); if (response.isEmpty) { @@ -213,7 +252,7 @@ class _MyHomePageState extends State { textAlign: TextAlign.center, ); case ConnectionState.done: - return isVideo ? _previewVideo() : _previewImage(); + return _handlePreview(); default: if (snapshot.hasError) { return Text( @@ -229,7 +268,7 @@ class _MyHomePageState extends State { } }, ) - : (isVideo ? _previewVideo() : _previewImage()), + : _handlePreview(), ), floatingActionButton: Column( mainAxisAlignment: MainAxisAlignment.end, @@ -243,6 +282,22 @@ class _MyHomePageState extends State { }, heroTag: 'image0', tooltip: 'Pick Image from gallery', + child: const Icon(Icons.photo), + ), + ), + Padding( + padding: const EdgeInsets.only(top: 16.0), + child: FloatingActionButton( + onPressed: () { + isVideo = false; + _onImageButtonPressed( + ImageSource.gallery, + context: context, + isMultiImage: true, + ); + }, + heroTag: 'image1', + tooltip: 'Pick Multiple Image from gallery', child: const Icon(Icons.photo_library), ), ), @@ -253,7 +308,7 @@ class _MyHomePageState extends State { isVideo = false; _onImageButtonPressed(ImageSource.camera, context: context); }, - heroTag: 'image1', + heroTag: 'image2', tooltip: 'Take a Photo', child: const Icon(Icons.camera_alt), ), diff --git a/packages/image_picker/image_picker/pubspec.yaml b/packages/image_picker/image_picker/pubspec.yaml index e8fafb324e71..620d118142fb 100755 --- a/packages/image_picker/image_picker/pubspec.yaml +++ b/packages/image_picker/image_picker/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image library, and taking new pictures with the camera. repository: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22 -version: 0.8.1+1 +version: 0.8.1+2 environment: sdk: ">=2.12.0 <3.0.0"