How to display a part of a big image? - flutter

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

Related

Problem I have space in listview between items (Container) with flutter

Problem I have space in listview between items (Container) with flutter
Problem I have space in listview between items (Container) with flutter
this code :
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView(
children: [
Container(
width: width,
height: 220,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/exclusion.png'),
// fit: BoxFit.contain,
),
),
),
Container(
width: width,
height: 220,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/exclusion.png'),
// fit: BoxFit.contain,
),
),
),
],
),
),
],
),
);
The height of the container you provided does not reflect the image height, so the image gets centered try to set fit: BoxFit.fill in the DecorationImage so that you see the actual height you need

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

Make background a scrollable png for flutter

I am making a flutter website, where on mobile I would want to have a png image that fits the width of the device, but it keeps the ratio of the image, and makes it scrollable. This is my code:
child: Stack(children: [
SingleChildScrollView(
child: Container(
color: Colors.red,
width: width,
height: height,
child: DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(mobileBackground),
fit: BoxFit.fitWidth),
)
//height: height,
),
),
),
This doesn't scroll though. Is there a way to fix this? Thanks.

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.