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

Flutter中Row嵌套双ListView无显示及布局报错问题求助

问题分析与解决

错误信息(翻译后)

RenderBox未完成布局:RenderPointerListener#fb2be relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
第1966行第12位置断言失败: 'hasSize'

错误原因

这个错误核心是布局约束缺失:

  • 最外层多余的Row未提供明确高度约束,导致内部SingleChildScrollView无法确定自身尺寸
  • 嵌套的ListView未设置shrinkWrap: true,即便禁用了自身滚动(NeverScrollableScrollPhysics),仍会试图占用无限垂直空间,触发布局断言失败

修复后的代码

child: SingleChildScrollView(
  // 主滚动控制器,用于同步两个列表
  controller: _scrollController,
  // 监听滚动事件,同步第二个列表的滚动位置
  onScrollNotification: (notification) {
    if (notification is ScrollUpdateNotification) {
      _scrollController2.jumpTo(_scrollController.offset);
    }
    return false;
  },
  child: Row(
    children: [
      Expanded(
        child: ListView(
          physics: const NeverScrollableScrollPhysics(),
          controller: _scrollController2,
          shrinkWrap: true, // 关键:让ListView根据内容自适应高度
          children: your_history.map((item) {
            return Column(
              children: [
                Container(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  child: Column(
                    children: [
                      const Text("Sizning javobingiz:"),
                      Text(item),
                    ],
                  ),
                ),
                const Divider(color: Colors.black),
              ],
            );
          }).toList(),
        ),
      ),
      Expanded(
        child: ListView(
          physics: const NeverScrollableScrollPhysics(),
          controller: _scrollController,
          shrinkWrap: true, // 关键:让ListView根据内容自适应高度
          children: true_history.map((item) {
            return Column(
              children: [
                Container(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  child: Column(
                    children: [
                      const Text("To'g'ri javob:"),
                      Text(item),
                    ],
                  ),
                ),
                const Divider(color: Colors.black),
              ],
            );
          }).toList(),
        ),
      ),
    ],
  ),
)

关键修复点

  1. 移除冗余布局:删掉最外层多余的Row,让SingleChildScrollView直接作为父组件的子元素,确保它能获取正确的布局约束
  2. 约束ListView高度:给两个ListView添加shrinkWrap: true,让它们根据内容高度自适应,避免无限空间的布局冲突
  3. 实现同步滚动:通过监听主滚动控制器的滚动事件,调用第二个控制器的jumpTo方法,让两个列表保持滚动位置一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 17:17:36