table_calendar icon indicating copy to clipboard operation
table_calendar copied to clipboard

eventLoader doesnt work with Getx

Open mohhamad-esmaili opened this issue 2 years ago • 4 comments

I tried to solve problem with #441 and #141, but i couldnt i have project with getx and handle calendar updates and events from controller, when calendar loads for first time the events doesnt load, when tap on calendar and select day again, it loads! i have project in my github,

here is calendar

class TableCalendarWidget extends StatelessWidget {
  final EventController controller = Get.find<EventController>();

  @override
  Widget build(BuildContext context) {
    return GetX<EventController>(
      builder: (themeController) => TableCalendar(
        focusedDay: controller.focusedDay.value,
        firstDay: DateTime(1990),
        lastDay: DateTime(2100),
        calendarFormat: CalendarTheme.calendarThemeFormat,
        startingDayOfWeek: StartingDayOfWeek.saturday,
        daysOfWeekVisible: true,
        headerVisible: false,
        weekendDays: const [4, 5],
        daysOfWeekStyle: CalendarTheme.calendarDayOfWeekTheme,
        calendarStyle: CalendarTheme.calendarDarkMode,
        pageAnimationEnabled: true,
        onDaySelected: (DateTime selectDay, DateTime focusDay) {
          controller.selectedDay.value = selectDay;
          controller.focusedDay.value = focusDay;
          controller.firstLoad.value = false;
        },
        selectedDayPredicate: (DateTime date) {
          return isSameDay(controller.selectedDay.value, date);
        },
        eventLoader: controller.getEventsFromDate,
      ),
    );
  }
}

here is controller

class EventController extends GetxController {
  Map<dynamic, dynamic> items = {};
  var selectedDay = DateTime.now().obs;
  var focusedDay = DateTime.now().obs;
  final _eventBox = boxList[0];
  var remindMe = false.obs;
  RxBool firstLoad = true.obs;

  @override
  void onInit() {
    refreshItems();
    super.onInit();
  }

  /// this refresh items by getting again from box
  void refreshItems() {
    final value = _eventBox.get("events") ?? {};
    items = value;
    selectedDay.refresh();
    focusedDay.refresh();
    update();
  }


  /// It creats todoEvent with [DateTime] parameter.
  List<Event> getEventsFromDate(DateTime date) {
    return List<Event>.from(items[date] ?? []);
  }
}

I made firstLoad to handle this and make it when first time calendar loads user see error and tap on calendar to load, but it not what i want, how can i solve this, if this is a bug plz solve it or give solution, thank you

mohhamad-esmaili avatar Sep 02 '22 07:09 mohhamad-esmaili

Same here, in my case even if I force triggered onDaySeleted and even loaded, then after I wipe to next week/month, event makers disappears again.

LanceQuare avatar Sep 04 '22 07:09 LanceQuare

Exactly i tried many ways and didnt work again

mohhamad-esmaili avatar Sep 09 '22 09:09 mohhamad-esmaili

same here, first load of events don't shows in calendar month if I swipe to next week/month events load in al calendar. any way to force load events?

sandamil avatar Mar 13 '23 16:03 sandamil

basically, we need method to force eventLoader or, maybe eventLoader as a future . For now, hope, i found the solution. Set TableCalendar key as GlobalKey

late GlobalKey calendarGlobalKey;
TableCalendar(
key : calendarGlobalKey
);

and in your loading method

void loadData (){
  ....
 calendarGlobalKey.currentState?.setState(() {});
}

but it is soooo lame

astheras avatar Nov 30 '23 14:11 astheras