首页 > 解决方案 > Flutter 表日历:CalendarController 问题

问题描述

所以根据表日历版本的更新日志。3.0.0,CalendarController被删除。我的代码上有它,现在它显示为错误Undefined class 'CalendarController'。现在,我想知道该怎么做,因为我在任何地方都找不到答案。另外,有人可以向我解释一下最初的用途是CalendarController什么?

任何帮助将不胜感激,谢谢!

标签: flutterflutter-dependencies

解决方案


就像变更日志所说的那样,CalendarController 在较新的版本中被删除。它用于突出显示的(当前选择的)日功能。

您可以通过将这些属性添加到TableCalender()来手动配置所选日期:

selectedDayPredicate: (day) {
      // Use `selectedDayPredicate` to determine which day is currently selected.
      // If this returns true, then `day` will be marked as selected.

      // Using `isSameDay` is recommended to disregard
      // the time-part of compared DateTime objects.
      return isSameDay(_selectedDay, day);
    },
    onDaySelected: (selectedDay, focusedDay) {
      if (!isSameDay(_selectedDay, selectedDay)) {
        // Call `setState()` when updating the selected day
        setState(() {
          _selectedDay = selectedDay;
          _focusedDay = focusedDay;
        });
      }
    },
    onFormatChanged: (format) {
      if (_calendarFormat != format) {
        // Call `setState()` when updating calendar format
        setState(() {
          _calendarFormat = format;
        });
      }
    },
    onPageChanged: (focusedDay) {
      // No need to call `setState()` here
      _focusedDay = focusedDay;
    },

还要确保定义firstDay:、lastDay: 和 focusDay:,因为它是 TableCalendar() 函数所必需的,并且还要设置 calendarFormat。

参考官方 table_calendar 示例:https ://github.com/aleksanderwozniak/table_calendar/blob/master/example/lib/pages/basics_example.dart


推荐阅读