如果在另一个 Column 中的 Column 中使用 Spacer,则会出现 Flutter unbounded height 错误

Flutter unbounded height error if using Spacer in Column inside another Column

我卡住了...这是我分解的代码:

 Column(
    children: [
      Column(
        children: [
          Text('Whyy'),
          Spacer(),
          Text('crash?'),
        ],
      ),
      Text('ok'),
    ],
  ),

这会因错误而崩溃:

RenderFlex children have non-zero flex but incoming height constraints are unbounded.

When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction. These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.

但是为什么呢?为什么这会崩溃? Parent-ColumnScaffoldbody.

我在这里错过了什么?

如果您需要更多信息,请告诉我!

这是因为内栏对其高度没有任何限制,所以Spacer小部件可以取无限大space。您可以通过用 Expanded(或任何其他高度限制小部件,如 SizedBox)包装它来设置对内列高度的实际限制:

Column(
  children: [
    Expanded(
      child: Column(
        children: [
          Text('No'),
          Spacer(),
          Text('crash!'),
        ],
      ),
    ),
    Text('ok'),
  ],
),