Flutter Stack内使用Expanded出现Each child must be laid out exactly once报错
错误原因
Incorrect use of ParentDataWidget触发原因是Expanded仅可作为Row、Column、Flex类组件的直接子组件使用,你当前将Expanded嵌套在Stack的子组件Positioned中,违反了组件的父级约束规则。Each child must be laid out exactly once是前者引发的连锁布局异常,错误的嵌套导致Stack无法正常完成子节点的布局计算流程。
解决方案
直接删除 Positioned 下包裹内容区域的多余 Expanded 组件即可,你已经通过 Positioned 的参数明确指定了内容区域的尺寸约束,不需要 Expanded 额外拉伸。如果需要避免内容被底部面板遮挡,可以给内容组件加对应底部面板高度的内边距,或是调整内容区域 Positioned 的 bottom 参数为底部面板的高度。
修正后代码
Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.transparent, body: Stack( children: [ Positioned( left: 0, right: 0, top: 0, height: MediaQuery.of(context).size.height, // 移除了多余的Expanded child: child, ), const Positioned( left: 0, right: 0, bottom: 0, child: BottomPanel(), ), ], ), ); }
效果示意图

内容的提问来源于stack exchange,提问作者Alex Aung
相关产品推荐
相关产品推荐

