skip to Main Content

i’m using calendar_ view flutter package to display events on dates, i have read documentation but still confused how to display events on MonthView() Widget

CalendarControllerProvider(
    controller: EventController(),
    child: MaterialApp(
        // Your initialization for material app.
    ),
)
//reste of code

 List<CalendarEventData> events = [
            CalendarEventData(
                title: "title to display", date: DateTime(2023, 06, 2), 
event: "or this event to display")];

MonthView(             
controller: EventController(),
              // to provide custom UI for month cells.
cellBuilder: (date, events, isToday, isInMonth) {
                // Return your widget to display as month cell.
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(date.day.toString()),
                    Text("I want to display here event based on date")
                  ],
                );
              },
              minMonth: DateTime(2020),
              maxMonth: DateTime(2023),
              initialMonth: DateTime(DateTime.now().year, DateTime.now().month),
              onPageChange: (date, pageIndex) => print("$date, $pageIndex"),
              onCellTap: (events, date) {
                // here we must add event
              },
              startDay: WeekDays.sunday, // To change the first day of the week.
              // This callback will only work if cellBuilder is null.
            ),

2

Answers


  1. Chosen as BEST ANSWER

    after many tries I got solution with myself maybe someone can find it helpful... the problem was in how to display events in the cells

    // First we add event to controller
     final event = CalendarEventData(
              date: DateTime(2023, 7, 07),
              title: 'Duplicate',
            );
            controller.add(event);
    
    // second step to display the event in cellbuilder
    MonthView(
     cellBuilder: (date, events, isToday, isInMonth) {
                  // Return your widget to display as month cell.
                  if (events.isNotEmpty) {
                    // Display the first event's title in the cell.
                     // at place 0 you can index it
                        return Text(events[0].title),
                  } else {
                    // No events, return an empty container.
                    return Container();
                  }
                     //Rest of code
    
                },), 
    

  2. calendar_view library has an controller (EventController). You can create an instance of this controller:

     final EventController controller = EventController();
    

    and using this controller in MonthView

    MonthView(
      controller:controller ,
    );
    

    you can add events in this controller:

     controller.addAll(<CalendarEventData>[ ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search