flutter - 如何将一列小部件紧紧包裹在卡片内?

标签 flutter

我的应用程序页面仅包含一个 Card,其中包含一个 Column,其末尾是一个 MaterialList。即使我的列表只有一两个项目,卡片也会将其垂直空间扩展到屏幕底部,而不是停在列表项目的末尾。 Column documentation表明这是正常的 Column 行为(“为每个子项布局一个 null 或零弹性因子(例如,那些未展开的弹性因子),具有无限的垂直约束”)。我认为将列表包装在 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 会扩展以填充最大垂直空间。您可以通过将 mainAxisSize 属性设置为 MainAxisSize.min 来更改此行为,这会导致 Column 仅占用最少的垂直空间它需要适合它的 child 。

关于flutter - 如何将一列小部件紧紧包裹在卡片内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41845693/

相关文章:

list - 范围错误(索引): Invalid value: Valid value range is empty: 0

flutter - 简单 Flutter 的 hello world 代码中的 LateInitializationError

flutter - flutter :在 Debug模式下构建时,运行pod安装将永远花费

flutter - 为什么Android在iOS上不能在TextField中显示正确格式的文本?

flutter - 如何在 TextButton 上设置最小宽度(不更改最小高度)

android - 如何使用 dart :ffi in Flutter? 打印到控制台

dart - 无法弄清楚如何在 Flutter 的多项选择测验中从 List<String> 分配正确的答案值,

flutter - 如何在Flutter中通过BloC模式在页面的加载(初始)中设置两个不同的列表

list - ListView 中一次只显示一个项目

webstorm - 无法与适用于 Web 的 Angular 和适用于移动项目的 Flutter 一起运行 Dart 2 - 计划共享代码