flutter - 如何将列宽设置为其子行中最长的列宽?

标签 flutter flutter-layout

我有一列,它的所有子项都是单词行(文本小部件),每行都有 MainAxisAlignment.spaceBetween 的主要对齐方式。我想将列的宽度设置为子项之间最长的行,其他行将相应地更改之间的间距。

我的代码:

Container(
  color: Colors.green,
  child: Column(
  children: [
    Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('A', style: Theme.of(context).textTheme.headline4), 
        Text('B', style: Theme.of(context).textTheme.headline4),
        Text('C', style: Theme.of(context).textTheme.headline4)
      ]),
     Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('A', style: Theme.of(context).textTheme.headline4), 
        Text('B', style: Theme.of(context).textTheme.headline4),
        Text('C', style: Theme.of(context).textTheme.headline4),
        Text('D', style: Theme.of(context).textTheme.headline4),
        Text('E', style: Theme.of(context).textTheme.headline4),
      ]),
  ]))

预期结果: enter image description here

实际结果:enter image description here

假设我无法在容器中将其设置为固定宽度,如何将列宽(动态)设置为最长的行?

最佳答案

将您的Column包裹在IntrinsicWidth中:

IntrinsicWidth(
  child: Column(
    children: [
      Row(...),
      Row(...),
    ],
  ),
)

关于flutter - 如何将列宽设置为其子行中最长的列宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66872488/

相关文章:

flutter - 如何在 flutter 中拉伸(stretch)容器以使其占据剩余的屏幕?

ios - flutter 适用于 iOS 的新版 Google Maps SDK 可用 : 4. 1.0.0

flutter - 为什么要扩大而不是扩大高度

flutter - Flutter 中的 AutoFocus Textfield 是如何工作的

flutter - 在 Flutter 的嵌套导航中输入焦点

添加第三方生成器时,Flutter build_runner 构建失败

key - 'MyhomePage({Key key, this.title}) : super(key: key);' in Flutter - what would be a clear explanation with an example?

flutter - 如何在 Flutter 中将具有不同大小的 TextSpan 子项沿中间对齐?

dart - 如何禁用复选框 flutter

flutter - 在小部件之间画一条线