颤动堆栈大小到兄弟姐妹

Tom*_*myF 10 flutter

有没有办法将堆栈子项自动调整为最大的兄弟?也就是说,如果我有Stack一个ListTileContainer在上面,我该如何确保Container覆盖整个ListTile

例:

new Stack(children: <Widget>[
      new ListTile(
          leading: new AssetImage('foo.jpg'),
          title: new Text('Bar'),
          subtitle: new Text('yipeee'),
          isThreeLine: true,
      ),
      new Container(color: Colors.grey, child: new Text('foo'))
 ],
)
Run Code Online (Sandbox Code Playgroud)

我试图使它一起工作Row,ColumnExpanded而正在运行与无边界约束的问题.

有没有办法将Container(或任何其他小部件,如a GestureDetector)的大小调整为堆栈中最大的兄弟?

小智 26

我有同样的问题,最后设法用这个答案来解决它:

在Stack中,你应该将你的背景小部件包装在Positioned.fill中.

return new Stack(
  children: <Widget>[
    new Positioned.fill(
      child: background,
    ),
    foreground,
  ],
);
Run Code Online (Sandbox Code Playgroud)

- Mary @ /sf/answers/3202183561/

将其应用于您的问题会产生以下结果:

Stack(
  children: <Widget>[
    ListTile(
      leading: AssetImage('foo.jpg'),
      title: Text('Bar'),
      subtitle: Text('yipeee'),
      isThreeLine: true,
    ),
    Positioned.fill(
      child: Container(color: Colors.grey, child: Text('foo')),
    ),
  ],
),
Run Code Online (Sandbox Code Playgroud)

  • 只是为了澄清 - 应该包装在“Positioned.fill”中的小部件是应该根据其同级部件调整其大小的小部件。这是背景还是前景小部件取决于您的用例(引用的答案包含背景小部件,而此答案包含前景小部件)。 (2认同)