table_calendar_base.dart
11.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0
import 'package:flutter/material.dart';
import 'package:simple_gesture_detector/simple_gesture_detector.dart';
import 'package:table_calendar/src/shared/utils.dart';
import 'package:table_calendar/src/widgets/calendar_core.dart';
class TableCalendarBase extends StatefulWidget {
final DateTime firstDay;
final DateTime lastDay;
final DateTime focusedDay;
final CalendarFormat calendarFormat;
final DayBuilder? dowBuilder;
final DayBuilder? weekNumberBuilder;
final FocusedDayBuilder dayBuilder;
final double? dowHeight;
final double rowHeight;
final bool sixWeekMonthsEnforced;
final bool dowVisible;
final bool weekNumbersVisible;
final Decoration? dowDecoration;
final Decoration? rowDecoration;
final TableBorder? tableBorder;
final EdgeInsets? tablePadding;
final Duration formatAnimationDuration;
final Curve formatAnimationCurve;
final bool pageAnimationEnabled;
final Duration pageAnimationDuration;
final Curve pageAnimationCurve;
final StartingDayOfWeek startingDayOfWeek;
final AvailableGestures availableGestures;
final SimpleSwipeConfig simpleSwipeConfig;
final Map<CalendarFormat, String> availableCalendarFormats;
final SwipeCallback? onVerticalSwipe;
final void Function(DateTime focusedDay)? onPageChanged;
final void Function(PageController pageController)? onCalendarCreated;
TableCalendarBase({
super.key,
required this.firstDay,
required this.lastDay,
required this.focusedDay,
this.calendarFormat = CalendarFormat.month,
this.dowBuilder,
required this.dayBuilder,
this.dowHeight,
required this.rowHeight,
this.sixWeekMonthsEnforced = false,
this.dowVisible = true,
this.weekNumberBuilder,
this.weekNumbersVisible = false,
this.dowDecoration,
this.rowDecoration,
this.tableBorder,
this.tablePadding,
this.formatAnimationDuration = const Duration(milliseconds: 200),
this.formatAnimationCurve = Curves.linear,
this.pageAnimationEnabled = true,
this.pageAnimationDuration = const Duration(milliseconds: 300),
this.pageAnimationCurve = Curves.easeOut,
this.startingDayOfWeek = StartingDayOfWeek.sunday,
this.availableGestures = AvailableGestures.all,
this.simpleSwipeConfig = const SimpleSwipeConfig(
verticalThreshold: 25.0,
swipeDetectionBehavior: SwipeDetectionBehavior.continuousDistinct,
),
this.availableCalendarFormats = const {
CalendarFormat.month: 'Month',
CalendarFormat.twoWeeks: '2 weeks',
CalendarFormat.week: 'Week',
},
this.onVerticalSwipe,
this.onPageChanged,
this.onCalendarCreated,
}) : assert(!dowVisible || (dowHeight != null && dowBuilder != null)),
assert(isSameDay(focusedDay, firstDay) || focusedDay.isAfter(firstDay)),
assert(isSameDay(focusedDay, lastDay) || focusedDay.isBefore(lastDay));
@override
State<TableCalendarBase> createState() => _TableCalendarBaseState();
}
class _TableCalendarBaseState extends State<TableCalendarBase> {
late final ValueNotifier<double> _pageHeight;
late final PageController _pageController;
late DateTime _focusedDay;
late int _previousIndex;
late bool _pageCallbackDisabled;
@override
void initState() {
super.initState();
_focusedDay = widget.focusedDay;
final rowCount = _getRowCount(widget.calendarFormat, _focusedDay);
_pageHeight = ValueNotifier(_getPageHeight(rowCount));
final initialPage = _calculateFocusedPage(
widget.calendarFormat,
widget.firstDay,
_focusedDay,
);
_pageController = PageController(initialPage: initialPage);
widget.onCalendarCreated?.call(_pageController);
_previousIndex = initialPage;
_pageCallbackDisabled = false;
}
@override
void didUpdateWidget(TableCalendarBase oldWidget) {
super.didUpdateWidget(oldWidget);
if (_focusedDay != widget.focusedDay ||
widget.calendarFormat != oldWidget.calendarFormat ||
widget.startingDayOfWeek != oldWidget.startingDayOfWeek) {
final shouldAnimate = _focusedDay != widget.focusedDay;
_focusedDay = widget.focusedDay;
_updatePage(shouldAnimate: shouldAnimate);
}
if (widget.rowHeight != oldWidget.rowHeight ||
widget.dowHeight != oldWidget.dowHeight ||
widget.dowVisible != oldWidget.dowVisible ||
widget.sixWeekMonthsEnforced != oldWidget.sixWeekMonthsEnforced) {
final rowCount = _getRowCount(widget.calendarFormat, _focusedDay);
_pageHeight.value = _getPageHeight(rowCount);
}
}
@override
void dispose() {
_pageController.dispose();
_pageHeight.dispose();
super.dispose();
}
bool get _canScrollHorizontally =>
widget.availableGestures == AvailableGestures.all ||
widget.availableGestures == AvailableGestures.horizontalSwipe;
bool get _canScrollVertically =>
widget.availableGestures == AvailableGestures.all ||
widget.availableGestures == AvailableGestures.verticalSwipe;
void _updatePage({bool shouldAnimate = false}) {
final currentIndex = _calculateFocusedPage(
widget.calendarFormat,
widget.firstDay,
_focusedDay,
);
final endIndex = _calculateFocusedPage(
widget.calendarFormat,
widget.firstDay,
widget.lastDay,
);
if (currentIndex != _previousIndex ||
currentIndex == 0 ||
currentIndex == endIndex) {
_pageCallbackDisabled = true;
}
if (shouldAnimate && widget.pageAnimationEnabled) {
if ((currentIndex - _previousIndex).abs() > 1) {
final jumpIndex =
currentIndex > _previousIndex ? currentIndex - 1 : currentIndex + 1;
_pageController.jumpToPage(jumpIndex);
}
_pageController.animateToPage(
currentIndex,
duration: widget.pageAnimationDuration,
curve: widget.pageAnimationCurve,
);
} else {
_pageController.jumpToPage(currentIndex);
}
_previousIndex = currentIndex;
final rowCount = _getRowCount(widget.calendarFormat, _focusedDay);
_pageHeight.value = _getPageHeight(rowCount);
_pageCallbackDisabled = false;
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
return SimpleGestureDetector(
onVerticalSwipe: _canScrollVertically ? widget.onVerticalSwipe : null,
swipeConfig: widget.simpleSwipeConfig,
child: ValueListenableBuilder<double>(
valueListenable: _pageHeight,
builder: (context, value, child) {
final height =
constraints.hasBoundedHeight ? constraints.maxHeight : value;
return AnimatedSize(
duration: widget.formatAnimationDuration,
curve: widget.formatAnimationCurve,
alignment: Alignment.topCenter,
child: SizedBox(
height: height,
child: child,
),
);
},
child: CalendarCore(
constraints: constraints,
pageController: _pageController,
scrollPhysics: _canScrollHorizontally
? const PageScrollPhysics()
: const NeverScrollableScrollPhysics(),
firstDay: widget.firstDay,
lastDay: widget.lastDay,
startingDayOfWeek: widget.startingDayOfWeek,
calendarFormat: widget.calendarFormat,
previousIndex: _previousIndex,
focusedDay: _focusedDay,
sixWeekMonthsEnforced: widget.sixWeekMonthsEnforced,
dowVisible: widget.dowVisible,
dowHeight: widget.dowHeight,
rowHeight: widget.rowHeight,
weekNumbersVisible: widget.weekNumbersVisible,
weekNumberBuilder: widget.weekNumberBuilder,
dowDecoration: widget.dowDecoration,
rowDecoration: widget.rowDecoration,
tableBorder: widget.tableBorder,
tablePadding: widget.tablePadding,
onPageChanged: (index, focusedMonth) {
if (!_pageCallbackDisabled) {
if (!isSameDay(_focusedDay, focusedMonth)) {
_focusedDay = focusedMonth;
}
if (widget.calendarFormat == CalendarFormat.month &&
!widget.sixWeekMonthsEnforced &&
!constraints.hasBoundedHeight) {
final rowCount = _getRowCount(
widget.calendarFormat,
focusedMonth,
);
_pageHeight.value = _getPageHeight(rowCount);
}
_previousIndex = index;
widget.onPageChanged?.call(focusedMonth);
}
_pageCallbackDisabled = false;
},
dowBuilder: widget.dowBuilder,
dayBuilder: widget.dayBuilder,
),
),
);
},
);
}
double _getPageHeight(int rowCount) {
final tablePaddingHeight = widget.tablePadding?.vertical ?? 0.0;
final dowHeight = widget.dowVisible ? widget.dowHeight! : 0.0;
return dowHeight + rowCount * widget.rowHeight + tablePaddingHeight;
}
int _calculateFocusedPage(
CalendarFormat format,
DateTime startDay,
DateTime focusedDay,
) {
switch (format) {
case CalendarFormat.month:
return _getMonthCount(startDay, focusedDay);
case CalendarFormat.twoWeeks:
return _getTwoWeekCount(startDay, focusedDay);
case CalendarFormat.week:
return _getWeekCount(startDay, focusedDay);
}
}
int _getMonthCount(DateTime first, DateTime last) {
final yearDif = last.year - first.year;
final monthDif = last.month - first.month;
return yearDif * 12 + monthDif;
}
int _getWeekCount(DateTime first, DateTime last) {
return last.difference(_firstDayOfWeek(first)).inDays ~/ 7;
}
int _getTwoWeekCount(DateTime first, DateTime last) {
return last.difference(_firstDayOfWeek(first)).inDays ~/ 14;
}
int _getRowCount(CalendarFormat format, DateTime focusedDay) {
if (format == CalendarFormat.twoWeeks) {
return 2;
} else if (format == CalendarFormat.week) {
return 1;
} else if (widget.sixWeekMonthsEnforced) {
return 6;
}
final first = _firstDayOfMonth(focusedDay);
final daysBefore = _getDaysBefore(first);
final firstToDisplay = first.subtract(Duration(days: daysBefore));
final last = _lastDayOfMonth(focusedDay);
final daysAfter = _getDaysAfter(last);
final lastToDisplay = last.add(Duration(days: daysAfter));
return (lastToDisplay.difference(firstToDisplay).inDays + 1) ~/ 7;
}
int _getDaysBefore(DateTime firstDay) {
return (firstDay.weekday + 7 - getWeekdayNumber(widget.startingDayOfWeek)) %
7;
}
int _getDaysAfter(DateTime lastDay) {
final invertedStartingWeekday =
8 - getWeekdayNumber(widget.startingDayOfWeek);
final daysAfter = 7 - ((lastDay.weekday + invertedStartingWeekday) % 7);
if (daysAfter == 7) {
return 0;
}
return daysAfter;
}
DateTime _firstDayOfWeek(DateTime week) {
final daysBefore = _getDaysBefore(week);
return week.subtract(Duration(days: daysBefore));
}
DateTime _firstDayOfMonth(DateTime month) {
return DateTime.utc(month.year, month.month);
}
DateTime _lastDayOfMonth(DateTime month) {
final date = month.month < 12
? DateTime.utc(month.year, month.month + 1)
: DateTime.utc(month.year + 1);
return date.subtract(const Duration(days: 1));
}
}