有没有一种方法可以自动调整堆栈子对象的大小以适应其最大的同级对象? 也就是说,如果我有一个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'))
 ],
)

我试图让它与RowColumnExpanded一起工作,但遇到了无限约束的问题.

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

推荐答案

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

在Stack中,您应该将背景小部件包装在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')),
    ),
  ],
),

Flutter相关问答推荐

为什么在Flutter中使用firebase数据库时获取引用时会划掉引用?

无法将Flutter 连接到Firebase

带有可滚动页面(不包括分页区)的Flutter 页面视图

MobX Build_Runner问题:不生成.G文件(Flutter )

允许冻结在初始化时使用工厂

Flutter 蜂窝单元测试

更改BottomNavigationBar onTap?的大小

在Flutter 中每次点击按钮时都很难调用自定义函数

Flutter Android Google Sign-in error:ApiException:10在确认软件包名称、SHA-1 fingerprint 和设置同意页面后出现异常

尽管设置了对齐方式,如何对齐列中的行?

我无法找到修复此错误的位置,指出无法使用静态访问来访问实例成员

断言失败:std::move(hal_2_1_verifier).Run(). 初始化,LE音频客户端至少需要Bluetooth音频HAL V2.1

无法在 ListView (Flutter) 中限制 Card 的最大宽度

Flutter 3.10 - 'window' 已弃用,不应使用

Android 模拟器在 Apple M2 芯片 EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 上随机崩溃

ListView 后台可见

在不丢失游戏状态的情况下从 Flame 游戏导航到 Flutter 小部件

如何将 void 函数放入单独的 dart 文件的单独页面中

更改文本字段Flutter 中最大字母的 colored颜色

Flutter RawMaterialButton 常量相对大小