How to Add a Fade In Effect for Images in Flutter

Images are important components of a mobile app. It’s a bit abnormal when images appear plainly in your app. In this Flutter tutorial, we discuss how to add a fade-in effect for images. We also see how to add placeholder when an image is loading.

FadeInImage widget is used for showing a placeholder as well as to create a Fade in effect when an image appears. It shows the placeholder image until the main image gets loaded. You can use the FadeInImage widget as given below.

FadeInImage(
  // here `bytes` is a Uint8List containing the bytes for the in-memory image
  placeholder: MemoryImage(bytes),
  image: NetworkImage('https://backend.example.com/image.png'),
)

Now, let’s go to Flutter image with a placeholder and fade in effect example.

First of all, create assets/images folders in the root project folder. Then put your placeholder image in the images folder. Update your pubspec.yaml with the image file details by adding the following line. Here, I added a loading gif image named loading.gif.

assets:
    - assets/images/loading.gif

And the following is the main code.

import 'package:flutter/material.dart';

class FadeinImageExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Tutorials',
      home: Main()
    );
  }
}

/// This is the stateless widget that the main application instantiates.
class Main extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Fade in image effect Example'),
      ),
      body: Center(child:FadeInImage.assetNetwork(
            placeholder: 'assets/images/loading.gif',
            image: 'https://cdn.pixabay.com/photo/2020/02/04/16/00/cheetah-4818603_960_720.jpg',
          ),
      ),
    );
  }
}

Following is the output of the Flutter example.

flutter image placeholder example

I hope you liked this Flutter tutorial.

1 thought on “How to Add a Fade In Effect for Images in Flutter”

  1. This is a good example thanks for sharing..

    And I couldn’t find any placeholder: ‘assets/images/loading.gif’, that you used. Is there link that you take it as a gif..

    Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *