### Example main.dart for image_picker_windows Source: https://pub.dev/packages/image_picker_windows/example Example main.dart for the image_picker_windows package. It sets up a Flutter app with an ImagePickerPlatform instance and a video player controller, and includes a method to play a video from a picked file. The code is incomplete as shown; the rest of the example is not included in the source. ```dart // Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; import 'package:mime/mime.dart'; import 'package:video_player/video_player.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Image Picker Demo', home: MyHomePage(title: 'Image Picker Example'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, this.title}); final String? title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { List? _mediaFileList; // This must be called from within a setState() callback void _setImageFileListFromFile(XFile? value) { _mediaFileList = value == null ? null : [value]; } dynamic _pickImageError; bool _isVideo = false; VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; final ImagePickerPlatform _picker = ImagePickerPlatform.instance; final TextEditingController maxWidthController = TextEditingController(); final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); Future _playVideo(XFile? file) async { if (file != null && mounted) { await _disposeVideoController(); final VideoPlayerController controller = VideoPlayerController.file(File(file.path)); _controller = controller; await controller.setVolume(1.0); await controller.initialize(); await controller.setLooping(true); await controller.play(); setState(() {}); } } ``` -------------------------------- ### Example main.dart for image_picker_macos Source: https://pub.dev/packages/image_picker_macos/example Example app for image_picker_macos. It sets up a MaterialApp with a home page that uses ImagePickerPlatform.instance to pick images or videos, and plays videos with VideoPlayerController. The code includes controllers for max width, max height, and quality, and a _playVideo method that initializes and plays a video file. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; import 'package:mime/mime.dart'; import 'package:video_player/video_player.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Image Picker Demo', home: MyHomePage(title: 'Image Picker Example'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, this.title}); final String? title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { List? _mediaFileList; // This must be called from within a setState() callback void _setImageFileListFromFile(XFile? value) { _mediaFileList = value == null ? null : [value]; } dynamic _pickImageError; bool _isVideo = false; VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; final ImagePickerPlatform _picker = ImagePickerPlatform.instance; final TextEditingController maxWidthController = TextEditingController(); final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); Future _playVideo(XFile? file) async { if (file != null && mounted) { await _disposeVideoController(); final VideoPlayerController controller = VideoPlayerController.file( File(file.path), ); _controller = controller; await controller.setVolume(1.0); await controller.initialize(); await controller.setLooping(true); await controller.play(); setState(() {}); } } ``` -------------------------------- ### image_picker_linux example main.dart Source: https://pub.dev/packages/image_picker_linux/example Example main.dart for the image_picker_linux package. It sets up a Flutter app that uses ImagePickerPlatform.instance to pick images and videos, and includes a _playVideo method that initializes and plays a video file using VideoPlayerController. ```dart // Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; import 'package:mime/mime.dart'; import 'package:video_player/video_player.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Image Picker Demo', home: MyHomePage(title: 'Image Picker Example'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, this.title}); final String? title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { List? _mediaFileList; // This must be called from within a setState() callback void _setImageFileListFromFile(XFile? value) { _mediaFileList = value == null ? null : [value]; } dynamic _pickImageError; bool _isVideo = false; VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; final ImagePickerPlatform _picker = ImagePickerPlatform.instance; final TextEditingController maxWidthController = TextEditingController(); final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); Future _playVideo(XFile? file) async { if (file != null && mounted) { await _disposeVideoController(); final VideoPlayerController controller = VideoPlayerController.file(File(file.path)); _controller = controller; await controller.setVolume(1.0); await controller.initialize(); await controller.setLooping(true); await controller.play(); setState(() {}); } } } ``` -------------------------------- ### Enable Android Photo Picker in image_picker_android example Source: https://pub.dev/packages/image_picker_android/example Example app for the image_picker_android plugin. It enables the Android Photo Picker by setting useAndroidPhotoPicker to true on the ImagePickerAndroid instance, then runs the app. The code includes a video player controller setup for playing picked videos. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_driver/driver_extension.dart'; // #docregion photo-picker-example import 'package:image_picker_android/image_picker_android.dart'; import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; // #enddocregion photo-picker-example import 'package:mime/mime.dart'; import 'package:video_player/video_player.dart'; @pragma('vm:entry-point') void appMain() { enableFlutterDriverExtension(); main(); } void main() { // Set to use Android Photo Picker. // #docregion photo-picker-example final ImagePickerPlatform imagePickerImplementation = ImagePickerPlatform.instance; if (imagePickerImplementation is ImagePickerAndroid) { imagePickerImplementation.useAndroidPhotoPicker = true; } // #enddocregion photo-picker-example runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Image Picker Demo', home: MyHomePage(title: 'Image Picker Example'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, this.title}); final String? title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { List? _mediaFileList; void _setImageFileListFromFile(XFile? value) { _mediaFileList = value == null ? null : [value]; } dynamic _pickImageError; bool _isVideo = false; VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; final ImagePickerPlatform _picker = ImagePickerPlatform.instance; final TextEditingController maxWidthController = TextEditingController(); final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); final TextEditingController limitController = TextEditingController(); Future _playVideo(XFile? file) async { if (file != null && mounted) { await _disposeVideoController(); final controller = VideoPlayerController.file(File(file.path)); _controller = controller; await controller.setVolume(1.0); await controller.initialize(); await controller.setLooping(true); await controller.play(); setState(() {}); } } } ``` -------------------------------- ### Flutter example app using image_picker_ios Source: https://pub.dev/packages/image_picker_ios/example Complete example Flutter application that uses the image_picker_platform_interface to pick images and videos, with video playback via video_player. Includes controllers for max width, max height, quality, and limit, and handles errors and disposal of video controllers. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; import 'package:mime/mime.dart'; import 'package:video_player/video_player.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Image Picker Demo', home: MyHomePage(title: 'Image Picker Example'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, this.title}); final String? title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { List? _mediaFileList; void _setImageFileListFromFile(XFile? value) { _mediaFileList = value == null ? null : [value]; } dynamic _pickImageError; bool _isVideo = false; VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; final ImagePickerPlatform _picker = ImagePickerPlatform.instance; final TextEditingController maxWidthController = TextEditingController(); final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); final TextEditingController limitController = TextEditingController(); Future _playVideo(XFile? file) async { if (file != null && mounted) { await _disposeVideoController(); final controller = VideoPlayerController.file(File(file.path)); _controller = controller; await controller.setVolume(1.0); await controller.initialize(); await controller.setLooping(true); await controller.play(); setState(() {}); } } ``` -------------------------------- ### Image picker example: preview and build widgets Source: https://pub.dev/packages/image_picker_linux/example Widget methods from the image_picker example that display picked images or videos, handle preview selection, and build the main Scaffold with floating action buttons for picking from the gallery. The preview method uses Semantics labels and handles errors, while the video player controller is initialized and played inline. The build method includes buttons for single, multiple, and media picking with appropriate tooltips and icons. ```dart Widget _previewImages() { final Text? retrieveError = _getRetrieveErrorWidget(); if (retrieveError != null) { return retrieveError; } if (_mediaFileList != null) { return Semantics( label: 'image_picker_example_picked_images', child: ListView.builder( key: UniqueKey(), itemBuilder: (BuildContext context, int index) { final XFile image = _mediaFileList![index]; final String? mime = lookupMimeType(_mediaFileList![index].path); return Column( mainAxisSize: MainAxisSize.min, children: [ Text(image.name, key: const Key('image_picker_example_picked_image_name')), Semantics( label: 'image_picker_example_picked_image', child: mime == null || mime.startsWith('image/') ? Image.file( File(_mediaFileList![index].path), errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) { return const Center( child: Text('This image type is not supported')); }, ) : _buildInlineVideoPlayer(index), ), ], ); }, itemCount: _mediaFileList!.length, ), ); } else if (_pickImageError != null) { return Text( 'Pick image error: $_pickImageError', textAlign: TextAlign.center, ); } else { return const Text( 'You have not yet picked an image.', textAlign: TextAlign.center, ); } } ``` ```dart Widget _buildInlineVideoPlayer(int index) { final VideoPlayerController controller = VideoPlayerController.file(File(_mediaFileList![index].path)); controller.setVolume(1.0); controller.initialize(); controller.setLooping(true); controller.play(); return Center(child: AspectRatioVideo(controller)); } ``` ```dart Widget _handlePreview() { if (_isVideo) { return _previewVideo(); } else { return _previewImages(); } } ``` ```dart @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title!), ), body: Align( alignment: Alignment.topCenter, child: _handlePreview(), ), floatingActionButton: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ Semantics( label: 'image_picker_example_from_gallery', child: FloatingActionButton.extended( key: const Key('image_picker_example_from_gallery'), onPressed: () { _isVideo = false; _onImageButtonPressed(ImageSource.gallery, context: context); }, heroTag: 'image0', tooltip: 'Pick image from gallery', label: const Text('Pick image from gallery'), icon: const Icon(Icons.photo), ), ), Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton.extended( onPressed: () { _isVideo = false; _onImageButtonPressed( ImageSource.gallery, context: context, allowMultiple: true, ); }, heroTag: 'image1', tooltip: 'Pick multiple images', label: const Text('Pick multiple images'), icon: const Icon(Icons.photo_library), ), ), Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton.extended( onPressed: () { _isVideo = false; _onImageButtonPressed( ImageSource.gallery, context: context, isMedia: true, ); }, heroTag: 'media', tooltip: 'Pick item from gallery', label: const Text('Pick item from gallery'), icon: const Icon(Icons.photo_outlined), ), ), Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton.extended( onPressed: () { _isVideo = false; _onImageButtonPressed( ImageSource.gallery, context: context, allowMultiple: true, isMedia: true, ); }, heroTag: 'multipleMedia', tooltip: 'Pick multiple items', label: const Text('Pick multiple items'), ), ), ], ), ); } ``` -------------------------------- ### Flutter image_picker example app (main.dart) Source: https://pub.dev/packages/image_picker/example Complete Flutter app demonstrating image_picker usage. It initializes the app, provides a home page with state management, and includes logic for picking media files and playing videos. The code handles web-specific video playback by muting the video on web to allow auto-play. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:mime/mime.dart'; import 'package:video_player/video_player.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( title: 'Image Picker Demo', home: MyHomePage(title: 'Image Picker Example'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, this.title}); final String? title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { List? _mediaFileList; void _setImageFileListFromFile(XFile? value) { _mediaFileList = value == null ? null : [value]; } dynamic _pickImageError; bool isVideo = false; VideoPlayerController? _controller; VideoPlayerController? _toBeDisposed; String? _retrieveDataError; final ImagePicker _picker = ImagePicker(); final TextEditingController maxWidthController = TextEditingController(); final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); final TextEditingController limitController = TextEditingController(); Future _playVideo(XFile? file) async { if (file != null && mounted) { await _disposeVideoController(); final VideoPlayerController controller; if (kIsWeb) { controller = VideoPlayerController.networkUrl(Uri.parse(file.path)); } else { controller = VideoPlayerController.file(File(file.path)); } _controller = controller; // In web, most browsers won't honor a programmatic call to .play // if the video has a sound track (and is not muted). // Mute the video so it auto-plays in web! // This is not needed if the call to .play is the result of user // interaction (clicking on a "play" button, for example). const volume = kIsWeb ? 0.0 : 1.0; await controller.setVolume(volume); await controller.initialize(); await controller.setLooping(true); await controller.play(); setState(() {}); } } } ``` -------------------------------- ### Example Flutter app (main.dart) Source: https://pub.dev/packages/image_picker_for_web/example A minimal Flutter application that displays a text widget. It is used for testing the image_picker_for_web package, with results expected in the console output. ```dart // Copyright 2013 The Flutter Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } /// App for testing class MyApp extends StatefulWidget { /// Default Constructor const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { @override Widget build(BuildContext context) { return const Directionality( textDirection: TextDirection.ltr, child: Text('Testing... Look at the console output for results!'), ); } } ``` -------------------------------- ### Image picker example: preview, retrieve lost data, and build UI Source: https://pub.dev/packages/image_picker_android/example These Dart methods are part of an image_picker example app. They handle previewing picked images and videos, retrieving lost data on Android, and building the main UI with a gallery picker button. The build method uses FutureBuilder to call retrieveLostData() on Android, and the preview methods use Semantics labels for accessibility. ```dart Widget _previewImages() { final Text? retrieveError = _getRetrieveErrorWidget(); if (retrieveError != null) { return retrieveError; } if (_mediaFileList != null) { return Semantics( label: 'image_picker_example_picked_images', child: ListView.builder( key: UniqueKey(), itemBuilder: (BuildContext context, int index) { final XFile image = _mediaFileList![index]; final String? mime = lookupMimeType(_mediaFileList![index].path); return Column( mainAxisSize: MainAxisSize.min, children: [ Text(image.name, key: const Key('image_picker_example_picked_image_name')), Semantics( label: 'image_picker_example_picked_image', child: mime == null || mime.startsWith('image/') ? Image.file( File(_mediaFileList![index].path), errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) { return const Center( child: Text('This image type is not supported'), ); }, ) : _buildInlineVideoPlayer(index), ), ], ); }, itemCount: _mediaFileList!.length, ), ); } else if (_pickImageError != null) { return Text('Pick image error: $_pickImageError', textAlign: TextAlign.center); } else { return const Text('You have not yet picked an image.', textAlign: TextAlign.center); } } ``` ```dart Widget _buildInlineVideoPlayer(int index) { final controller = VideoPlayerController.file(File(_mediaFileList![index].path)); controller.setVolume(1.0); controller.initialize(); controller.setLooping(true); controller.play(); return Center(child: AspectRatioVideo(controller)); } ``` ```dart Widget _handlePreview() { if (_isVideo) { return _previewVideo(); } else { return _previewImages(); } } ``` ```dart Future retrieveLostData() async { final LostDataResponse response = await _picker.getLostData(); if (response.isEmpty) { return; } if (response.file != null) { if (response.type == RetrieveType.video) { _isVideo = true; await _playVideo(response.files?.firstOrNull); } else { _isVideo = false; setState(() { if (response.files == null) { _setImageFileListFromFile(response.file); } else { _mediaFileList = response.files; } }); } } else { _retrieveDataError = response.exception!.code; } } ``` ```dart @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(widget.title!)), body: Align( alignment: Alignment.topCenter, child: !kIsWeb && defaultTargetPlatform == TargetPlatform.android ? FutureBuilder( future: retrieveLostData(), builder: (BuildContext context, AsyncSnapshot snapshot) { switch (snapshot.connectionState) { case ConnectionState.none: case ConnectionState.waiting: return const Text( 'You have not yet picked an image.', textAlign: TextAlign.center, ); case ConnectionState.done: return _handlePreview(); case ConnectionState.active: if (snapshot.hasError) { return Text( 'Pick image/video error: ${snapshot.error}}', textAlign: TextAlign.center, ); } else { return const Text( 'You have not yet picked an image.', textAlign: TextAlign.center, ); } } }, ) : _handlePreview(), ), floatingActionButton: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ Semantics( label: 'image_picker_example_from_gallery', child: FloatingActionButton.extended( key: const Key('image_picker_example_from_gallery'), onPressed: () { _isVideo = false; _onImageButtonPressed(ImageSource.gallery, context: context); }, heroTag: 'image0', tooltip: 'Pick image from gallery', label: const Text('Pick image from gallery'), icon: const Icon(Icons.photo), ), ), ], ), ); } ``` -------------------------------- ### Display picked media and handle retrieval in image_picker example Source: https://pub.dev/packages/image_picker/example This is a Dart code snippet from the image_picker package example. It shows how to display picked images or videos, handle errors, retrieve lost data on Android, and build the main UI with floating action buttons for picking from gallery. The code uses Image.network for web, Image.file for local files, and VideoPlayerController for videos. It also includes a FutureBuilder to retrieve lost data on Android and a switch on connection state to show appropriate messages. ```dart // Why network for web? // See https://pub.dev/packages/image_picker_for_web#limitations-on-the-web-platform return Semantics( label: 'image_picker_example_picked_image', child: kIsWeb ? Image.network(_mediaFileList![index].path) : (mime == null || mime.startsWith('image/') ? Image.file( File(_mediaFileList![index].path), errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) { return const Center( child: Text('This image type is not supported'), ); }, ) : _buildInlineVideoPlayer(index)), ); }, itemCount: _mediaFileList!.length, ), ); } else if (_pickImageError != null) { return Text('Pick image error: $_pickImageError', textAlign: TextAlign.center); } else { return const Text('You have not yet picked an image.', textAlign: TextAlign.center); } } Widget _buildInlineVideoPlayer(int index) { final controller = VideoPlayerController.file(File(_mediaFileList![index].path)); const volume = kIsWeb ? 0.0 : 1.0; controller.setVolume(volume); controller.initialize(); controller.setLooping(true); controller.play(); return Center(child: AspectRatioVideo(controller)); } Widget _handlePreview() { if (isVideo) { return _previewVideo(); } else { return _previewImages(); } } Future retrieveLostData() async { final LostDataResponse response = await _picker.retrieveLostData(); if (response.isEmpty) { return; } if (response.file != null) { if (response.type == RetrieveType.video) { isVideo = true; await _playVideo(response.file); } else { isVideo = false; setState(() { if (response.files == null) { _setImageFileListFromFile(response.file); } else { _mediaFileList = response.files; } }); } } else { _retrieveDataError = response.exception!.code; } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(widget.title!)), body: Center( child: !kIsWeb && defaultTargetPlatform == TargetPlatform.android ? FutureBuilder( future: retrieveLostData(), builder: (BuildContext context, AsyncSnapshot snapshot) { switch (snapshot.connectionState) { case ConnectionState.none: case ConnectionState.waiting: return const Text( 'You have not yet picked an image.', textAlign: TextAlign.center, ); case ConnectionState.done: return _handlePreview(); case ConnectionState.active: if (snapshot.hasError) { return Text( 'Pick image/video error: ${snapshot.error}}', textAlign: TextAlign.center, ); } else { return const Text( 'You have not yet picked an image.', textAlign: TextAlign.center, ); } } }, ) : _handlePreview(), ), floatingActionButton: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ Semantics( label: 'image_picker_example_from_gallery', child: FloatingActionButton( onPressed: () { isVideo = false; _onImageButtonPressed(ImageSource.gallery, context: context); }, 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, allowMultiple: true); }, heroTag: 'image1', tooltip: 'Pick multiple images', child: const Icon(Icons.photo_library), ), ), Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton( onPressed: () { isVideo = false; ``` -------------------------------- ### Get error widget for retrieval errors Source: https://pub.dev/packages/image_picker_linux/example Returns a Text widget if _retrieveDataError is set, then clears it. Used to display errors from image/video retrieval. ```dart Text? _getRetrieveErrorWidget() { if (_retrieveDataError != null) { final Text result = Text(_retrieveDataError!); _retrieveDataError = null; return result; } return null; } ``` -------------------------------- ### Handle image button press with picker methods Source: https://pub.dev/packages/image_picker/example Handles the image button press, branching on video, multiple, media, or single image selection. It mutes the video controller if present, checks context.mounted, and uses _displayPickImageDialog for image/media options. For video, it picks a single video with a 10-second max duration or multiple videos, then plays the first file. ```dart Future _onImageButtonPressed( ImageSource source, { required BuildContext context, bool allowMultiple = false, bool isMedia = false, }) async { if (_controller != null) { await _controller!.setVolume(0.0); } if (context.mounted) { if (isVideo) { final List files; if (allowMultiple) { files = await _picker.pickMultiVideo(); } else { final XFile? file = await _picker.pickVideo( source: source, maxDuration: const Duration(seconds: 10), ); files = [if (file != null) file]; } // Just play the first file, to keep the example simple. await _playVideo(files.firstOrNull); } else if (allowMultiple) { await _displayPickImageDialog(context, true, ( double? maxWidth, double? maxHeight, int? quality, int? limit, ) async { try { final List pickedFileList = isMedia ? await _picker.pickMultipleMedia( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, limit: limit, ) : await _picker.pickMultiImage( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, limit: limit, ); setState(() { _mediaFileList = pickedFileList; }); } catch (e) { setState(() { _pickImageError = e; }); } }); } else if (isMedia) { await _displayPickImageDialog(context, false, ( double? maxWidth, double? maxHeight, int? quality, int? limit, ) async { try { final pickedFileList = []; final XFile? media = await _picker.pickMedia( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ); if (media != null) { pickedFileList.add(media); setState(() { _mediaFileList = pickedFileList; }); } } catch (e) { setState(() { _pickImageError = e; }); } }); } else { await _displayPickImageDialog(context, false, ( double? maxWidth, double? maxHeight, int? quality, int? limit, ) async { try { final XFile? pickedFile = await _picker.pickImage( source: source, maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ); setState(() { _setImageFileListFromFile(pickedFile); }); } catch (e) { setState(() { _pickImageError = e; }); } }); } } } ``` -------------------------------- ### Set a cameraDelegate for desktop image_picker Source: https://pub.dev/packages/image_picker Defines a custom camera delegate for desktop platforms and sets it on the platform instance. Required to enable ImageSource.camera on Windows, macOS, and Linux, where no system camera UI exists. ```dart import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; // ··· class MyCameraDelegate extends ImagePickerCameraDelegate { @override Future takePhoto({ ImagePickerCameraDelegateOptions options = const ImagePickerCameraDelegateOptions(), }) async { return _takeAPhoto(options.preferredCameraDevice); } @override Future takeVideo({ ImagePickerCameraDelegateOptions options = const ImagePickerCameraDelegateOptions(), }) async { return _takeAVideo(options.preferredCameraDevice); } } // ··· void setUpCameraDelegate() { final ImagePickerPlatform instance = ImagePickerPlatform.instance; if (instance is CameraDelegatingImagePickerPlatform) { instance.cameraDelegate = MyCameraDelegate(); } } ``` -------------------------------- ### Handle image/video picking with ImagePicker options Source: https://pub.dev/packages/image_picker_windows/example Handles image/video picking with options for multiple selection, media type, and image quality. It first mutes the video controller if present, then branches on _isVideo, allowMultiple, and isMedia to call the appropriate picker method, shows a snackbar, and updates state. Errors are stored in _pickImageError. ```dart Future _onImageButtonPressed( ImageSource source, { required BuildContext context, bool allowMultiple = false, bool isMedia = false, }) async { if (_controller != null) { await _controller!.setVolume(0.0); } if (context.mounted) { if (_isVideo) { final List files; if (allowMultiple) { files = await _picker.getMultiVideoWithOptions(); } else { final XFile? file = await _picker.getVideo( source: source, maxDuration: const Duration(seconds: 10)); files = [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); } } else if (allowMultiple) { await _displayPickImageDialog(context, (double? maxWidth, double? maxHeight, int? quality) async { try { final ImageOptions imageOptions = ImageOptions( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ); final List pickedFileList = isMedia ? await _picker.getMedia( options: MediaOptions( allowMultiple: allowMultiple, imageOptions: imageOptions, ), ) : await _picker.getMultiImageWithOptions( options: MultiImagePickerOptions( imageOptions: imageOptions, ), ); if (pickedFileList.isNotEmpty && context.mounted) { _showPickedSnackBar(context, pickedFileList); } setState(() { _mediaFileList = pickedFileList; }); } catch (e) { setState(() { _pickImageError = e; }); } }); } else if (isMedia) { await _displayPickImageDialog(context, (double? maxWidth, double? maxHeight, int? quality) async { try { final List pickedFileList = []; final XFile? media = _firstOrNull(await _picker.getMedia( options: MediaOptions( allowMultiple: allowMultiple, imageOptions: ImageOptions( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, )), )); if (media != null) { pickedFileList.add(media); setState(() { _mediaFileList = pickedFileList; }); } } catch (e) { setState(() => _pickImageError = e); } }); } else { await _displayPickImageDialog(context, (double? maxWidth, double? maxHeight, int? quality) async { try { final XFile? pickedFile = await _picker.getImageFromSource( source: source, options: ImagePickerOptions( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ), ); if (pickedFile != null && context.mounted) { _showPickedSnackBar(context, [pickedFile]); } setState(() => _setImageFileListFromFile(pickedFile)); } catch (e) { setState(() => _pickImageError = e); } }); } } } ``` -------------------------------- ### Build video preview widget Source: https://pub.dev/packages/image_picker_android/example Builds the video preview widget, showing an error widget if retrieval failed, a placeholder text if no video is picked, or an AspectRatioVideo with padding when a controller exists. ```dart Widget _previewVideo() { final Text? retrieveError = _getRetrieveErrorWidget(); if (retrieveError != null) { return retrieveError; } if (_controller == null) { return const Text('You have not yet picked a video', textAlign: TextAlign.center); } return Padding(padding: const EdgeInsets.all(10.0), child: AspectRatioVideo(_controller)); } ``` -------------------------------- ### Handle image button press with image_picker options Source: https://pub.dev/packages/image_picker_android/example Handles the image button press, branching on video, multiple, media, or single image selection. Uses image_picker APIs like getMultiVideoWithOptions, getVideo, getMedia, getMultiImageWithOptions, and getImageFromSource, with options for maxWidth, maxHeight, imageQuality, and limit. Mutes the video controller before picking and shows a snack bar with picked files. ```dart Future _onImageButtonPressed( ImageSource source, { required BuildContext context, bool allowMultiple = false, bool isMedia = false, }) async { if (_controller != null) { await _controller!.setVolume(0.0); } if (context.mounted) { if (_isVideo) { final List files; if (allowMultiple) { files = await _picker.getMultiVideoWithOptions(); } else { final XFile? file = await _picker.getVideo( source: source, maxDuration: const Duration(seconds: 10), ); files = [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); } } else if (allowMultiple) { await _displayPickImageDialog(context, true, ( double? maxWidth, double? maxHeight, int? quality, int? limit, ) async { try { final imageOptions = ImageOptions( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ); final List pickedFileList = isMedia ? await _picker.getMedia( options: MediaOptions( allowMultiple: allowMultiple, imageOptions: imageOptions, limit: limit, ), ) : await _picker.getMultiImageWithOptions( options: MultiImagePickerOptions(imageOptions: imageOptions, limit: limit), ); if (pickedFileList.isNotEmpty && context.mounted) { _showPickedSnackBar(context, pickedFileList); } setState(() { _mediaFileList = pickedFileList; }); } catch (e) { setState(() { _pickImageError = e; }); } }); } else if (isMedia) { await _displayPickImageDialog(context, false, ( double? maxWidth, double? maxHeight, int? quality, int? limit, ) async { try { final pickedFileList = []; final XFile? media = _firstOrNull( await _picker.getMedia( options: MediaOptions( allowMultiple: allowMultiple, imageOptions: ImageOptions( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ), ), ), ); if (media != null) { pickedFileList.add(media); setState(() { _mediaFileList = pickedFileList; }); } } catch (e) { setState(() => _pickImageError = e); } }); } else { await _displayPickImageDialog(context, false, ( double? maxWidth, double? maxHeight, int? quality, int? limit, ) async { try { final XFile? pickedFile = await _picker.getImageFromSource( source: source, options: ImagePickerOptions( maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: quality, ), ); if (pickedFile != null && context.mounted) { _showPickedSnackBar(context, [pickedFile]); } setState(() => _setImageFileListFromFile(pickedFile)); } catch (e) { setState(() => _pickImageError = e); } }); } } } ``` -------------------------------- ### Build UI with image/video picker buttons Source: https://pub.dev/packages/image_picker_linux/example Builds a column of FloatingActionButtons for picking images and videos from gallery or camera. Uses _picker.supportsImageSource() to conditionally show camera options. Each button calls _onImageButtonPressed with the appropriate ImageSource and flags for video or multiple selection. ```dart icon: const Icon(Icons.photo_library_outlined), ), ), if (_picker.supportsImageSource(ImageSource.camera)) Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton.extended( onPressed: () { _isVideo = false; _onImageButtonPressed(ImageSource.camera, context: context); }, heroTag: 'image2', tooltip: 'Take a photo', label: const Text('Take a photo'), icon: const Icon(Icons.camera_alt), ), ), Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton.extended( backgroundColor: Colors.red, onPressed: () { _isVideo = true; _onImageButtonPressed(ImageSource.gallery, context: context); }, heroTag: 'video', 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: 'multiVideo', tooltip: 'Pick multiple videos', label: const Text('Pick multiple videos'), icon: const Icon(Icons.video_library), ), ), if (_picker.supportsImageSource(ImageSource.camera)) Padding( padding: const EdgeInsets.only(top: 16.0), child: FloatingActionButton.extended( backgroundColor: Colors.red, onPressed: () { _isVideo = true; _onImageButtonPressed(ImageSource.camera, context: context); }, heroTag: 'takeVideo', tooltip: 'Take a video', label: const Text('Take a video'), icon: const Icon(Icons.videocam), ), ), ], ), ); } ```