Add placeholder to a network image in flutter

I have many network images that are being displayed in my app.

When these images are being loaded... Nothing shows on the screen (the screen remains blank for about 3-5 seconds, time for the image to be displayed)

How to show something like a loader or animation while the image is being loaded?

Upvotes: 1

Views: 1566

Answers (5)

Sabahat Hussain Qureshi
Sabahat Hussain Qureshi

Reputation: 1364

Use Image.network build in property

    Image.network(
      "https://www.kindpng.com/imgv/ioJmwwJ_dummy-profile-image-jpg-hd-png-download/",
      loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent loadingProgress) {
        if (loadingProgress == null) return child;
          return Center(
            child: CircularProgressIndicator(
              value: loadingProgress.expectedTotalBytes != null
                ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes
                : null,
           ),
         );
       },
     ),

Upvotes: 4

Ravin Laheri
Ravin Laheri

Reputation: 822

use this package https://pub.dev/packages/cached_network_image

Widget getNetworkImage({
  double? height = 45.0,
  double? width = 45.0,
  double? borderRadius = 10.0,
  bool? showCircularProgressIndicator, //<== this is for loader
  required String url,
}) {
  return ClipRRect(
    borderRadius: BorderRadius.circular(borderRadius ?? 0.0),
    child: SizedBox(
      height: height,
      width: width,
      child: CachedNetworkImage(
        fit: BoxFit.fill,
        imageUrl: url,
        placeholder: (context, url) {
          showCircularProgressIndicator ??= true;
          if (showCircularProgressIndicator == true) {
            return Center(
                child: Container(
              padding: const EdgeInsets.all(7),
              decoration: BoxDecoration(color: const Color(0xff0C59EA).withOpacity(0.35), borderRadius: BorderRadius.circular(10)),
            ));
          } else {
            return const Center(child: SizedBox());
          }
        },
        errorWidget: (context, url, error) { //<== if you get error in image
          return Image.asset(((height ?? 45) >= 100 || (width ?? 45.0) >= 100) ? Assets.imagesCommonPlaceholder : Assets.imagesLogoPlaceholder, fit: BoxFit.cover);//<== use your app placeholder or logo for better UX.
        },
      ),
    ),
  );
}

try using this way.

 getNetworkImage(
      url: "your-image-url-here",
      borderRadius: 15, //<== if you like to give border radius
      height: 60, //<== if you like to give height
     width: 60, //<== if you like to give width
   )

Upvotes: 1

Kamran Amin
Kamran Amin

Reputation: 31

class CachedImage extends StatelessWidget {
final String image;
final double radius;
final double imageWidth;
final double imageHeight;
final double imageArea;
final BoxFit fit;
final Color color;
final String placeHolder;
final double width;
CachedImage({
this.color = colorPrimary,
this.image = '',
this.radius = 30,
this.imageHeight = 70,
this.imageWidth = 70,
this.imageArea = 30,
this.fit = BoxFit.cover,
this.placeHolder = IMG_PLACEHOLDER,
this.width = 1.0,
});

@override
Widget build(BuildContext context) {
return Container(
  width: imageWidth,
  height: imageHeight,
  decoration: BoxDecoration(
    border: Border.all(
      color: color,
      width: width,
    ),
    borderRadius: BorderRadius.circular(radius),
  ),
  child: ClipRRect(
    borderRadius: BorderRadius.circular(radius),
    child: CachedNetworkImage(
        placeholder: (context, url) => Container(
              child: Center(
                child: CircularProgressIndicator(
                  valueColor: new AlwaysStoppedAnimation<Color> 
         (colorWhite),
                ),
              ),
              height: imageArea,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(10.0))),
            ),
        errorWidget: (context, url, error) => Material(
              child: Image.asset(
                placeHolder,
                width: imageWidth,
                height: imageHeight,
                fit: BoxFit.fill,
              ),
              borderRadius: BorderRadius.all(Radius.circular(20.0)),
              clipBehavior: Clip.hardEdge,
            ),
        imageUrl: image,
        width: imageWidth,
        height: imageHeight,
        fit: fit),
   ),
  );
 }
}

You can use this custom widget created using CachedNetworkImage package. In which in case the image is not loaded from network we are using CircularProgressBarIndicator while in case of error that the network image is not loaded we can use error widget and can pass an alternative image from assets.

Upvotes: 1

Yashraj
Yashraj

Reputation: 999

You can use cached_network_image package.

CachedNetworkImage(
        placeholder: (context, url) => Container(color : Colors.red), // Add whatever you want to display.
        imageUrl: url,
      )

Upvotes: 1

AlishanMuhammadAmin
AlishanMuhammadAmin

Reputation: 310

Try using FadeInImage(), it has properties of placeHolder and errorPlaceHolder

Upvotes: 2

Related Questions