Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Rework examples for testing and to reduce drift
  • Loading branch information
stuartmorgan-g committed Aug 8, 2025
commit 528a6e7c8795a071ee1f320b483273d6bcfe631b
63 changes: 42 additions & 21 deletions packages/image_picker/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _MyHomePageState extends State<MyHomePage> {
Future<void> _playVideo(XFile? file) async {
if (file != null && mounted) {
await _disposeVideoController();
late VideoPlayerController controller;
final VideoPlayerController controller;
if (kIsWeb) {
controller = VideoPlayerController.networkUrl(Uri.parse(file.path));
} else {
Expand All @@ -85,18 +85,25 @@ class _MyHomePageState extends State<MyHomePage> {
Future<void> _onImageButtonPressed(
ImageSource source, {
required BuildContext context,
bool isMultiImage = false,
bool allowMultiple = false,
bool isMedia = false,
}) async {
if (_controller != null) {
await _controller!.setVolume(0.0);
}
if (context.mounted) {
if (isVideo) {
final XFile? file = await _picker.pickVideo(
source: source, maxDuration: const Duration(seconds: 10));
await _playVideo(file);
} else if (isMultiImage) {
final List<XFile> files;
if (allowMultiple) {
files = await _picker.pickMultiVideo();
} else {
final XFile? file = await _picker.pickVideo(
source: source, maxDuration: const Duration(seconds: 10));
files = <XFile>[if (file != null) file];
}
// Just play the first file, to keep the example simple.
await _playVideo(files.first);
} else if (allowMultiple) {
await _displayPickImageDialog(context, true, (double? maxWidth,
double? maxHeight, int? quality, int? limit) async {
try {
Expand Down Expand Up @@ -349,7 +356,7 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'image0',
tooltip: 'Pick Image from gallery',
tooltip: 'Pick image from gallery',
child: const Icon(Icons.photo),
),
),
Expand All @@ -361,12 +368,11 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(
ImageSource.gallery,
context: context,
isMultiImage: true,
isMedia: true,
allowMultiple: true,
);
},
heroTag: 'multipleMedia',
tooltip: 'Pick Multiple Media from gallery',
heroTag: 'image1',
tooltip: 'Pick multiple images',
child: const Icon(Icons.photo_library),
),
),
Expand All @@ -382,8 +388,8 @@ class _MyHomePageState extends State<MyHomePage> {
);
},
heroTag: 'media',
tooltip: 'Pick Single Media from gallery',
child: const Icon(Icons.photo_library),
tooltip: 'Pick item from gallery',
child: const Icon(Icons.photo_outlined),
),
),
Padding(
Expand All @@ -394,12 +400,13 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(
ImageSource.gallery,
context: context,
isMultiImage: true,
allowMultiple: true,
isMedia: true,
);
},
heroTag: 'image1',
tooltip: 'Pick Multiple Image from gallery',
child: const Icon(Icons.photo_library),
heroTag: 'multipleMedia',
tooltip: 'Pick multiple items',
child: const Icon(Icons.photo_library_outlined),
),
),
if (_picker.supportsImageSource(ImageSource.camera))
Expand All @@ -411,7 +418,7 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'image2',
tooltip: 'Take a Photo',
tooltip: 'Take a photo',
child: const Icon(Icons.camera_alt),
),
),
Expand All @@ -424,7 +431,21 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'video0',
tooltip: 'Pick Video from gallery',
tooltip: 'Pick video from gallery',
child: const Icon(Icons.video_file),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(ImageSource.gallery,
context: context, allowMultiple: true);
},
heroTag: 'video1',
tooltip: 'Pick multiple videos',
child: const Icon(Icons.video_library),
),
),
Expand All @@ -437,8 +458,8 @@ class _MyHomePageState extends State<MyHomePage> {
isVideo = true;
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'video1',
tooltip: 'Take a Video',
heroTag: 'video2',
tooltip: 'Take a video',
child: const Icon(Icons.videocam),
),
),
Expand Down
95 changes: 57 additions & 38 deletions packages/image_picker/image_picker_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ class _MyHomePageState extends State<MyHomePage> {
Future<void> _playVideo(XFile? file) async {
if (file != null && mounted) {
await _disposeVideoController();
late VideoPlayerController controller;

controller = VideoPlayerController.file(File(file.path));
final VideoPlayerController controller =
VideoPlayerController.file(File(file.path));
_controller = controller;
const double volume = 1.0;
await controller.setVolume(volume);
await controller.setVolume(1.0);
await controller.initialize();
await controller.setLooping(true);
await controller.play();
Expand All @@ -95,21 +93,28 @@ class _MyHomePageState extends State<MyHomePage> {
Future<void> _onImageButtonPressed(
ImageSource source, {
required BuildContext context,
bool isMultiImage = false,
bool allowMultiple = false,
bool isMedia = false,
}) async {
if (_controller != null) {
await _controller!.setVolume(0.0);
}
if (context.mounted) {
if (_isVideo) {
final XFile? file = await _picker.getVideo(
source: source, maxDuration: const Duration(seconds: 10));
if (file != null && context.mounted) {
_showPickedSnackBar(context, <XFile>[file]);
final List<XFile> files;
if (allowMultiple) {
files = await _picker.getMultiVideoWithOptions();
} else {
final XFile? file = await _picker.getVideo(
source: source, maxDuration: const Duration(seconds: 10));
files = <XFile>[if (file != null) file];
}
if (files.isNotEmpty && context.mounted) {
_showPickedSnackBar(context, files);
// Just play the first file, to keep the example simple.
await _playVideo(files.first);
}
await _playVideo(file);
} else if (isMultiImage) {
} else if (allowMultiple) {
await _displayPickImageDialog(context, true, (double? maxWidth,
double? maxHeight, int? quality, int? limit) async {
try {
Expand All @@ -121,7 +126,7 @@ class _MyHomePageState extends State<MyHomePage> {
final List<XFile> pickedFileList = isMedia
? await _picker.getMedia(
options: MediaOptions(
allowMultiple: isMultiImage,
allowMultiple: allowMultiple,
imageOptions: imageOptions,
limit: limit,
),
Expand Down Expand Up @@ -151,7 +156,7 @@ class _MyHomePageState extends State<MyHomePage> {
final List<XFile> pickedFileList = <XFile>[];
final XFile? media = _firstOrNull(await _picker.getMedia(
options: MediaOptions(
allowMultiple: isMultiImage,
allowMultiple: allowMultiple,
imageOptions: ImageOptions(
maxWidth: maxWidth,
maxHeight: maxHeight,
Expand Down Expand Up @@ -290,8 +295,7 @@ class _MyHomePageState extends State<MyHomePage> {
Widget _buildInlineVideoPlayer(int index) {
final VideoPlayerController controller =
VideoPlayerController.file(File(_mediaFileList![index].path));
const double volume = 1.0;
controller.setVolume(volume);
controller.setVolume(1.0);
controller.initialize();
controller.setLooping(true);
controller.play();
Expand All @@ -314,7 +318,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (response.file != null) {
if (response.type == RetrieveType.video) {
_isVideo = true;
await _playVideo(response.file);
await _playVideo(response.files?.firstOrNull);
} else {
_isVideo = false;
setState(() {
Expand Down Expand Up @@ -380,8 +384,8 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'image0',
tooltip: 'Pick Image from gallery',
label: const Text('Pick Image from gallery'),
tooltip: 'Pick image from gallery',
label: const Text('Pick image from gallery'),
icon: const Icon(Icons.photo),
),
),
Expand All @@ -393,13 +397,12 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(
ImageSource.gallery,
context: context,
isMultiImage: true,
isMedia: true,
allowMultiple: true,
);
},
heroTag: 'multipleMedia',
tooltip: 'Pick Multiple Media from gallery',
label: const Text('Pick Multiple Media from gallery'),
heroTag: 'image1',
tooltip: 'Pick multiple images',
label: const Text('Pick multiple images'),
icon: const Icon(Icons.photo_library),
),
),
Expand All @@ -415,9 +418,9 @@ class _MyHomePageState extends State<MyHomePage> {
);
},
heroTag: 'media',
tooltip: 'Pick Single Media from gallery',
label: const Text('Pick Single Media from gallery'),
icon: const Icon(Icons.photo_library),
tooltip: 'Pick item from gallery',
label: const Text('Pick item from gallery'),
icon: const Icon(Icons.photo_outlined),
),
),
Padding(
Expand All @@ -428,13 +431,14 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(
ImageSource.gallery,
context: context,
isMultiImage: true,
allowMultiple: true,
isMedia: true,
);
},
heroTag: 'image1',
tooltip: 'Pick Multiple Image from gallery',
label: const Text('Pick Multiple Image from gallery'),
icon: const Icon(Icons.photo_library),
heroTag: 'multipleMedia',
tooltip: 'Pick multiple items',
label: const Text('Pick multiple items'),
icon: const Icon(Icons.photo_library_outlined),
),
),
Padding(
Expand All @@ -445,8 +449,8 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'image2',
tooltip: 'Take a Photo',
label: const Text('Take a Photo'),
tooltip: 'Take a photo',
label: const Text('Take a photo'),
icon: const Icon(Icons.camera_alt),
),
),
Expand All @@ -459,8 +463,23 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'video0',
tooltip: 'Pick Video from gallery',
label: const Text('Pick Video from gallery'),
tooltip: 'Pick video from gallery',
label: const Text('Pick video from gallery'),
icon: const Icon(Icons.video_file),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton.extended(
backgroundColor: Colors.red,
onPressed: () {
_isVideo = true;
_onImageButtonPressed(ImageSource.gallery,
context: context, allowMultiple: true);
},
heroTag: 'video0',
tooltip: 'Pick multiple videos',
label: const Text('Pick multiple videos'),
icon: const Icon(Icons.video_library),
),
),
Expand All @@ -473,8 +492,8 @@ class _MyHomePageState extends State<MyHomePage> {
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'video1',
tooltip: 'Take a Video',
label: const Text('Take a Video'),
tooltip: 'Take a video',
label: const Text('Take a video'),
icon: const Icon(Icons.videocam),
),
),
Expand Down
Loading