Image in Flutter streched FittedBox - flutter

i m a beginner to flutter and I was trying to Implement some image inside a Container in my app.
Here is my code :
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: Container(
width: 200,
height: 200,
child: FittedBox(
child: Image.asset(
'assets/images/' + i.toString() + ".jpg"),
fit: BoxFit.fill,
),
),
),
My image showed up and it did filled the Container , but it looks horrible . It is super stretched , I tried changing the fit: BoxFit to something like fit : BoxFit.contain but it won't work.
Here is how my UI Looks :
The picture (Display properly but just stretched)
How do I make this image fit without stretching it.
Any answer would be appreciated , and thanks for helping.

When we use Boxfit.fill, the image will take up the complete space of parent container. Aspect ratio here would be secondary. That is why the image looks stretched.
Try Boxfit.cover. Another option would be to equal Container width to double.infinity and adjust the height, as needed.

BoxFit.fill will stretch the image to fill the space.
You should use BoxFit.cover to fill the space without stretching the image.
If you want to make sure the full width of the image is visible, use BoxFit.fitWidth.
If you want to make sure the full height of the image is visible, use BoxFit.fitHeight.

Related

Flutter - How to make SvgPicture.asset() take full screen inside of Slack with main parent Scaffold without Appbar

I want to make the SvgPicture take full width and height. Here is the current solution.
I have currently used double.infinity for width and height. However, I want to make it use the LayoutBuilder's constraints to make the image full width and height.
LayoutBuilder(
builder: (layoutBuilderContext, constraints) {
return Stack(
children: [
SvgPicture.asset(
'assets/images/welcome/bg-welcome-screen.svg',
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
]),
})
However, that is not able to happen using constraints.maxWidth and constraints.maxHeight for sizing the SvgPicture. It takes full height but failes to take full width.
How can I size the image to full width without using double.infinity if possible. I really want it to take the full screen width and height as it is a background image for the app.
To take full size of stack use Positioned.fill, And to size the Stack widget, wrap Stack with SizedBox
SizedBox(
width: constraints.maxWidth,
height:constraints.maxHeight,
child: Stack(
children: [
Positioned.fill(
child: Text(""),
),
],
),
),
It is better to use LayoutBuilder on top level widget like scaffold's body for getting body size.
More about Stack
Have you tried using BoxFit.fill? That will fill the target width and height by distorting the image's original aspect ratio.

Flutter place object on top of image

I try to recreate the following image in Flutter web, I understand how to do almost everything, except the profile box that is above the image,
How can I put an object on top of an image?
Use Stack to put widgets on each other ,if you want to put widget on specific position use Positioned
i suggest you to read this article
This is an example of placing object on the top of an image:
Stack(
children: [
// Background image placed in the center of Stack
Center(
child: Image.network(
'https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg'),
),
// Blue container 50x50 placed on the top of an image
Center(
child: Container(
width: 50,
height: 50,
color: Colors.blue
),
),
],
)

What is the best way to resize images to fit a certain area?

I am looking for a way to put random-sized network images into a fixed size area. The issue is the pictures can be random sizes. Is there a way to get images resized to fit a certain area? Any suggestions? I thought about expanded or aspect ratio but I don't really know how to take this approach.
This is my code so far:
child: Expanded(
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(7.0)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
child: Image.network('https://picsum.photos/250?image=9'),
)
],
),
),
),
Image(
fit: BoxFit.cover, //or BoxFit.fill
image: NetworkImage(
'https://picsum.photos/250?image=9',
),
),
With BoxFit you can fit your image to any size you want.
https://api.flutter.dev/flutter/widgets/FittedBox-class.html
If you want to set the borderRadius of a photo, you can use ClipRRect.
To make an Image fill its parent, simply wrap it into a FittedBox:
FittedBox(
child: Image.asset('foo.png'),
fit: BoxFit.fill,
)
FittedBox restricts its child widgets from growing its size beyond a
certain limit. It re-scales them according to the size available. For
instance, if the text is displayed inside a container, and the text is
to be entered by the user. If the user enters a large string of text,
then the container would grow beyond its allocated size. But, if we
wrap it with FittedBox, then it would fit the text according to the
size available. For large string, it would shrink its size, hence
would fit in the container.
Sytnax
FittedBox({
Key key,
BoxFit fit: BoxFit.contain,
AlignmentGeometry alignment: Alignment.center,
Widget child
}
)

Flutter grey lines around expanded

I am building a flutter web app, and I want to have a carousel slider widget with images. For this I use the carousel_pro package. I want the size to be the width of my carouselslider, which takes up the whole screen's width, and then set the size accordingly, to be the exact size needed. Is there a way to do this?
Now I am settling for a less option, which looks like this:
Flexible(
child: Container(
color: CustomColours.midBlue,
constraints: BoxConstraints(maxHeight: height / 2),
//height: height / 2,
width: width,
child: Carousel(
dotBgColor: Colors.transparent,
autoplayDuration: Duration(seconds: 5),
boxFit: BoxFit.cover,
images: [
AssetImage(
'lib/assets/create-esports-poster-design-in-48-hours.png'),
//AssetImage('lib/assets/Image from iOS.jpg'),
// AssetImage('lib/assets/banner.png'),
],
),
),
),
And I know I could not add a height for a container for it to work, but since this is in a column, it has to have a height. This solution would be good for me, however now the slider has grey lines on top and below it, like this: Is there a way to 1) do what I originally wanted or 2) fix this issue? Thank you very much!

How to dynamically change container size with text quantity in flutter?

I want to create a container in flutter which changes its height according to the amount of text present in it.
If there is more text, it should cover more height, if text is less, it should cover less height.
You can use fitted Box and width of container will help you to increase height dramatically.
FittedBox(fit: BoxFit.fill,
child: Container(width: 70,
child:
Text('hello '), color: Colors.red,
)
)