Flutter Boxfit.cover image with slider animation - flutter

I have a Flutter project with a PageView with different Images, that fill the entire screen with the BoxFit.cover property. What I want to achieve is an automatically initial animation, that begins with setting the Image Widget property alignment to Alignment.centerLeft and then animates the image to Alignment.centerRight, e.g. the images moves from left to right, and then, as soon as the image reaches the Alignment.centerLeft final position, it automatically switches to the next PageView.
I tried wrapping the Image Widget with the AnimatedAlign, but then the Boxfit.cover does not work anymore.
I want an animation like this:
PageView(
children: [
Container(
child: Image(
// Here I want the animation to go from
// Alignment.centerLeft to Alignment.centerRight
// and then automatically switch to the next PageView
alignment: Alignment.centerLeft
fit: BoxFit.cover,
image: AssetImage("assets/image1.jpg"),
),
),
Container(
child: Image(
fit: BoxFit.cover,
image: AssetImage("assets/image2.jpg"),
),
),
Container(
child: Image(
fit: BoxFit.cover,
image: AssetImage("assets/image3.jpg"),
),
),
],
),

Related

Image gets shrinked when put inside Stack widget Flutter

My expected ui is to have the image occupy full width and have an text on top of it. So I an stack widget but it shrinks down and does not take complete width.
body: ListView(
children: [
Stack(
children: [
Container(
child: Image(
height:120.0,
image: AssetImage('images/business.jpg'),
),
),
Text("Business",style: TextStyle(fontSize: 30.0),)
],
)
])
First of all, I think there is no need for the ListView widget as far I know from looking at your code. Remove it and to achieve what you're looking for, add the below properties to the Image widget
fit: BoxFit.cover,
height: double.infinity,
width: double.infinity
so the final version should look like:
Image(
fit: BoxFit.cover,
height: double.infinity,
width: double.infinity
image: AssetImage('images/business.jpg'),
),

Flutter - Issue with Container size when using BoxDecoration

I'm trying to create color overlay for Image to make it a bit gray. I'm using the following code:
Container(
padding: EdgeInsets.only(bottom: 20.0),
decoration: new BoxDecoration(
color: const Color(0xff7c94b6),
image: new DecorationImage(
fit: BoxFit.fitWidth,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.2), BlendMode.dstATop),
image: new AssetImage("assets/sunset.jpg"),
),
),
)
However without a Container child, color overlay takes all the screen.
[Result is the following][1]
I expect the Image to be on Top with overlay on Image only
[like this but with overlay][2]
Any thoughts, what am I'm missing?
[1]: https://i.stack.imgur.com/ifPEu.png
[2]: https://i.stack.imgur.com/4P0Cb.png
The overlay is over the whole screen because the Container has no constraints and therefore the parent's constraints (which in your case probably the screen size) are being used. When Container has a child, it uses its constraints.
The Column is not really necessary but if you want to have the image stuck to the top I assume you want to add other stuff below.
Column(
children:[
Container(
child: Image.asset(
"assets/images/sunset.JPG",
colorBlendMode: BlendMode.dstATop,
color: Colors.black.withOpacity(0.2),
),
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
),
),
],
)

How to display a part of a big image?

I got an image that is landscape and want to use it on my phone (portrait).
I don't want to resize my image. I just want to use what I need based on my screen size and start from the middle of it. And if I want to start from the left, what would be the solution?
I am not sure I am clear, so I created a basic image to explain what I meant.
Thanks
UPDATE
Scaffold(
body: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image:
const AssetImage("assets/images/login_960x540.png"),
fit: BoxFit.cover,
// colorFilter: ColorFilter.mode(
// Colors.black.withOpacity(0.3), BlendMode.dstATop),
),
),
),
#David You can use FittedBox and Alignment for positioning background image, for example:
Container(
height: 100,
width: 100,
child: FittedBox(
alignment: Alignment.centerLeft, //Positioned in start
fit: BoxFit.fitHeight,
child: Image.asset('assets/image.png'), //If image 100x300, you will see just 100x100
),
);

Flutter: Stack ignores Image's alignment property

I have a grid of images and I want an delete icon on the top-right position of each image.
I used this code to achieve it:
return Stack(
alignment: Alignment.topRight,
fit: StackFit.expand,
children: <Widget>[
Image(
image: image,
fit: BoxFit.cover,
),
Icon(Icons.delete, color: Colors.white,),
],
);
What I got:
The problem is, as you can see, the delete icon is on the center of the image not the up-right even though I set Stack's alignment to Alignment.topRight. I know this happens because I set Stack's fit to StackFit.expand but if I remove it then Image's fit property will be ignored and I'll get this:
So what should I do if I want to keep my Images square and be able to move the Icon to borders?
You should set the alignment only for the icon, so wrap the icon in an Align widget and set its alignment.
return Stack(
fit: StackFit.expand,
children: <Widget>[
Image(
image: image,
fit: BoxFit.cover,
),
Align(
alignment: Alignment.topRight,
child: Icon(
Icons.delete,
color: Colors.white,
),
),
],
);

How to overlay a widget in Flutter with another widget, opacity set to multiply?

This is what I want to achieve:
I know that there's a colorBlendMode on the image widget, but it doesn't allow me to color a part of the image, just the whole. Is it possible to overlay color for a percentage of the image?
For color overlay, you could simply use Container with color that has opacity. Then if you put your image and Container in Stack you could get same result as you have.
EDIT:
If you want to use multiply blending only option that comes to my mind is take two picture one with colorFiler, second(cutted in half) without and put them together. But it's definitely not clean or somehow nice solution.
Code for similar result as your image:
Column(children: <Widget>[
Stack(children: <Widget>[
Image(
image: NetworkImage('https://picsum.photos/600?image=9'),
colorBlendMode: BlendMode.multiply,
color: Colors.green),
AspectRatio(
aspectRatio: 2 / 1,
child: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
fit: BoxFit.fitWidth,
alignment: FractionalOffset.topCenter,
image: NetworkImage(
'https://picsum.photos/600?image=9',
))))),
]),
Stack(children: <Widget>[
Image(
image: NetworkImage(
'https://www.solidbackgrounds.com/images/1920x1080/1920x1080-white-solid-color-background.jpg'),
height: 100,
fit: BoxFit.fitWidth,
width: MediaQuery.of(context).size.width,
colorBlendMode: BlendMode.multiply,
color: Colors.green),
Center(
child: Text('5',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 40)))
])
])
I would definitely consider looking into the Stack Widget, which allows you to overlay children on top of one another. You could have your base image on the bottom, and that filter just above it.