You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 01:24:02