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?
CodePudding user response:
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,
),
);
},
),
CodePudding user response:
Try using FadeInImage(), it has properties of placeHolder and errorPlaceHolder
CodePudding user response:
You can use cached_network_image package.
CachedNetworkImage(
placeholder: (context, url) => Container(color : Colors.red), // Add whatever you want to display.
imageUrl: url,
)
CodePudding user response:
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.
CodePudding user response:
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
)