代码之家  ›  专栏  ›  技术社区  ›  TommyF

将堆栈大小颤振到同级

  •  5
  • TommyF  · 技术社区  · 6 年前

    是否有一种方法可以将堆栈子级自动调整为其最大的同级? 也就是说,如果我有 Stack 用一个 ListTile 还有一个 Container 最重要的是,我如何确保 集装箱 覆盖整个 列表平铺 是吗?

    例子:

    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'))
     ],
    )
    

    我试着让它和 Row ,请 Column Expanded 但是我遇到了无限约束的问题。

    有什么方法可以调整 集装箱 (或任何其他小部件,如 GestureDetector )到它在堆栈中最大的兄弟?

    1 回复  |  直到 6 年前
        1
  •  13
  •   nesbocaj    6 年前

    我也遇到了同样的问题,最后用这个答案解决了它:

    在堆栈中,您应该将背景小部件包装在一个positioned.fill中。

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

    -- Mary @ https://stackoverflow.com/a/45745479

    将其应用于您的问题将导致以下结果:

    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')),
        ),
      ],
    ),