Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.8.1+2

* Update the example app to support the multi-image feature.

## 0.8.1+1

Expand Down
87 changes: 71 additions & 16 deletions packages/image_picker/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
PickedFile? _imageFile;
List<PickedFile>? _imageFileList;

set _imageFile(PickedFile? value) {
_imageFileList = value == null ? null : [value];
}

dynamic _pickImageError;
bool isVideo = false;

VideoPlayerController? _controller;
VideoPlayerController? _toBeDisposed;
String? _retrieveDataError;
Expand Down Expand Up @@ -73,14 +79,32 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _onImageButtonPressed(ImageSource source,
{BuildContext? context}) async {
{BuildContext? context, bool isMultiImage = false}) async {
if (_controller != null) {
await _controller!.setVolume(0.0);
}
if (isVideo) {
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 {
Expand Down Expand Up @@ -146,21 +170,28 @@ class _MyHomePageState extends State<MyHomePage> {
);
}

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',
Expand All @@ -174,6 +205,14 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Widget _handlePreview() {
if (isVideo) {
return _previewVideo();
} else {
return _previewImages();
}
}

Future<void> retrieveLostData() async {
final LostData response = await _picker.getLostData();
if (response.isEmpty) {
Expand Down Expand Up @@ -213,7 +252,7 @@ class _MyHomePageState extends State<MyHomePage> {
textAlign: TextAlign.center,
);
case ConnectionState.done:
return isVideo ? _previewVideo() : _previewImage();
return _handlePreview();
default:
if (snapshot.hasError) {
return Text(
Expand All @@ -229,7 +268,7 @@ class _MyHomePageState extends State<MyHomePage> {
}
},
)
: (isVideo ? _previewVideo() : _previewImage()),
: _handlePreview(),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
Expand All @@ -243,6 +282,22 @@ class _MyHomePageState extends State<MyHomePage> {
},
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),
),
),
Expand All @@ -253,7 +308,7 @@ class _MyHomePageState extends State<MyHomePage> {
isVideo = false;
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'image1',
heroTag: 'image2',
tooltip: 'Take a Photo',
child: const Icon(Icons.camera_alt),
),
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down