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

如何无需指定高度使用Flutter嵌套列表并解决ParentDataWidget错误

解决Flutter嵌套ListView无需指定高度且避免ParentDataWidget错误

你遇到的Incorrect use of ParentDataWidget错误,核心原因是在ListView.builder的item中错误使用了Expanded组件。Expanded需要配合Flex容器(如Column、Row)分配剩余空间,但嵌套的ListView已经通过shrinkWrap: true实现了根据内容自适应高度,此时Expanded会干扰ListView的布局逻辑,导致ParentData冲突。

关键修改方案

  • 移除所有嵌套在ListView.builder item里的Expanded组件
  • 保持内层ListView的shrinkWrap: true和physics: NeverScrollableScrollPhysics(),确保内层不独立滚动,由外层ListView统一处理
  • 最外层ListView可根据需求调整滚动物理效果,若需要Drawer整体滚动,可移除NeverScrollableScrollPhysics()

修改后的代码

return Drawer(
  child: Container(
    margin: const EdgeInsets.symmetric(vertical: 13, horizontal: 13),
    child: ListView.builder(
      // 若需要Drawer整体滚动,可移除下面的physics设置
      // physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemCount: catalogDrawer.length,
      itemBuilder: (BuildContext context, int index) {
        var one = catalogDrawer[index].one;
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              catalogDrawer[index].name,
              style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
            ),
            ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: catalogDrawer[index].one.length,
              itemBuilder: (BuildContext context, int i) {
                var two = catalogDrawer[index].one[i].two;
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      margin: const EdgeInsets.only(left: 10),
                      padding: const EdgeInsets.all(10),
                      color: Colors.yellow,
                      child: Text(
                        one[i].name,
                        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 15),
                      ),
                    ),
                    ListView.builder(
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: catalogDrawer[index].one[i].two.length,
                      itemBuilder: (BuildContext context, int a) {
                        return Container(
                          margin: const EdgeInsets.only(left: 30),
                          padding: const EdgeInsets.all(10),
                          color: Colors.red,
                          child: Text(
                            two[a].name,
                            style: TextStyle(fontWeight: FontWeight.w500, fontSize: 15),
                          ),
                        );
                      },
                    ),
                  ],
                );
              },
            ),
          ],
        );
      },
    ),
  ),
);

修改后,所有嵌套的ListView都会根据自身内容自动调整高度,无需手动指定固定值,同时彻底避免了ParentDataWidget错误。外层ListView负责整体滚动逻辑,内层ListView仅作为内容容器展示服务器返回的动态数据。

内容的提问来源于stack exchange,提问作者Daniil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 18:25:40