How to display image with zoom effect - flutter

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

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

how to change image opacity within Image() widget - flutter

So am trying to change image opacity using the opacity property i found in Image() widget, this is a short code to be clear :
Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(15),
child: Image(
image: NetworkImage(challengeImage),
fit: BoxFit.cover,
height: 150,
width: 350,
opacity: //not sure how to use
),
),
),
),
the image is a network image i retrieved from Firestore and am not sure what is the value i should put if its not double in order to apply opacity on the image. i thought to create a stack and insert asset image as a layer above my image and use the filter property but i would really like to know how the opacity can work with my code above, Thank you.
You can use AlwaysStoppedAnimation or create an Animation<double>
Image(
image: NetworkImage(""),
opacity: AlwaysStoppedAnimation(.1),
),
Another solution : wrap the widget with Opacity Widget
it accepts a double value in range [0,1] where 1 is fully displaying and 0 is not

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 do I show more or less of an element depending on window size?

I would like to have a banner widget, probably a VideoPlayer (from the video_player plugin) or else just an image.
Depending on the size of the screen/window I want my banner to follow like this:
https://i.imgur.com/YADZSrV.mp4
Imagine that the scaling in the video is the window size changing.
Basically:
If aspect gets wider than the original -> show less on top and bottom (kinda zooming in)
If aspect gets taller than the original -> show less on the sides (kinda cropping while centering)
I got something to work partially. It works when making the window wider, but when it gets slimmer it just starts to scale everything down, it doesn't keep the full height while showing less on the sides.
Here is my work in progress:
return ClipRect(
child: OverflowBox(
maxWidth: double.infinity,
maxHeight: double.infinity,
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: VideoPlayer(_controller),
),
),
);
This can be solved very easily by using the FittedBox widget and the BoxFit enum.
FittedBox(
fit: BoxFit.cover,
child: Container(
width: 960,
height: 360,
child: VideoPlayer(_controller),
),
)
Using the VideoPlayer widget as a child of a Container where I set the size to the original size of the video.
Example using an image:
FittedBox(
fit: BoxFit.cover,
child: Image(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
),
)
You can use LayoutBuilder to get the size of its content and render differently accordingly:
https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html
In you case, it would be something like this :
return ClipRect(
child: OverflowBox(
maxWidth: double.infinity,
maxHeight: double.infinity,
child: LayoutBuilder(
builder: (context, constraints) {
if (constaints.bigger.width > constaints.bigger.height) {
return Text('More width than height');
}
else {
return Text('More height than width');
}
}
)
),
);

how to cache image which don't affect UI and GPU performane

I want to cache an image with CachedNetworkImage. But because my network image has high quality, I face the issue that my list and grids are not rendered smoothly. So I test image.network to reduce image quality but unfortunately it has not faded in image animation.
So I want to reduce quality with fade in the image.
I am new to flutter and want to know which way is optimal to handle this field.
CachedNetworkImage(
fadeInDuration = const Duration(milliseconds: 400),
fit: BoxFit.cover,
imageUrl: i,
width: 1000,
),
Image.network(
i,
fit: BoxFit.cover,
filterQuality: FilterQuality.low,
width: 1000,
)
I want to cache my images while the user is in app and if the app is exited, clear the cached image.
Solution code:
CachedNetworkImage(
fadeInDuration : const Duration(milliseconds: 400),
fit: BoxFit.cover,
imageUrl: i,
width: 1000,
placeholder: (context, url) => new Image.asset('assets/images/placeholder.jpg'), // Your default image here
),
I don't know if this will be the best solution for your case but try the cached_network_image package.