Change opacity of image in CachedNetworkImage Flutter - 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.

Related

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,
)

Cached Network Image does not show progress indicator when image is loading

I am displaying an image using cachednetworkimage and when I click to view an image, it first shows the placeholder image and then loads the image. I don't understand why it does not show progress indicator when the image loads. I want to show progress indicator when the image loads. Incase the image is not available, then the placeholder image should be shown. What am I doing wrong here?
Container(
height: size.height * 0.35,
width: double.infinity,
child:_imageUrl != null
? CachedNetworkImage(
imageUrl: _imageUrl,
progressIndicatorBuilder: (context, url, downloadProgress) =>
Center(child:Loading()),
errorWidget: (context, url, error) => Icon(Icons.error),
)
:Image.asset('assets/images/placeholder.png'),
),
In progressIndicatorBuilder of cached network image, there's a parameter called downloadProgress, pass that value to CircularProgressIndicator to display loading.
Note: Replace NetowrkUrl with actual url
CachedNetworkImage(
height: 400.h,
imageUrl:"NetowrkUrl,
progressIndicatorBuilder: (context, url, downloadProgress) =>
Container(
margin: EdgeInsets.only(
top: 100.h,
bottom: 100.h
),
child: CircularProgressIndicator(
value: downloadProgress.progress,
color: AppColors.lightBlack)),
)
This behavior occurs because the first value of _imageUrl is null so it never goes into the CachedNetworkImage widget, it goes through the Image.asset(...) and then when the value stops being null, it goes into the CachedNetworkImage widget.
For this to work you must know the url (not null) before rendering the CacheImageNetwork widget.
An alternative solution is to replace Image.asset (...) with CircularProgressIndicator() to simulate the behavior.

How to display image with zoom effect

I have an original image like this, I want to display the image into an image widget on flutter UI, with a zoom center effect.
And I want the image widget display center and focus zoom in the center of the original image.
I used boxfit but don't have options for something like I wanted, I also use transform matrix scale, but it scales all the image widget, not the image content.
Please help me how to achieve it, thank you.
update 1:
I've used the code to achieve above requirement, but I don't know if anyone have better solution. My steps are transform scale with Transform.scale, then ClipRect the zoom to get the center.
int height = 250;
int zoom = 3;
return ClipRect(
child: OptimizedCacheImage(
imageUrl: imgUrl,
useScaleCacheManager: false,
filterQuality: FilterQuality.medium,
height: height,
fit: BoxFit.cover,
imageBuilder: (context, imageProvider) => Transform.scale(
scale: zoom,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: imageProvider,
),
))),
placeholder: (context, url) => SizedBox(height: height),
errorWidget: (context, url, error) => Icon(Icons.error),
),
);

FadeInImage in CircleAvatar

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"),

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)