【问题标题】:Flutter MainAxisAlignment not working correctlyFlutter MainAxisAlignment 无法正常工作
【发布时间】:2021-05-17 20:39:24
【问题描述】:

我正在尝试在 Row 中使用 MainAxixsAlignment(spaceBetween)。 这是我的代码

ListView.separated(
                  shrinkWrap: true,
                  separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                    indent: 20,
                    endIndent: 20,
                  ),
                  itemCount: prayerTimes.length,
                  itemBuilder: (context, index) => Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Center(
                        child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          prayerTimes[index].name,
                          style: TextStyle(
                              color: Colors.teal,
                              fontWeight: FontWeight.bold,
                              fontSize: 18.0),
                        ),
                        Text(
                          prayerTimes[index].time,
                          style: TextStyle(color: Colors.teal, fontSize: 18.0),
                        ),
                        Icon(
                          Icons.volume_up,
                          color: Colors.teal,
                        )
                      ],
                    )),
                  ),
                )

结果:

如您所见,当第一行标题名称较大时,中间行中的时间未对齐。 如何使中间文本小部件与第一个和最后一个小部件对齐?

【问题讨论】:

  • 它实际上按预期工作,您需要将行中的所有 3 个元素包装在 Expanded 中,以便它们占用相等的空间。

标签: flutter listview widget


【解决方案1】:

将第一个文本包装在 Expanded 中并使用 MainAxisSize.max(删除对齐)。之后,您可以使用填充时间 Text 来设置一些间距,然后就完成了。

【讨论】:

  • 谢谢!我应该使用 flex(inside Expanded) 使我的图标向右吗?
  • 能否提供更新后的代码和截图?
猜你喜欢
  • 2021-08-22
  • 2021-09-04
  • 1970-01-01
  • 2021-06-11
  • 2018-09-27
  • 2021-05-28
  • 2020-08-14
  • 2021-07-14
  • 2019-07-29
相关资源
最近更新 更多