如何将一列小部件紧紧包裹在卡片中?
How can I tightly wrap a Column of widgets inside a Card?
我的应用程序页面仅包含一个 Card
,其中包含一个 Column
,最后是一个 MaterialList
。即使我的列表只有一两个项目,卡片也会将其垂直 space 扩展到屏幕底部,而不是停在列表项目的末尾。 Column documentation 表明这是正常的列行为 ("Layout each child a null or zero flex factor (e.g., those that are not Expanded) with unbounded vertical constraints")。我认为将列表包装在 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
小部件的卡片?
默认情况下,Column
展开以填充最大垂直 space。您可以通过将 mainAxisSize
属性 设置为 MainAxisSize.min
来更改此行为,这会导致 Column
仅占用它需要的最小垂直量 space适合其 children.
如果有人想缩小列表,Wrap widget 可能会派上用场,这与
@亚当回答
body: Container(
child: Card(
child: Wrap(
direction: Axis.vertical,
spacing: 10,
children: <Widget>[
Text('One'),
Text('Two'),
Text('Three'),
Text('Four'),
],
),
),
),
但是如果您为 Axis.vertical 添加列并为 Axis.Horizontal 添加行,则此换行可以完成行和列的工作。 您还可以为列和行中缺少的所有中间小部件添加相等的间距
我的应用程序页面仅包含一个 Card
,其中包含一个 Column
,最后是一个 MaterialList
。即使我的列表只有一两个项目,卡片也会将其垂直 space 扩展到屏幕底部,而不是停在列表项目的末尾。 Column documentation 表明这是正常的列行为 ("Layout each child a null or zero flex factor (e.g., those that are not Expanded) with unbounded vertical constraints")。我认为将列表包装在 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
小部件的卡片?
默认情况下,Column
展开以填充最大垂直 space。您可以通过将 mainAxisSize
属性 设置为 MainAxisSize.min
来更改此行为,这会导致 Column
仅占用它需要的最小垂直量 space适合其 children.
如果有人想缩小列表,Wrap widget 可能会派上用场,这与 @亚当回答
body: Container(
child: Card(
child: Wrap(
direction: Axis.vertical,
spacing: 10,
children: <Widget>[
Text('One'),
Text('Two'),
Text('Three'),
Text('Four'),
],
),
),
),
但是如果您为 Axis.vertical 添加列并为 Axis.Horizontal 添加行,则此换行可以完成行和列的工作。 您还可以为列和行中缺少的所有中间小部件添加相等的间距