Can't keep aspect ratio on items added to Row

1,426 views
Skip to first unread message

Mattias Månsson

unread,
May 18, 2019, 9:43:37 PM5/18/19
to Flutter Dev
Been struggling with this issue for days now, so really grateful for any help. I have created a custom widget, basically just a square image with border. I then have a Row in the top of my app where I want to add 3 or sometimes 5 of my square widgets. To support both cases, I just want to set the height of the Row (using a Container around it) and then let the custom widgets shrink or grow to fit while still of course keep their aspect ratio. I have tried endless combinations of Container, AspectRatio, Expanded, FittedBox, but either they overflow, get stretched to fill the width or shrink to nothing.

Here is my latest attempt (but with only one item), the image grows to keep its aspect ratio as I want, but the border
  Widget _buildCompBar(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
height: 100,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,

children: <Widget>[
Expanded(
child: AspectRatio(
aspectRatio: 1.0,
child:
Container(
decoration: new BoxDecoration(
border: Border.all(width: 2),
borderRadius:
new BorderRadius.all(Radius.circular(5.0)),
shape: BoxShape.rectangle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage("assets/images/mons/unit_icon_0000_0_0.png"),
))),
),
),
],
),
);
}
Enter code here...

Mattias Månsson

unread,
May 19, 2019, 1:57:42 PM5/19/19
to Flutter Dev
Sigh, I see now the code block cut off the end om my text and I can't find any way to edit it. Anyway, Ignore what I really meant was that with one item and this code, it gets stretched to fill the whole row and definitely loses its aspect ratio. What I really want is like in these two images. If 3 widgets, fill the height by growing and still keeping its shape and divide the empty space between them. Same with 5 or more, but if more than 5 it needs to shrink because of the width and then I also want them to keep their aspect ratio.

3row.png

5row.png

Mattias Månsson

unread,
May 19, 2019, 2:25:10 PM5/19/19
to Flutter Dev


Actually, it might be even better to show the real example. Here is the latest source and two screenshots of 3 respective 5 items. As you can see the 5 items looks good (but just because the width kinda matches the height times 5), while 3 are stretched. I can change the layout so that 3 looks fine (still square and with empty space in between them), but then the 5 overflows to the side. 

3rowpics.png

5rowpics.png



  Widget _buildCompBar(BuildContext context) {
   
return Container(
      padding
: EdgeInsets.all(10),
      height: 100,
      child: Row(
        mainAxisSize
: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: List<Widget>.filled(
           
widget._buildComp.length,
            Expanded(

              child
:
                  Container(
                      decoration
: new BoxDecoration(
                          border
: Border.all(width: 2),
                          borderRadius:
                             
new BorderRadius.all(Radius.circular(5.0)),
                          shape: BoxShape.rectangle,
                          image: new DecorationImage(
                            fit
: BoxFit.cover,
                            image: AssetImage(
                               
"assets/images/mons/unit_icon_0000_0_0.png"),
                          ))),
            )),
      ),
    );
  }

Mattias Månsson

unread,
May 19, 2019, 2:32:53 PM5/19/19
to Flutter Dev
Ah, and in the last example I removed the AspectRatio I had inside the Expanded, but it does exactly NOTHING. I'm pretty sure Expanded somehow overrides AspectRatio.

Mattias Månsson

unread,
May 19, 2019, 2:44:32 PM5/19/19
to Flutter Dev
And as another example, if I put them inside FittedBox with fit contain, the images disappear completely and just gets black from the border, I guess because they are just decoration. But I noticed that if I place the images as child instead, I get a thin white border between the image and the real border, so I would prefer it like this. 

3rowblack.png

5rowblack.png

  Widget _buildCompBar(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
height: 100,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: List<Widget>.filled(
widget._buildComp.length,
Expanded(
              child: FittedBox(
fit: BoxFit.contain,
            child: Container(
decoration: new BoxDecoration(
border: Border.all(width: 2),
borderRadius: new BorderRadius.all(Radius.circular(5.0)),
shape: BoxShape.rectangle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
"assets/images/mons/unit_icon_0000_0_0.png"),
))),
)),
),
      ),
);
}




Mattias Månsson

unread,
May 19, 2019, 3:10:43 PM5/19/19
to Flutter Dev
Actually, if I put the image as a child of the compontent with fittedbox, it behaves as I want it. But as I said before it gets a white border I really don't like. So maybe if the border issue can be fixed instead, I can go on like this.

3rowwhite.png

5rowwhite.png


Widget _buildCompBar(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
height: 100,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: List<Widget>.filled(
widget._buildComp.length,
Expanded(
child: FittedBox(
fit: BoxFit.contain,
child: Container(
decoration: new BoxDecoration(
border: Border.all(width: 2),
borderRadius: new BorderRadius.all(Radius.circular(5.0)),
shape: BoxShape.rectangle,
                      ),
child: Image.asset("assets/images/mons/unit_icon_0000_0_0.png"),
),
)),
),
),
);
}




Quentin S

unread,
Jun 9, 2020, 11:08:44 AM6/9/20
to Flutter Development (flutter-dev)
For everyone who needs to do the same, just wathc the official video of the widget.
A center or an align does the job
Reply all
Reply to author
Forward
0 new messages