How to set Image fit on every devices in flutter - flutter

I am a beginner in Flutter and I just making a simple image carousel application using carosel_slider package. What I want is an image should look on all devices. Image width should be greater than height. The emulator shows fine, but not on my device. I have read this post, but still, I don't know how to do it.
AspectRatio(
aspectRatio: 16 / 9,
child: Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage(
"https://miro.medium.com/max/1200/1*ZMOh7JwiTAVVHF8e7sijNg.png"),
fit: BoxFit.cover,
),
),
),
),

Thanks to **
MediaQuery.of (context).size
**, you can get the size of the phone. The event you want to describe in the medium article is proportional by taking the page size instead of giving it a fixed height. this is the most logical thing anyway. But in the case of the image sometimes it may not eat. So you may need to use extra fix atribute.
Container(
alignment: Alignment.center,
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width * 0.6,
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage(
"https://miro.medium.com/max/1200/1*ZMOh7JwiTAVVHF8e7sijNg.png"),
fit: BoxFit.cover,
),
),

Related

Flutter. Image rounded borders not working

I'm trying to make rounded corners of image. Here is my code:
ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
"assets/images/test.png"
))
Everything works well, but when I try to fit the image into a container with a fixed height and width, the rounded borders stop working.
Here is my code:
LimitedBox(
maxWidth: 95,
maxHeight: 95,
child: ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
"assets/images/test.png"
),
),
)
Why is this happening, please help me.
Let's try with background image
Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
image: DecorationImage(
image: AssetImage(
'assets/images/test.jpg'),
fit: BoxFit.fill,
),
),
)
output:

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.

How to create a circular image without distortion in flutter

I need to create a circular image but when it is displayed in the container I use the photo is deformed and it shows very badly
Use this code
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent),
borderRadius: BorderRadius.circular(50),
image: DecorationImage(
image: FileImage(photoImage),
fit: BoxFit.fill,
),
),
)
I also provided this code
CircleAvatar(
radius: 40,
child: ClipOval(
child: Image(
height: 100,
fit: BoxFit.fill,
image: FileImage(photoImage),
),
),
)
I appreciate your help with this.
By very badly, I assume you're referring to the distorted aspect ratio of the image. You might want to use BoxFit.cover. BoxFit.fill is known to distort the image's aspect ratio as per the documentation
BoxFit.cover ,on the other hand maintains the aspect ratio while being small as possible.
I suggest use Circle avatar it is better
For asset images
CircleAvatar(backgroundImage: FileImage(photoImage), radius: 55.0)
CircleAvatar With network image
CircleAvatar(
backgroundImage:
NetworkImage('https://www.woolha.com/media/2020/03/eevee.png'),
minRadius: 50,
maxRadius: 75,
)

How can I add an opaque overlay over the image on box decoration in Flutter

I am struggling to come up with a solution to this seemingly simple task. I need an opaque overlay over an image in Flutter. I have tried to add the overlay different ways, but the image will always overlay on top of whatever I do. Mind you I need the opaque overlay over the image, but not over the title or text that sits on top of both the image and the overlay. Alternatively, I could have a black background and make the image opaque resulting possibly in the same effect that I want to achieve? Before I start to hack too much I would like to see how pros are doing it the way it should be done. Thank you all for your kind advices.
Container(
width: 320.0,
height: 180.0,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
news[index]['jetpack_featured_media_url'],
),
fit: BoxFit.cover,
),
color: Color(0xFF1F1F98),
gradient: LinearGradient(
begin: Alignment.topCenter,
colors: [Colors.black, Colors.black],
stops: [0.0, 0.5]
),
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(8.0),
topRight: const Radius.circular(8.0)
),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
cardTitle(news[index]['title']['rendered']),
style: kCardOverlayTitleTextStyle
)
) /* add child content here */,
),
You can use the ColorFilter option available in the DecoratedImage Widget
Container(
width: 320.0,
height: 180.0,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(news[index['jetpack_featured_media_url']),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.2),
BlendMode.darken
),
),
),
BlendMode.srcOver composite the source image over the destination image. So, if you want to add the overlay, this will be the option of choice. You can find more about this blend mode here
Here is the snippet that will work:
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: const AssetImage('../your-image.png'),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.red.withOpacity(0.8),
BlendMode.srcOver,
),
),
),
)