SfCalendar周视图设置numberOfDaysInView=1时触发重复GlobalKey错误
解决SfCalendar周视图设置
numberOfDaysInView:1时的重复GlobalKey错误 问题场景
使用Syncfusion的SfCalendar组件,将视图设置为CalendarView.week,同时在TimeSlotViewSettings中配置numberOfDaysInView: 1时,触发Duplicate GlobalKey detected in widget tree错误,注释掉numberOfDaysInView这行配置后错误消失。
解决方案
不要在周视图(CalendarView.week)下设置numberOfDaysInView:1,如果需要展示单日的时间槽视图,直接切换为CalendarView.day视图即可——这是组件设计的预期用法,能彻底避免重复GlobalKey的问题。
原因分析
SfCalendar的周视图和日视图内部组件结构不同,周视图默认按一周天数渲染子组件,强行通过numberOfDaysInView:1限制显示天数,会导致组件内部重复创建使用相同GlobalKey的子元素,触发全局Key冲突。numberOfDaysInView参数的设计初衷更适配月视图的时间槽模式,而非周视图场景。
修正后的代码示例
SfCalendar( view: CalendarView.day, // 替换为日视图 timeSlotViewSettings: TimeSlotViewSettings( timeInterval: const Duration(minutes: 30), timeIntervalHeight: 90, timeRulerSize: 82, timeFormat: 'jm', timeTextStyle: const TextStyle( fontFamily: 'Source Sans Pro', fontStyle: FontStyle.normal, fontWeight: FontWeight.w400, fontSize: 14, color: Color(0xff868e96), ), nonWorkingDays: const <int>[ DateTime.saturday, DateTime.sunday ], ), // 其他日历配置项... )
内容的提问来源于stack exchange,提问作者Phạm Hữu Phúc
相关产品推荐
相关产品推荐

