complex_example.dart
7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0
// ignore_for_file: avoid_print
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:table_calendar_example/utils.dart';
class TableComplexExample extends StatefulWidget {
const TableComplexExample({super.key});
@override
State<TableComplexExample> createState() => _TableComplexExampleState();
}
class _TableComplexExampleState extends State<TableComplexExample> {
late final ValueNotifier<List<Event>> _selectedEvents;
final ValueNotifier<DateTime> _focusedDay = ValueNotifier(DateTime.now());
final Set<DateTime> _selectedDays = LinkedHashSet<DateTime>(
equals: isSameDay,
hashCode: getHashCode,
);
late PageController _pageController;
CalendarFormat _calendarFormat = CalendarFormat.month;
RangeSelectionMode _rangeSelectionMode = RangeSelectionMode.toggledOff;
DateTime? _rangeStart;
DateTime? _rangeEnd;
@override
void initState() {
super.initState();
_selectedDays.add(_focusedDay.value);
_selectedEvents = ValueNotifier(_getEventsForDay(_focusedDay.value));
}
@override
void dispose() {
_focusedDay.dispose();
_selectedEvents.dispose();
super.dispose();
}
bool get canClearSelection =>
_selectedDays.isNotEmpty || _rangeStart != null || _rangeEnd != null;
List<Event> _getEventsForDay(DateTime day) {
return kEvents[day] ?? [];
}
List<Event> _getEventsForDays(Iterable<DateTime> days) {
return [
for (final d in days) ..._getEventsForDay(d),
];
}
List<Event> _getEventsForRange(DateTime start, DateTime end) {
final days = daysInRange(start, end);
return _getEventsForDays(days);
}
void _onDaySelected(DateTime selectedDay, DateTime focusedDay) {
setState(() {
if (_selectedDays.contains(selectedDay)) {
_selectedDays.remove(selectedDay);
} else {
_selectedDays.add(selectedDay);
}
_focusedDay.value = focusedDay;
_rangeStart = null;
_rangeEnd = null;
_rangeSelectionMode = RangeSelectionMode.toggledOff;
});
_selectedEvents.value = _getEventsForDays(_selectedDays);
}
void _onRangeSelected(DateTime? start, DateTime? end, DateTime focusedDay) {
setState(() {
_focusedDay.value = focusedDay;
_rangeStart = start;
_rangeEnd = end;
_selectedDays.clear();
_rangeSelectionMode = RangeSelectionMode.toggledOn;
});
if (start != null && end != null) {
_selectedEvents.value = _getEventsForRange(start, end);
} else if (start != null) {
_selectedEvents.value = _getEventsForDay(start);
} else if (end != null) {
_selectedEvents.value = _getEventsForDay(end);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('TableCalendar - Complex'),
),
body: Column(
children: [
ValueListenableBuilder<DateTime>(
valueListenable: _focusedDay,
builder: (context, value, _) {
return _CalendarHeader(
focusedDay: value,
clearButtonVisible: canClearSelection,
onTodayButtonTap: () {
setState(() => _focusedDay.value = DateTime.now());
},
onClearButtonTap: () {
setState(() {
_rangeStart = null;
_rangeEnd = null;
_selectedDays.clear();
_selectedEvents.value = [];
});
},
onLeftArrowTap: () {
_pageController.previousPage(
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
},
onRightArrowTap: () {
_pageController.nextPage(
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
},
);
},
),
TableCalendar<Event>(
firstDay: kFirstDay,
lastDay: kLastDay,
focusedDay: _focusedDay.value,
headerVisible: false,
selectedDayPredicate: (day) => _selectedDays.contains(day),
rangeStartDay: _rangeStart,
rangeEndDay: _rangeEnd,
calendarFormat: _calendarFormat,
rangeSelectionMode: _rangeSelectionMode,
eventLoader: _getEventsForDay,
holidayPredicate: (day) {
// Every 20th day of the month will be treated as a holiday
return day.day == 20;
},
onDaySelected: _onDaySelected,
onRangeSelected: _onRangeSelected,
onCalendarCreated: (controller) => _pageController = controller,
onPageChanged: (focusedDay) => _focusedDay.value = focusedDay,
onFormatChanged: (format) {
if (_calendarFormat != format) {
setState(() => _calendarFormat = format);
}
},
),
const SizedBox(height: 8.0),
Expanded(
child: ValueListenableBuilder<List<Event>>(
valueListenable: _selectedEvents,
builder: (context, value, _) {
return ListView.builder(
itemCount: value.length,
itemBuilder: (context, index) {
return Container(
margin: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 4.0,
),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(12.0),
),
child: ListTile(
onTap: () => print('${value[index]}'),
title: Text('${value[index]}'),
),
);
},
);
},
),
),
],
),
);
}
}
class _CalendarHeader extends StatelessWidget {
final DateTime focusedDay;
final VoidCallback onLeftArrowTap;
final VoidCallback onRightArrowTap;
final VoidCallback onTodayButtonTap;
final VoidCallback onClearButtonTap;
final bool clearButtonVisible;
const _CalendarHeader({
required this.focusedDay,
required this.onLeftArrowTap,
required this.onRightArrowTap,
required this.onTodayButtonTap,
required this.onClearButtonTap,
required this.clearButtonVisible,
});
@override
Widget build(BuildContext context) {
final headerText = DateFormat.yMMM().format(focusedDay);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
children: [
const SizedBox(width: 16.0),
SizedBox(
width: 120.0,
child: Text(
headerText,
style: const TextStyle(fontSize: 26.0),
),
),
IconButton(
icon: const Icon(Icons.calendar_today, size: 20.0),
visualDensity: VisualDensity.compact,
onPressed: onTodayButtonTap,
),
if (clearButtonVisible)
IconButton(
icon: const Icon(Icons.clear, size: 20.0),
visualDensity: VisualDensity.compact,
onPressed: onClearButtonTap,
),
const Spacer(),
IconButton(
icon: const Icon(Icons.chevron_left),
onPressed: onLeftArrowTap,
),
IconButton(
icon: const Icon(Icons.chevron_right),
onPressed: onRightArrowTap,
),
],
),
);
}
}