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

时间:2017-01-25 07:25:33

标签: flutter

我的应用页面只包含一个Card,其中包含Column,其末尾为MaterialList。即使我的列表只有一个或两个项目,卡片也会将其垂直空间扩展到屏幕底部,而不是停留在列表项目的末尾。 Column documentation表明这是正常的列行为(“将每个子行为布局为零或零弯曲因子(例如,那些未扩展的行)具有无界垂直约束”)。我认为将列表包装在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.

我显然误解了这种布局应该如何工作。

以下是我现在作为此页面的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
      ),
    ],
  ),
),

我如何拥有一张紧密包裹Column小部件的卡?

2 个答案:

答案 0 :(得分:44)

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

答案 1 :(得分:5)

如果有人想缩小其列表,Wrap小部件可能会抢救出来,这与 @Adam答案

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

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