FadeInImage widget and Attributes

FadeInImage widget and Attributes

ยท

2 min read

The FadeInImage widget in Flutter is used to display images with a fading effect when loading. It's commonly used to improve the user experience by providing a smooth transition as images load from the network.

Attributes:

  1. placeholder (Widget):

    • The widget to be displayed while the image is loading. This can be a placeholder image or any other widget.
  2. image (ImageProvider):

    • The image to be displayed once it's loaded. It can be an AssetImage, NetworkImage, or any other ImageProvider.
  3. fit (BoxFit):

    • How the image should be inscribed into the space allocated for the FadeInImage. The default value is BoxFit.contain.
  4. alignment (AlignmentGeometry):

    • The alignment of the image within its parent widget. The default value is Alignment.center.
  5. placeholderErrorBuilder (Widget Function(BuildContext, Object)):

    • An optional builder function that creates a widget to be displayed if an error occurs while loading the image.
  6. imageErrorBuilder (Widget Function(BuildContext, Object)):

    • An optional builder function that creates a widget to be displayed if an error occurs while loading the image.
  7. fadeDuration (Duration):

    • The duration of the fading effect when transitioning from the placeholder to the image. The default value is 300 milliseconds.

Example:

import 'package:flutter/material.dart';

class FadeInImageExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FadeInImage Widget Example'),
      ),
      body: Center(
        child: FadeInImage(
          placeholder: AssetImage('assets/placeholder.png'),
          image: NetworkImage('https://via.placeholder.com/300'),
          fit: BoxFit.cover,
          width: 300,
          height: 300,
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: FadeInImageExample(),
  ));
}

Explanation:

  • In this example, a FadeInImage widget is used to display an image with a fading effect when loading.

  • The placeholder attribute is set to an AssetImage for a placeholder image while the network image is loading.

  • The image attribute is set to a NetworkImage to load the image from the network.

  • The fit attribute is set to BoxFit.cover to make sure the image covers the entire space allocated for the FadeInImage.

  • The width and height attributes are set to 300 pixels each to specify the dimensions of the FadeInImage.

  • When you run this code, you'll see the placeholder image fading out smoothly as the network image loads in its place.

Did you find this article valuable?

Support Vinit Mepani by becoming a sponsor. Any amount is appreciated!

ย