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

Flutter:ListView嵌套SingleChildScrollView横向滚动报错求助

Flutter横向滚动嵌套AssertionError问题解决方法

问题原因

在shrinkWrap: true的ListView中嵌套横向滚动的SingleChildScrollView时,横向滚动组件缺少明确的高度约束,导致布局系统无法正确计算尺寸,触发AssertionError。

解决方法

给横向滚动的SingleChildScrollView包裹一个固定高度的容器(如Container或SizedBox),明确其高度约束,避免布局冲突。

修正后代码

ListView(
  shrinkWrap: true,
  children: [
    // 新增固定高度容器,解决约束问题
    Container(
      height: 150, // 可根据Card内容调整高度
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          children: [
            Card(
              child: Container(
                padding: const EdgeInsets.all(12),
                color: Colors.orange,
                width: 300,
                child: const Text('Hello'),
              ),
            ),
            Card(
              child: Container(
                padding: const EdgeInsets.all(12),
                color: Colors.orange,
                width: 300,
                child: const Text('Sample events'),
              ),
            ),
          ],
        ),
      ),
    ),
    //... 其他组件
  ],
);

补充说明

如果不想写死高度,也可以使用IntrinsicHeight让容器自适应子组件的高度,但性能略低于固定高度,适合内容高度不固定的场景:

IntrinsicHeight(
  child: SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: Row(...),
  ),
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 06:52:13