Sitemap

Image Picker in Flutter

3 min readSep 11, 2022

--

Image picker is one of the most used features in any application. Most famous applications including Facebook, Twitter, Instagram, Viber, Whatsapp e.t.c have an image picker that allows users to select the files from their device to use as a profile picture or share with friends.

The final app works as follows:

Video Tutorial

What is image_picker ?

Creating an image picker widget from scratch in Flutter would be tedious. Flutter consists of image_picker package for selecting images from device gallery or taking pictures from the camera.

The image_picker package exposes some helpful methods from the ImagePicker class it exports:

pickImage

XFile? image = await picker.pickImage(source:ImageSource.gallery);

The pickImage method opens the dialog to choose dialog and displays the mobile phone’s gallery to select the image. The source arg states where the image is to be picked from.

Get Flutter Junction’s stories in your inbox

Join Medium for free to get updates from this writer.

Here, the source is ImageSource.gallery , so that the image is selected from the user’s phone gallery.

XFile? image = await picker.pickImage(source: ImageSource.camera);

When the source is set to ImageSource.camera , it opens the camera and selects the image snapped by the user.

pickVideo

XFile? image = await picker.pickVideo(source:ImageSource.gallery);

This method opens a choose dialog to pick a video from the phone’s gallery. You would use the pickVideo method when you want a video to be picked either from the gallery or from the phone’s video camera.

pickMultiImage

List<XFile>? images = await picker.pickMultiImage(source: ImageSource.gallery);

pickMultiImage enables the user to pick multiple images. The arg source: ImageSource.gallery allows us to pick the images from the phone’s gallery.

List<XFile>? photos = await picker.pickMultiImage(source: ImageSource.camera);

Let’s Dive in

Press enter or click to view image in full size
Photo by Roberto Nickson on Unsplash

First, we need to add the image_picker package in the dependencies of pubsec.yaml file

dependencies:
flutter:
sdk: flutter
image_picker: ^0.8.5+3

Then we click the button we are going to open the dialog of the gallery to select the image.

Then we set the picked image in the grid view as follows:

When we pressed the cross icon we delete the image.

void dltImages(data) {
fileList.remove(data);
}

Let’s get connected

We can be friends. Find on Facebook, Linkedin, Github, Youtube, BuyMeACoffee, and Instagram.

Visit: Flutter Junction

Contribute: BuyMeACoffee

Conclusion

I hope this article is helpful to you and you learned new things. I have used various things in this article that might be new for some of you.

If you learned something new or want to suggest something then please let me know in the comment.

If you loved the article click on the 👏 icon which provides motivation on delivering to you all the new things.

Also, follow to get updated on exciting articles and projects.

Learning by sharing gives a great impact on the learning process and makes the community bigger and bigger.

Sharing is a magnet that attracts other enthusiasts toward you.

Hence, let’s take a small step toward making our learning community bigger.

Share this article with your friends or tweet about the article if you loved it.

Get full at:

--

--

Flutter Junction
Flutter Junction