table_calendar icon indicating copy to clipboard operation
table_calendar copied to clipboard

Programmatically set the selected day

Open lnaie opened this issue 2 years ago • 9 comments

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

lnaie avatar Feb 18 '23 13:02 lnaie

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!

JustMilan avatar Feb 23 '23 17:02 JustMilan

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?

lnaie avatar Mar 01 '23 11:03 lnaie

You’ll probably be good if you use:

In your class: late final PageController _pageController;

In TableCalendar: onCalendarCreated: (controller) => _pageController = controller,

JustMilan avatar Mar 01 '23 12:03 JustMilan

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);
    }
  }```

lnaie avatar Mar 01 '23 12:03 lnaie

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 avatar Mar 01 '23 12:03 JustMilan

@JustMilan I need your help in my issue

shifo-stack avatar Apr 21 '23 14:04 shifo-stack

@shifo-stack

@JustMilan I need your help in my issue

I need more context in order to help you.

JustMilan avatar Apr 21 '23 14:04 JustMilan

@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

shifo-stack avatar Apr 21 '23 14:04 shifo-stack

@JustMilan can you help ?

shifo-stack avatar Apr 21 '23 14:04 shifo-stack