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

Flutter水平ListView.builder内ListTile选中高亮布局报错

问题原因

水平方向滚动的ListView.builder在水平轴上是无界约束,而ListTile组件默认布局逻辑是撑满父组件分配的全部可用宽度,两者叠加后就会给内部文本组件传递无限宽度的无效约束,触发渲染异常。
你之前尝试用Expanded、Flexible包裹无效,是因为这两个组件仅能作为Flex(Row/Column)的直接子组件生效,无法解决水平滚动列表内部子组件的宽度无界问题。

两个报错的对应逻辑:

  • hasSize断言失败:子组件拿到无限宽度约束后无法计算自身合法尺寸,布局流程未完成就进入绘制阶段
  • BoxConstraints强制无限宽度异常:ListTile内部的Text组件拿到了w=Infinity的无效约束,无法完成文本排版
修复方法

给水平列表内的每个列表项设置明确的宽度约束即可,两种实现按需选择:

方案1:保留ListTile,给它指定明确宽度

适合需要用ListTile自带的leading、trailing等布局特性的场景,修改itemBuilder代码如下:

itemBuilder: (BuildContext ctxt, int index) {
  return Padding(
    padding: const EdgeInsets.all(8.0),
    // 给ListTile设置明确宽度
    child: SizedBox(
      width: 80, // 可根据设计需求调整宽度数值
      child: ListTile(
        title: Text(
          itemss[index],
          style: const TextStyle(
            color: Colors.white,
            fontFamily: 'eng',
            fontSize: 25,
            fontWeight: FontWeight.bold,
          ),
        ),
        tileColor: selectedIndex == index ? Colors.amber : null,
        onTap: () {
          setState(() {
            selectedIndex = index;
            currentYear = itemss[index]; // 同步更新存储的选中年份
          });
        },
      ),
    ),
  );
}

方案2:用轻量组件替代ListTile

你的场景只需要文本展示、点击回调、选中高亮,不需要ListTile的复杂布局特性,直接用GestureDetector+Container实现更简单,不会触发约束问题,性能更好:

itemBuilder: (BuildContext ctxt, int index) {
  return Padding(
    padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
    child: GestureDetector(
      onTap: () {
        setState(() {
          selectedIndex = index;
          currentYear = itemss[index];
        });
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
        decoration: BoxDecoration(
          color: selectedIndex == index ? Colors.amber : Colors.transparent,
          borderRadius: BorderRadius.circular(6),
        ),
        child: Text(
          itemss[index],
          style: const TextStyle(
            color: Colors.white,
            fontFamily: 'eng',
            fontSize: 25,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    ),
  );
}
补充说明
  • 所有放在水平滚动ListView里的子组件,都不能默认撑满父级宽度,必须给定明确的宽度约束,否则会重复触发同类无限宽度异常
  • 原代码中定义的currentYear变量没有在点击选中时同步更新,修复时记得补上赋值逻辑,否则选中值不会被正确存储

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 20:06:28