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

如何自定义Syncfusion Flutter Calendar日视图下的空白单元格?

自定义Syncfusion Flutter Calendar Day视图空白单元格

要修改Day视图下空白日期/时间单元格的背景色、边框样式等,不能仅依赖SfCalendar的基础属性,需要通过timeSlotViewSettings搭配timeSlotViewBuilder来实现自定义渲染,具体方案如下:

核心实现

SfCalendar(
  view: CalendarView.day,
  timeSlotViewSettings: TimeSlotViewSettings(
    timeSlotViewBuilder: (context, dateTime, startTime, endTime, timeText) {
      // 判断当前单元格是否为空白(无预约)
      final isEmptyCell = !_hasAppointmentInRange(startTime, endTime);
      
      return Container(
        decoration: BoxDecoration(
          // 空白单元格背景色
          color: isEmptyCell ? Colors.grey[100] : Colors.transparent,
          // 自定义边框样式
          border: Border(
            bottom: BorderSide(
              color: isEmptyCell ? Colors.blueAccent.withOpacity(0.3) : Colors.grey.shade300,
              width: 1,
            ),
          ),
        ),
        padding: const EdgeInsets.only(left: 8),
        alignment: Alignment.centerLeft,
        child: Text(timeText),
      );
    },
  ),
  dataSource: _calendarDataSource, // 你的预约数据源
)

辅助判断方法

需要实现一个方法来检查指定时间区间内是否存在预约,以此区分空白单元格:

bool _hasAppointmentInRange(DateTime startTime, DateTime endTime) {
  return _calendarDataSource.appointments?.any((appt) {
    // 判断预约时间与当前单元格时间是否重叠
    return appt.startTime.isBefore(endTime) && appt.endTime.isAfter(startTime);
  }) ?? false;
}

顶部日期栏空白单元格调整

如果需要修改Day视图顶部的日期头部空白样式,可通过viewHeaderStyle配置:

viewHeaderStyle: ViewHeaderStyle(
  backgroundColor: Colors.lightBlue[50],
  borderColor: Colors.blueAccent,
  borderWidth: 1,
),

说明

timeSlotViewBuilder会遍历Day视图的每一个时间单元格,你可以根据单元格的时间范围灵活判断是否为空白状态,进而完全自定义其装饰样式、内容布局,这是官方推荐的空白单元格定制方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 02:05:20