Home » Flutter Images

Flutter Images

In this section, we are going to see how we can display images in Flutter. When you create an app in Flutter, it includes both code and assets (resources). An asset is a file, which is bundled and deployed with the app and is accessible at runtime. The asset can include static data, configuration files, icons, and images. The Flutter supports many image formats, such as JPEG, WebP, PNG, GIF, animated WebP/GIF, BMP, and WBMP.

Displaying images is the fundamental concept of most of the mobile apps. Flutter has an Image widget that allows displaying different types of images in the mobile application.

How to display the image in Flutter

To display an image in Flutter, do the following steps:

Step 1: First, we need to create a new folder inside the root of the Flutter project and named it assets. We can also give it any other name if you want.

Step 2: Next, inside this folder, add one image manually.

Step 3: Update the pubspec.yaml file. Suppose the image name is tablet.png, then pubspec.yaml file is:

If the assets folder contains more than one image, we can include it by specifying the directory name with the slash (/) character at the end.

Step 4: Finally, open themain.dart file and insert the following code.

Step 5: Now, run the app. You will get something like the screen below.

Flutter Images

Display images from the internet

Displaying images from the internet or network is very simple. Flutter provides a built-in method Image.network to work with images from a URL. The Image.network method also allows you to use some optional properties, such as height, width, color, fit, and many more. We can use the following syntax to display an image from the internet.

The Imag.Network gives one useful thing that supports animated gifs. We can use the following syntax for displaying gifs from the internet.

Let us understand how to display an image from the network with the following example:

Output

When you run the app in Android Emulator, the following screen appears. Here, you can see the image of the given url.

Flutter Images

Display Fade-In Images

When we display an image, it simply pops onto the screen as they are loaded. It does not assume useful between the users. To overcome this issue, the Image uses a FadeInImage widget that shows a placeholder image while the target image is loading, then fades in the new image when it loads. The FadeInImage can work with various types of images, such as local assets, in-memory, or images from the internet.

From asset bundle

Flutter also allows us to use local assets for placeholders. To use local assets, you need to add the asset in your project pubspec.yaml file.

Let us see the following example, which helps you to understand it more clearly. Open the main.dart file and insert the following code.

Output

Now, run the app, it will give the laptop image (placeholder) before the image displayed from given url.

Flutter Images Flutter Images

In-Memory

Let us understand it with the following example where FadeInImage works with In-Memory. Here, you must have to use a transparent_image package for transparent placeholder and update the dependencies of pubspec.yaml file as below:

Now, open the main.dart file and insert the following code. When you run the app, it will give a transparent image as a placeholder.


Next TopicFlutter Card

You may also like