1

I'm using table_calendar from Flutter and when I select a day in another month, the calendar resets it's view to the current month again. I don't want this to happen.

Example: Today is 28 of March. I go to the May month and select a day there. Now, the calendar view goes back to March automatically.

But I still need to mark, on the calendar, which day is today.

Here's my calendar now:

TableCalendar(
        calendarFormat: _calendarFormat,
        firstDay: DateTime.utc(2010, 10, 16),
        lastDay: DateTime.utc(2030, 3, 14),
        focusedDay: DateTime.now(),
        selectedDayPredicate: (day) {
          return isSameDay(_selectedDay, day);
        },
        onDaySelected: (selectedDay, focusedDay) {
          setState(() {
            _selectedDay = selectedDay;
            _focusedDay = focusedDay;
            context.read<Counter>().modify(_focusedDay);
          });
        },
        onFormatChanged: (format) {
          setState(() {
            _calendarFormat = format;
          });
        },
        onPageChanged: (focusedDay) {
          _focusedDay = focusedDay;
        },
      )
NeoFahrenheit
  • 347
  • 5
  • 16

1 Answers1

1

I see

focusedDay: DateTime.now(),

so I believe each build will reset the focused day of the TableCalendar to the current date.

you can use _focusedDay instead:

focusedDay: _focusedDay,

and initialise _focusedDay with DateTime.now():

_focusedDay = DateTime.now()
Valentin Vignal
  • 6,151
  • 2
  • 33
  • 73