FadeInImage in CircleAvatar - flutter

How can I have a fade in effect in CircleAvatar?
Type of backgroundImage of CircleAvatar is ImageProvider and FadeInImage is not a ImageProvider

Use Clip react,
for image loading i use CachedNetworkImage(cached_network_image: 2.3.3)
ClipRRect(
borderRadius: BorderRadius.circular(25.0),
child: CachedNetworkImage(
fit: BoxFit.cover,
width: 50,
height: 50,
placeholder: (context, url) => Image.asset('loadder.gif'),
imageUrl:
"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRLe5PABjXc17cjIMOibECLM7ppDwMmiDg6Dw&usqp=CAU"),
),
this is user loader image or gif(example).
placeholder: (context, url) => Image.asset('loadder.gif'),
this is your main image(example).
imageUrl:"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRLe5PABjXc17cjIMOibECLM7ppDwMmiDg6Dw&usqp=CAU"),

Related

How can I show an asset Image while NetworkImage is loading

I am not able to show a circular loading indicator or an image while flutter is loading the image from the network.
image: DecorationImage(
image: NetworkImage(
Juegos_de_siempre[index].url_foto,
),
fit: BoxFit.fill,
),
Flutter's documentation suggested the usage of FadeInImage for the placeholder functionality. Here's a link to an example on Flutter's documentation.
You cannot use FadeInImage inside a DecorationImage, so you have to wrap it inside a Stack instead like shown in this thread.
Use CacheNetworkImage Package.Click here.
This is sample code.
CachedNetworkImage(
width: 100,
height: 100,
fit: BoxFit.cover,
imageUrl: imageUrl,
placeholder: (context, url) => const Center(
child: CupertinoActivityIndicator(),
), // replace with your asset image
errorWidget: (context, url, error) =>
const Icon(Icons.error),
),

Hero Animation for Network Image in Flutter

I am trying to add Hero animation for a Network Image.
The problem is, when I navigate from one screen to another the image loads again on second screen and so I am not able to see the animation.
After loading complete, if I repeat this navigation (from 1st screen to 2nd) this is working fine.
So the question is, How can I achieve this animation when I navigate for first time?
Error-Output:
Click Here
Code:
FirstScreen.dart
Hero(
tag: 'tag',
child: Image.network(
'https://...',
cacheHeight: 1080,
cacheWidth: 1080,
fit: BoxFit.none,
scale: 5,
),
),
secondScreen.dart
Hero(
tag: 'tag',
child: Image.network(
'https://...',
),
)
With CachedNetworkImage:
firstScreen.dart
Hero(
tag: tag
child: CachedNetworkImage(
imageUrl: 'url',
fit: BoxFit.scaleDown,
height: 200,
width: 200,
memCacheHeight: 1080,
memCacheWidth: 1080,
errorWidget: (context, url, error) => Image.asset('assets/..'),
),
)
secondScreen.dart
Hero(
tag: tag,
child: CachedNetworkImage(
imageUrl: 'url',
errorWidget: (context, url, error) => Image.asset('assets/...'),
),
),
create loading builder in the network image widget and use a placeholder instead of image when the image is not fully loaded something like this
Image.network(
imagePath,
// width: 100,
// height: 100,
loadingBuilder: (BuildContext context,
Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Container(
width: 100,
height: 100,
child: Center(
child: // PlaceHolderWidget()
),
);
},
// fit: BoxFit.none,
)

The return type 'AssetImage' isn't a 'void', as required by the closure's context

How can I add error detection for a Decorated Network image? Specifically I cannot use regular NetworkImage due to the limitations when using in combination with BoxDecoration and onError not supporting what I need (onError should support another image being returned via Asset/Network/Icon).
decoration: BoxDecoration(
image:
//NetworkImage but fallback to local image if http request fails:
DecorationImage(
image: NetworkImage(mediaUrl!),
fit: BoxFit.cover,
onError: (exception, stackTrace) {
return AssetImage('assets/images/placeholder.jpg');
}
)
),
You must use onError to log the error. See DecorationImage
onError: (Object exception, StackTrace? stackTrace) {
print('Image loading error: $exception, stack trace: $stackTrace');
}),
You can use FadeInImage to add a placeholder to an image
FadeInImage(
placeholder: MemoryImage('assets/images/placeholder.jpg'),
image: NetworkImage(mediaUrl!),
),
I was able to resolve my issue and keep the circle decoration by wrapping the decoration inside of a CachedNetworkImage which already contains a good error catching method
CachedNetworkImage(
imageUrl: userProfileImg!,
imageBuilder: (context, imageProvider) => Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider, fit: BoxFit.cover),
),
),
placeholder: (context, url) => new CircularProgressIndicator(backgroundColor: Colors.white,),
errorWidget: (context, exception, stackTrack) => Icon(Icons.person,color: Colors.grey,size: 35,),
),

Change opacity of image in CachedNetworkImage Flutter

Hello I have multiple images.
I want change the opacity of the image with the imageUrl.
My goal is that when user click on a image, it change his opacity.
My question is how to change the opacity while keeping the image ?
My CachedNetworkImage :
CachedNetworkImage(
imageUrl : _getImageUrl(),
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
fit: BoxFit.cover,
placeholder: (context, url) => const Center(child: CircularProgressIndicator()),
errorWidget: (context, exception, stacktrace)
{
return const Icon(Icons.warning);
},
)
You can wrap CachedNetworkImage with Opacity widget and provide opacity:x based on your need.
double _opacity =.3;
......
Opacity(
opacity: _opacity.
child:CachedNetworkImage(...)
More about Opacity.

How can I convert CachedNetworkImage to ImageProvider?

I want to show CachedNetworkImage in CircleAvatar Widget but backgroundImage parameter require ImageProvider.
Use CachedNetworkImageProvider
Creates an ImageProvider which loads an image from the url, using the
scale. When the image fails to load errorListener is called.
If what you want is just a circle-shaped CachedNetworkImage, you can use ClipOval:
ClipOval(
child: CachedNetworkImage(
width: 32,
height: 32,
fit: BoxFit.cover,
imageUrl: 'BLA.jpg',
placeholder: CircularProgressIndicator(),
),
),
please use imageBuilder from CachedNetworkImage
CachedNetworkImage(
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => new Icon(Icons.error),
fit: BoxFit.contain,
imageUrl: imagePath,
imageBuilder: (context, imageProvider) { // you can access to imageProvider
return CircleAvatar( // or any widget that use imageProvider like (PhotoView)
backgroundImage: imageProvider,
);
},
)
If your intention is to show the image inside a circular.
Use:
ClipRRect(borderRadius: BorderRadius.circular(100),
child: CachedNetworkImage(...))
If you want to convert the CachedNetworkImage into ImageProvider
Use:
CachedNetworkImageProvider(url)