When displaying images using the default Image widget, you might notice theysimply pop onto the screen as they’re loaded. This might feel visually jarringto your users.

Instead, wouldn’t it be nice if you could display a placeholder at first,and images would fade in as they’re loaded? You can use theFadeInImageWidget packaged with Flutter for exactly this purpose.

FadeInImage works with images of any type: in-memory, local assets, or imagesfrom the internet.

In-Memory

In this example, you’ll use thetransparent_imagepackage for a simple transparent placeholder.

  1. FadeInImage.memoryNetwork(
  2. placeholder: kTransparentImage,
  3. image: 'https://picsum.photos/250?image=9',
  4. );

Complete example

  1. import 'package:flutter/material.dart';
  2. import 'package:transparent_image/transparent_image.dart';
  3. void main() {
  4. runApp(MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. final title = 'Fade in images';
  10. return MaterialApp(
  11. title: title,
  12. home: Scaffold(
  13. appBar: AppBar(
  14. title: Text(title),
  15. ),
  16. body: Stack(
  17. children: <Widget>[
  18. Center(child: CircularProgressIndicator()),
  19. Center(
  20. child: FadeInImage.memoryNetwork(
  21. placeholder: kTransparentImage,
  22. image: 'https://picsum.photos/250?image=9',
  23. ),
  24. ),
  25. ],
  26. ),
  27. ),
  28. );
  29. }
  30. }

Fading In Image Demo

From asset bundle

You can also consider using local assets for placeholders. First, add the assetto the project’s pubspec.yaml file (for more details seeAssets and images):

  1. flutter:
  2. assets:
  3. + - assets/loading.gif

Then, use theFadeInImage.assetNetwork()constructor:

  1. FadeInImage.assetNetwork(
  2. placeholder: 'assets/loading.gif',
  3. image: 'https://picsum.photos/250?image=9',
  4. );

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. final title = 'Fade in images';
  9. return MaterialApp(
  10. title: title,
  11. home: Scaffold(
  12. appBar: AppBar(
  13. title: Text(title),
  14. ),
  15. body: Center(
  16. child: FadeInImage.assetNetwork(
  17. placeholder: 'assets/loading.gif',
  18. image: 'https://picsum.photos/250?image=9',
  19. ),
  20. ),
  21. ),
  22. );
  23. }
  24. }

Asset fade-in