如何在卡片中紧紧包裹小部件列?

Rea*_*nkm 28 flutter

我有一个我的应用程序的页面,它只Card包含一个Column结尾的文件MaterialList.即使我的列表只有一个或两个项目,卡片也会将其垂直空间扩展到屏幕底部,而不是停留在列表项目的末尾.该列的文件表明,这是正常的行为列("布局中的每个孩子一零或零弹性系数(例如,那些不展开)与无界垂直约束").我认为将列表包装在一个Flexible可能达到我想要的效果但是当我尝试时我得到以下错误:

RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter (11469): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter (11469): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter (11469): flex on a child (e.g. using a Flexible) indicates that the child is to expand to fill the remaining
I/flutter (11469): space in the vertical direction.
I/flutter (11469): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter (11469): cannot simultaneously expand to fit its parent.
Run Code Online (Sandbox Code Playgroud)

我显然误解了这个布局应该如何工作.

以下是我现在作为Scaffold此页面正文的示例代码:

new Container(
// Outer container so we can set margins and padding for the cards
// that will hold the rest of the view.
alignment: FractionalOffset.topCenter,
margin: new EdgeInsets.only(top: style.wideMargin),
padding: new EdgeInsets.symmetric(horizontal: style.defaultMargin),
child: new Card(
  child: new Column(
    children: [
      new CustomHeader(), // <-- a Row with some buttons in it
      new Divider(),
      new MaterialList(
        type: MaterialListType.twoLine,
        children: listItems, // <-- a list of custom Rows
      ),
    ],
  ),
),
Run Code Online (Sandbox Code Playgroud)

)

我怎么能有一张紧紧包裹Column小部件的卡?

小智 59

默认情况下,Column展开以填充最大垂直空间.您可以通过将mainAxisSize属性设置为更改此行为MainAxisSize.min,这会导致Column仅占用其子项所需的最小垂直空间量.

  • 新版本中“Column”的行为似乎发生了变化。如果内部小部件是“ListView”,您可以通过将“shrinkWrap”设置为“true”来要求它仅占用最小空间。 (4认同)

jit*_*555 9

如果有人想缩小其列表,包装小部件可能会来拯救,这与@Adam 的回答完全相似

body: Container(
        child: Card(
          child: Wrap(
            direction: Axis.vertical,
            spacing: 10,
            children: <Widget>[
              Text('One'),
              Text('Two'),
              Text('Three'),
              Text('Four'),
            ],
          ),
        ),
      ), 
Run Code Online (Sandbox Code Playgroud)

但是,如果您为 Axis.vertical 和 Row 为 Axis.Horizo​​ntal 添加 Column,则此 Wrap 可以完成 Row 和 Column 的工作。您也可以为列和行中缺少的所有中间小部件添加相等的间距