rwbr / flutter_neat_and_clean_calendar

Simple and clean flutter calendar with ability to slide up/down to show weekly/monthly calendar. Forked from [flutter_clean_calender](https://pub.dev/packages/flutter_clean_calendar)
MIT License
104 stars 51 forks source link

Repeat an AllDayEvent every year for the next X years? #38

Closed murad-alm closed 2 years ago

murad-alm commented 2 years ago

Hi, any idea how to repeat certain events at the same date every year? I've tried to do it like this to repeat the event for the next 5 years:

NeatCleanCalendarEvent('Allday Example', startTime: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 12, 0), endTime: DateTime(DateTime.now().year + 5, DateTime.now().month, DateTime.now().day, 12, 0), color: Colors.pink, isAllDay: true )

Unfortunately, the event is repeated every day for the next 5 years 😄 Thanks!

Edit: I am aware i can repeat the code 5 times with different years but i want a scalable solution without any hard-coding.

rwbr commented 2 years ago

When you add an event like that and you set it's end time as you did, it lasts for the next 5 years. What you need to do is to add the event 5 times - one event for each year.

Keep in mind, that this library is just a widget, whose task is to display calendar events. But it has no logic in it to calculate repeating events. That is beyond its primary goal. That has to be done in the business logic layer of your app.

murad-alm commented 2 years ago

When you add an event like that and you set it's end time as you did, it lasts for the next 5 years. What you need to do is to add the event 5 times - one event for each year.

Keep in mind, that this library is just a widget, whose task is to display calendar events. But it has no logic in it to calculate repeating events. That is beyond its primary goal. That has to be done in the business logic layer of your app.

I appreciate your reply! I think i will do it this way then, or i will figure out a workaround. Another issue I am facing now is the lag when using the calendar in a combination with a TabBarView. When i swipe or tab on another Tab the device lags for a small while before transitioning to the calendar view. I have tested the issue on multiple devices/emulators and unfortunately, i couldn't figure out the reason.

Please note that I am using a class that returns a Scaffold where the calendar is contained.

murad-alm commented 2 years ago

SOLVED

Problem Reason:

The TabBarView doesn't save the state of the widgets in it. They are rebuilt every time they reappear on screen.

Solution:

I used AutomaticKeepAliveClientMixin to keep the Widget "alive"

class CalendarScreen extends StatefulWidget {
  @override
  State<CalendarScreen> createState() => _CalendarScreenState();
}

class _CalendarScreenState extends State<CalendarScreen> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true; 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Calendar(
          startOnMonday: true,
          hideTodayIcon: true,
          weekDays: const ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'],
          eventsList: _eventList,
          isExpandable: false,
          eventDoneColor: Colors.green,
          selectedColor: Colors.pink,
          selectedTodayColor: Colors.red,
          todayColor: Colors.blue,
          eventColor: null,
          // locale: 'de_DE',
          // todayButtonText: 'Heute',
          // allDayEventText: 'Ganztägig',
          // multiDayEndText: 'Ende',
          isExpanded: true,
          expandableDateFormat: 'EEEE, dd. MMMM yyyy',
          datePickerType: DatePickerType.hidden,
          dayOfWeekStyle: const TextStyle(
              color: Colors.black, fontWeight: FontWeight.w800, fontSize: 11),
        ),
      ),
    );
  }
}
rwbr commented 2 years ago

Good Solution. Thank you for your contribution.