【问题标题】:Column's MainAxisAlignment has no effect when nested in Column->RowColumn 的 MainAxisAlignment 嵌套在 Column->Row 中时没有效果
【发布时间】:2019-10-30 10:12:45
【问题描述】:

我正在尝试使用 Column 上的 MainAxisAlignment 属性,但效果并不理想。这是我的代码。

Scaffold(
  appBar: AppBar(
    title: Text('Account'),
  ),
  body: Container(
    child: Column(
      children: <Widget>[
        Card(
          margin: EdgeInsets.only(left: 0.0, right: 0.0, top: 0.0, bottom: 20.0),
          child: InkWell(
            onTap: () {},
            child: Padding(
              padding: EdgeInsets.all(8.0),
              child: Row(
                children: <Widget>[
                  Image.network(
                    'https://cdn.mydomain.com/Color/PNG/512/profile-512.png',
                    height: 72.0,
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround, // <<<=== this line
                    children: <Widget>[
                      Text('User\'s display name'),
                      Text('user@company.com'),
                    ],
                  )
                ],
              ),
            ),
          ),
        )
      ],
    ),
  ),
)

我的层次结构是 Container->Column->Card->InkWell->Row->Column。最里面的 Column 小部件有问题。我发现如果我删除第一个 Column 小部件,那么 MainAxisAlignment 就可以正常工作。

但我不能这样做,因为我想构建一个包含各种 Card 的 Column 布局,正如您在代码中看到的那样。

有人可以告诉我这是什么奇怪的行为以及解决方案吗?

问候。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    尝试将此列包装在 Container 或 SizedBox 中并为其设置 double.infinity 高度。

    发生的问题是因为 Column 或您未设置高度或宽度的任何组件会自动调整到其中的小部件。

                Container(
                  height: MediaQuery.of(context).size.height, //Add here
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Text('User\'s display name'),
                      Text('user@company.com'),
                    ],
                  ),
                ),
    

    OBS:我个人喜欢为此使用MediaQuery。 eg:MediaQuery.of(context).size.height 得到设备的全高,然后,你可以对其进行操作,除以 2、3、减...

    【讨论】:

    • 但我认为 Container 只包裹它的孩子,而不是 Column。如果我错了,请纠正我。因为如果我只使用一个 Container->Text 则屏幕的剩余空间将是空的,通过这个属性。但另一方面,如果我使用 Column->Text(在 children 属性中),则默认情况下 Column 将扩展为全高。
    • 是的,对不起,我已经告诉了宽度,但正确的是高度,因为您想在元素周围留出空间。我会更新的。
    猜你喜欢
    • 2021-12-21
    • 2018-01-31
    • 2020-12-29
    • 2015-08-10
    • 2022-01-06
    • 2014-07-26
    相关资源
    最近更新 更多