I am using flutter and I am trying to change the aspect ratio of an image from 4:3 to 16:9. I have tried using the AspectRatio Widget and also using FittedBox but the image still remains 4:3
I have tried using AspectRatio, changing the fit prop on the Image to cover, fit, and contain
Card(elevation: 3.0, child: Column(
children: <Widget>[Container(child:
AspectRatio(aspectRatio: 16.0 / 9.0, child: FittedBox(fit:
BoxFit.contain,
child: Image(image: AssetImage('images/maggie.jpg')),),)
)],
Flutter Widget - AspectRatio() Flutter solves this by providing the AspectRatio widget. You give it an AspectRatio, a child, and, well, that's it. Aspect ratio is the ratio between the width and height of a box. It's often written as a fraction, like 3/2, as in three parts of width to two parts of height.
We will use AspectRatio() widget to achieve aspect ratio on the Image widget in Flutter.
To achieve this, you will need to apply the decoration property on Container . Since there are many type of decoration, we will use BoxDecoration and apply a DecorationImage with a BoxFit. cover.
You need to use BoxFit.fill
to see the effect, BoxFit.cover
shows same effect with image cropped. And you also don't need FittedBox
.
Card(
elevation: 3.0,
child: Column(
children: <Widget>[
Container(
child: AspectRatio(
aspectRatio: 16 / 9,
child: Image(
image: AssetImage('images/maggie.jpg'),
fit: BoxFit.fill, // use this
),
),
)
],
),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With