|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter/scheduler.dart'; |
| 5 | +import 'package:flutter_examples/model/model.dart'; |
| 6 | +import 'package:syncfusion_flutter_calendar/calendar.dart'; |
| 7 | + |
| 8 | +//ignore: must_be_immutable |
| 9 | +class AgendaViewCalendar extends StatefulWidget { |
| 10 | + AgendaViewCalendar({this.sample, Key key}) : super(key: key); |
| 11 | + SubItem sample; |
| 12 | + |
| 13 | + @override |
| 14 | + _AgendaViewCalendarState createState() => _AgendaViewCalendarState(sample); |
| 15 | +} |
| 16 | + |
| 17 | +class _AgendaViewCalendarState extends State<AgendaViewCalendar> { |
| 18 | + _AgendaViewCalendarState(this.sample); |
| 19 | + |
| 20 | + final SubItem sample; |
| 21 | + bool panelOpen; |
| 22 | + final ValueNotifier<bool> frontPanelVisible = ValueNotifier<bool>(true); |
| 23 | + List<String> subjectCollection; |
| 24 | + List<Color> colorCollection; |
| 25 | + List<Meeting> meetings; |
| 26 | + MeetingDataSource events; |
| 27 | + DateTime selectedDate; |
| 28 | + |
| 29 | + @override |
| 30 | + void initState() { |
| 31 | + panelOpen = frontPanelVisible.value; |
| 32 | + frontPanelVisible.addListener(_subscribeToValueNotifier); |
| 33 | + meetings = <Meeting>[]; |
| 34 | + selectedDate = DateTime.now(); |
| 35 | + addAppointmentDetails(); |
| 36 | + addAppointments(); |
| 37 | + events = MeetingDataSource(meetings); |
| 38 | + super.initState(); |
| 39 | + } |
| 40 | + |
| 41 | + void _subscribeToValueNotifier() => panelOpen = frontPanelVisible.value; |
| 42 | + |
| 43 | + @override |
| 44 | + void didUpdateWidget(AgendaViewCalendar oldWidget) { |
| 45 | + super.didUpdateWidget(oldWidget); |
| 46 | + frontPanelVisible.removeListener(_subscribeToValueNotifier); |
| 47 | + frontPanelVisible.addListener(_subscribeToValueNotifier); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + Widget build(BuildContext context) { |
| 52 | + return getAgendaViewCalendar(events, onViewChanged, selectedDate); |
| 53 | + } |
| 54 | + |
| 55 | + void addAppointmentDetails() { |
| 56 | + subjectCollection = <String>[]; |
| 57 | + subjectCollection.add('General Meeting'); |
| 58 | + subjectCollection.add('Plan Execution'); |
| 59 | + subjectCollection.add('Project Plan'); |
| 60 | + subjectCollection.add('Consulting'); |
| 61 | + subjectCollection.add('Support'); |
| 62 | + subjectCollection.add('Development Meeting'); |
| 63 | + subjectCollection.add('Scrum'); |
| 64 | + subjectCollection.add('Project Completion'); |
| 65 | + subjectCollection.add('Release updates'); |
| 66 | + subjectCollection.add('Performance Check'); |
| 67 | + |
| 68 | + colorCollection = <Color>[]; |
| 69 | + colorCollection.add(const Color(0xFF0F8644)); |
| 70 | + colorCollection.add(const Color(0xFF8B1FA9)); |
| 71 | + colorCollection.add(const Color(0xFFD20100)); |
| 72 | + colorCollection.add(const Color(0xFFFC571D)); |
| 73 | + colorCollection.add(const Color(0xFF36B37B)); |
| 74 | + colorCollection.add(const Color(0xFF01A1EF)); |
| 75 | + colorCollection.add(const Color(0xFF3D4FB5)); |
| 76 | + colorCollection.add(const Color(0xFFE47C73)); |
| 77 | + colorCollection.add(const Color(0xFF636363)); |
| 78 | + colorCollection.add(const Color(0xFF0A8043)); |
| 79 | + } |
| 80 | + |
| 81 | + void addAppointments() { |
| 82 | + final Random random = Random(); |
| 83 | + final DateTime rangeStartDate = |
| 84 | + DateTime.now().add(const Duration(days: -(365 ~/ 2))); |
| 85 | + final DateTime rangeEndDate = DateTime.now().add(const Duration(days: 365)); |
| 86 | + for (DateTime i = rangeStartDate; |
| 87 | + i.isBefore(rangeEndDate); |
| 88 | + i = i.add(const Duration(days: 1))) { |
| 89 | + final DateTime date = i; |
| 90 | + final int count = 1 + random.nextInt(3); |
| 91 | + for (int j = 0; j < count; j++) { |
| 92 | + final DateTime startDate = DateTime( |
| 93 | + date.year, date.month, date.day, 8 + random.nextInt(8), 0, 0); |
| 94 | + meetings.add(Meeting( |
| 95 | + subjectCollection[random.nextInt(7)], |
| 96 | + '', |
| 97 | + '', |
| 98 | + null, |
| 99 | + startDate, |
| 100 | + startDate.add(Duration(hours: random.nextInt(3))), |
| 101 | + colorCollection[random.nextInt(9)], |
| 102 | + false, |
| 103 | + '', |
| 104 | + '', |
| 105 | + '')); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // added recurrence appointment |
| 110 | + meetings.add(Meeting( |
| 111 | + 'Development status', |
| 112 | + '', |
| 113 | + '', |
| 114 | + null, |
| 115 | + DateTime.now(), |
| 116 | + DateTime.now().add(const Duration(hours: 2)), |
| 117 | + colorCollection[random.nextInt(9)], |
| 118 | + false, |
| 119 | + '', |
| 120 | + '', |
| 121 | + 'FREQ=WEEKLY;BYDAY=FR;INTERVAL=1')); |
| 122 | + } |
| 123 | + |
| 124 | + void onViewChanged(ViewChangedDetails visibleDatesChangedDetails) { |
| 125 | + SchedulerBinding.instance.addPostFrameCallback((_) { |
| 126 | + final DateTime currentViewDate = visibleDatesChangedDetails |
| 127 | + .visibleDates[visibleDatesChangedDetails.visibleDates.length ~/ 2]; |
| 128 | + if (currentViewDate.month == DateTime.now().month && |
| 129 | + currentViewDate.year == DateTime.now().year) { |
| 130 | + selectedDate = DateTime.now(); |
| 131 | + } else { |
| 132 | + selectedDate = |
| 133 | + DateTime(currentViewDate.year, currentViewDate.month, 01); |
| 134 | + } |
| 135 | + setState(() {}); |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +SfCalendar getAgendaViewCalendar( |
| 141 | + [CalendarDataSource _calendarDataSource, |
| 142 | + ViewChangedCallback onViewChanged, |
| 143 | + DateTime selectedDate]) { |
| 144 | + return SfCalendar( |
| 145 | + view: CalendarView.month, |
| 146 | + initialSelectedDate: selectedDate, |
| 147 | + onViewChanged: onViewChanged, |
| 148 | + dataSource: _calendarDataSource, |
| 149 | + monthViewSettings: MonthViewSettings(showAgenda: true), |
| 150 | + timeSlotViewSettings: TimeSlotViewSettings( |
| 151 | + minimumAppointmentDuration: const Duration(minutes: 60)), |
| 152 | + ); |
| 153 | +} |
| 154 | + |
| 155 | +class MeetingDataSource extends CalendarDataSource { |
| 156 | + MeetingDataSource(this.source); |
| 157 | + |
| 158 | + List<Meeting> source; |
| 159 | + |
| 160 | + @override |
| 161 | + List<dynamic> get appointments => source; |
| 162 | + |
| 163 | + @override |
| 164 | + DateTime getStartTime(int index) { |
| 165 | + return source[index].from; |
| 166 | + } |
| 167 | + |
| 168 | + @override |
| 169 | + DateTime getEndTime(int index) { |
| 170 | + return source[index].to; |
| 171 | + } |
| 172 | + |
| 173 | + @override |
| 174 | + bool isAllDay(int index) { |
| 175 | + return source[index].isAllDay; |
| 176 | + } |
| 177 | + |
| 178 | + @override |
| 179 | + String getSubject(int index) { |
| 180 | + return source[index].eventName; |
| 181 | + } |
| 182 | + |
| 183 | + @override |
| 184 | + String getStartTimeZone(int index) { |
| 185 | + return source[index].startTimeZone; |
| 186 | + } |
| 187 | + |
| 188 | + @override |
| 189 | + String getEndTimeZone(int index) { |
| 190 | + return source[index].endTimeZone; |
| 191 | + } |
| 192 | + |
| 193 | + @override |
| 194 | + Color getColor(int index) { |
| 195 | + return source[index].background; |
| 196 | + } |
| 197 | + |
| 198 | + @override |
| 199 | + String getRecurrenceRule(int index) { |
| 200 | + return source[index].recurrenceRule; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +class Meeting { |
| 205 | + Meeting( |
| 206 | + this.eventName, |
| 207 | + this.organizer, |
| 208 | + this.contactID, |
| 209 | + this.capacity, |
| 210 | + this.from, |
| 211 | + this.to, |
| 212 | + this.background, |
| 213 | + this.isAllDay, |
| 214 | + this.startTimeZone, |
| 215 | + this.endTimeZone, |
| 216 | + this.recurrenceRule); |
| 217 | + |
| 218 | + String eventName; |
| 219 | + String organizer; |
| 220 | + String contactID; |
| 221 | + int capacity; |
| 222 | + DateTime from; |
| 223 | + DateTime to; |
| 224 | + Color background; |
| 225 | + bool isAllDay; |
| 226 | + String startTimeZone; |
| 227 | + String endTimeZone; |
| 228 | + String recurrenceRule; |
| 229 | +} |
0 commit comments