Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale elements in flutter Row to have the same aspect ratio

I want to display a list of elements in a row. The problem is that it is not scaled correctly. If I have six elements, it looks nice, but if the list contains only four elements, it doesn't look good. Can someone tell me what I'm doing wrong?

Code:

    Container(
                height: 100,
                margin: marginMediumHorizontal,
                decoration: decorationLight,
                alignment: Alignment.center,
                child: Row(
                  children: <Widget>[
                    ...model.gridListItems.map(
                      (element) => Expanded(
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 5, vertical: 5),
                          decoration: decorationDark,
                          padding: EdgeInsets.all(5),
                          child: Image(
                            color: lightGrayLimeGreen,
                            image: AssetImage(element['icon']),
                            fit: BoxFit.contain,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),

Thanks a lot for your help.

like image 919
NeverAsksQuestions Avatar asked Dec 05 '25 15:12

NeverAsksQuestions


1 Answers

Use Flexible widget instead of Expanded and give each widget as flex : 1 It'll do the trick itself.

Container(
            height: 100,
            margin: marginMediumHorizontal,
            decoration: decorationLight,
            alignment: Alignment.center,
            child: Row(
              children: <Widget>[
                ...model.gridListItems.map(
                  (element) => Flexible(
                    flex : 1,
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 5, vertical: 5),
                      decoration: decorationDark,
                      padding: EdgeInsets.all(5),
                      child: Image(
                        color: lightGrayLimeGreen,
                        image: AssetImage(element['icon']),
                        fit: BoxFit.contain,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
like image 163
Guru Prasad mohapatra Avatar answered Dec 08 '25 14:12

Guru Prasad mohapatra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!