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

Flutter:Table Calendar嵌入ExpansionTile导致ListView无法滚动

解决TableCalendar区域无法滚动ListView的问题

问题根源是TableCalendar默认会拦截垂直方向的触摸手势,导致ListView无法响应该区域的滚动操作。以下是两种可行的修复方案:

方法一:禁用TableCalendar的滚动物理效果

给TableCalendar添加physics属性,设置为NeverScrollableScrollPhysics(),让它不再处理垂直滚动手势,将手势交还给父级ListView:

Expanded(
  child: ListView.builder(
      itemCount: map.keys.toList().length,
      itemBuilder: (context, index) {
        return ExpansionTile(
          title: Text(map.keys.toList()[index]),
          children: [
            Container(
              height: 400,
              child: TableCalendar(
                focusedDay: DateTime.now(),
                firstDay: DateTime(DateTime.now().year, DateTime.now().month, 1),
                lastDay: DateTime(DateTime.now().year, DateTime.now().month + 1, 2),
                // 添加该行禁用自身滚动
                physics: const NeverScrollableScrollPhysics(),
              ),
            )
          ],
        );
      }),
)

方法二:用GestureDetector透传垂直手势

如果需要保留TableCalendar的日期点击等交互,可通过GestureDetector包裹容器,设置behavior: HitTestBehavior.translucent让垂直滚动手势透传给父ListView:

Expanded(
  child: ListView.builder(
      itemCount: map.keys.toList().length,
      itemBuilder: (context, index) {
        return ExpansionTile(
          title: Text(map.keys.toList()[index]),
          children: [
            GestureDetector(
              behavior: HitTestBehavior.translucent,
              onVerticalDragUpdate: (_) {}, // 空实现确保手势透传
              child: Container(
                height: 400,
                child: TableCalendar(
                  focusedDay: DateTime.now(),
                  firstDay: DateTime(DateTime.now().year, DateTime.now().month, 1),
                  lastDay: DateTime(DateTime.now().year, DateTime.now().month + 1, 2),
                ),
              ),
            )
          ],
        );
      }),
)

推荐优先使用第一种方法,实现更直接高效,且不会影响TableCalendar的核心交互功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:46:01