Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Aspect Ratio of Image in Flutter

Tags:

flutter

dart

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')),),)
                                   )],
like image 664
Chris Matthews Avatar asked Jul 21 '19 04:07

Chris Matthews


People also ask

How do I add aspect ratio in flutter?

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.

How do I find the aspect ratio of my Image flutter?

We will use AspectRatio() widget to achieve aspect ratio on the Image widget in Flutter.

How do I fit my screen Image 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.


1 Answers

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
          ),
        ),
      )
    ],
  ),
)
like image 89
CopsOnRoad Avatar answered Oct 04 '22 12:10

CopsOnRoad