在Flutter中,如何调整Row中的元素大小比例?

3

我想要在一行中展示一个元素列表。问题是当元素数量不够时,它的比例变得不正确。如果我有六个元素,看起来很好,但是如果列表只包含四个元素,它就看起来不好了。请问有人可以告诉我我做错了什么吗?

代码:

    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,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),

非常感谢您的帮助。

你能添加marginMediumHorizontal和decorationLight吗?请说明你使用的模型类型。 - Jitesh Mohite
1个回答

2

使用Flexible小部件代替Expanded,并将每个小部件设置为flex : 1。它会自己完成。

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,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),

你非常受欢迎。 - Guru Prasad mohapatra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接