table_calendar
table_calendar copied to clipboard
Programmatically set the selected day
Hello,
The widget handles very well the user interaction part, but I need a way to programmatically set the selected day. So that would be without the user's input. Is there one? If not then I could use some hints about how to implement it.
Thanks
Hi, it is possible to programmatically set the selected day.
The TableCalendar widget has a selectedDay property which is used to control the currently selected day. You can simply set this property to the desired DateTime value to programmatically select a day.
This is what I have done:
// property final ValueNotifier<DateTime> _focusedDay = ValueNotifier(DateTime.now());
Inside the TableCalendar(...) I have the following properties amongst others
focusedDay: _focusedDay.value, onDaySelected: _onDaySelected, onDaySelected: _onDaySelected, selectedDayPredicate: (day) { return isSameDay(_focusedDay.value, day); // <-- is included in this library },
` void _onDaySelected(DateTime selectedDay, DateTime focusedDay) { if (!isSameDay(_focusedDay.value, selectedDay)) { setState(() { _focusedDay.value = selectedDay; });
// extra logic that I have for my own added things
`
Hope this helps you!
Yes, thanks, I figured it out from samples. And that works even with the bloc state provider. Awesome!
Now the next challenge is to show the calendar view of the selected day. So if the selected day goes out of the visible month, by setting it programmatically, it should also move the month view to the one that contains the selected day. Any idea how that could be achieved currently?
You’ll probably be good if you use:
In your class:
late final PageController _pageController;
In TableCalendar:
onCalendarCreated: (controller) => _pageController = controller,
Ok, that's a good lead. Thanks.
But the page index has to be calculated somehow to be able to use jumpToPage(). Otherwise, nextPage() and previousPage() have to be called repetitively. And it looks to me that the page index could be provided by some utility function like this private method does it, in the _TableCalendarBaseState:
int _calculateFocusedPage(
CalendarFormat format, DateTime startDay, DateTime focusedDay) {
switch (format) {
case CalendarFormat.month:
return _getMonthCount(startDay, focusedDay);
case CalendarFormat.twoWeeks:
return _getTwoWeekCount(startDay, focusedDay);
case CalendarFormat.week:
return _getWeekCount(startDay, focusedDay);
default:
return _getMonthCount(startDay, focusedDay);
}
}```
I’m at work now, so I can’t go into my code and try different things, but I thought that by giving it that property that if you select a day, it would go to the right page straight away. No other things needed. Let me know if that’s the case. Maybe you need to setState after and in setState body the focusedDay.value = focusedDay?
@JustMilan I need your help in my issue
@shifo-stack
@JustMilan I need your help in my issue
I need more context in order to help you.
@shifo-stack
@JustMilan I need your help in my issue
I need more context in order to help you.
https://github.com/aleksanderwozniak/table_calendar/issues/794
@JustMilan can you help ?