-
Notifications
You must be signed in to change notification settings - Fork 54
/
scrollable_clean_calendar.dart
369 lines (332 loc) · 11.8 KB
/
scrollable_clean_calendar.dart
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
357
358
359
360
361
362
363
364
365
366
367
368
369
library scrollable_clean_calendar;
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:scrollable_clean_calendar/src/clean_calendar_controller.dart';
import 'package:scrollable_clean_calendar/src/week_helper.dart';
import 'package:scrollable_clean_calendar/utils/date_models.dart';
import 'src/week_helper.dart';
extension StringExtension on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1)}";
}
}
typedef RangeDate = Function(DateTime minDate, DateTime? maxDate);
typedef SelectDate = Function(DateTime date);
typedef TextStyleFunction = Function(bool isSelected);
class ScrollableCleanCalendar extends StatefulWidget {
ScrollableCleanCalendar({
this.locale = 'en',
required this.minDate,
required this.maxDate,
this.isRangeMode = true,
this.onRangeSelected,
this.showDaysWeeks = true,
this.monthLabelStyle,
this.monthLabelAlign = MainAxisAlignment.center,
this.dayLabelStyle,
this.dayWeekLabelStyle,
this.selectedDateColor = Colors.indigo,
this.rangeSelectedDateColor = Colors.blue,
this.selectDateRadius = 15,
this.onTapDate,
this.renderPostAndPreviousMonthDates = false,
this.disabledDateColor = Colors.grey,
this.startWeekDay = DateTime.monday,
this.initialDateSelected,
this.endDateSelected,
this.scrollController,
});
final ScrollController? scrollController;
final bool isRangeMode;
final String locale;
final bool showDaysWeeks;
final bool renderPostAndPreviousMonthDates;
final DateTime minDate;
final DateTime maxDate;
final DateTime? initialDateSelected;
final DateTime? endDateSelected;
/// Esse parametro sera habilitado em futuras versoes
@deprecated
final int startWeekDay;
final double selectDateRadius;
final RangeDate? onRangeSelected;
final TextStyleFunction? dayLabelStyle;
final SelectDate? onTapDate;
///Styles
final TextStyle? monthLabelStyle;
final TextStyle? dayWeekLabelStyle;
final Color selectedDateColor;
final Color rangeSelectedDateColor;
final Color disabledDateColor;
final MainAxisAlignment monthLabelAlign;
@override
_ScrollableCleanCalendarState createState() =>
_ScrollableCleanCalendarState();
}
class _ScrollableCleanCalendarState extends State<ScrollableCleanCalendar> {
CleanCalendarController? _cleanCalendarController;
List<Month>? months;
DateTime? rangeMinDate;
DateTime? rangeMaxDate;
DateTime? _minDate;
DateTime? _maxDate;
@override
void initState() {
initializeDateFormatting();
final _minDateDay =
widget.renderPostAndPreviousMonthDates ? 1 : widget.minDate.day;
final _maxDateDay = widget.renderPostAndPreviousMonthDates
? WeekHelper.daysPerMonth(widget.maxDate.year)[widget.maxDate.month - 1]
: widget.maxDate.day;
_minDate = DateTime(widget.minDate.year, widget.minDate.month, _minDateDay);
_maxDate = DateTime(
widget.maxDate.year, widget.maxDate.month, _maxDateDay, 23, 59, 00);
months = WeekHelper.extractWeeks(_minDate!, _maxDate!, widget.startWeekDay);
_cleanCalendarController =
CleanCalendarController(startWeekDay: widget.startWeekDay);
WidgetsBinding.instance?.addPostFrameCallback((_) {
if (widget.initialDateSelected != null &&
(widget.initialDateSelected!.isAfter(widget.minDate) ||
widget.initialDateSelected!.isSameDay(widget.minDate))) {
_onDayClick(widget.initialDateSelected!);
}
if (widget.endDateSelected != null &&
(widget.endDateSelected!.isBefore(widget.maxDate) ||
widget.endDateSelected!.isSameDay(widget.maxDate))) {
_onDayClick(widget.endDateSelected!);
}
});
super.initState();
}
@override
Widget build(BuildContext context) {
return ListView.builder(
controller: widget.scrollController,
cacheExtent:
(MediaQuery.of(context).size.width / DateTime.daysPerWeek) * 6,
itemCount: months!.length,
itemBuilder: (context, index) {
final month = months![index];
return Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Column(
children: [
_buildMonthLabelRow(month, context),
Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Table(
key: ValueKey('Calendar$index'),
children: [
_buildDayWeeksRow(context),
...month.weeks.map(
(Week week) {
DateTime firstDay = week.firstDay;
return _buildDaysRow(week, firstDay, context);
},
).toList(growable: false),
],
),
)
],
),
);
},
);
}
TableRow _buildDaysRow(Week week, DateTime firstDay, BuildContext context) {
// _cleanCalendarController.dayOfWeek()
return TableRow(
children: List<Widget>.generate(
DateTime.daysPerWeek,
(int position) {
DateTime day = DateTime(
week.firstDay.year,
week.firstDay.month,
firstDay.day +
(position - (firstDay.weekday - widget.startWeekDay)));
final dayIsBeforeMinDate =
day.isBefore(widget.minDate) && !day.isSameDay(widget.minDate);
final dayIsAfterMaxDate =
day.isAfter(widget.maxDate) && !day.isSameDay(widget.maxDate);
if ((position + widget.startWeekDay) < week.firstDay.weekday ||
(position + widget.startWeekDay) > week.lastDay.weekday ||
day.isBefore(_minDate!) ||
day.isAfter(_maxDate!)) {
return SizedBox.shrink();
} else if (dayIsBeforeMinDate || dayIsAfterMaxDate) {
return TableCell(
key:
ValueKey(DateFormat('dd-MM-yyyy', widget.locale).format(day)),
child: Container(
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0,
),
child: Text(
DateFormat('d', widget.locale).format(day),
style: Theme.of(context).textTheme.bodyText2!.copyWith(
color: widget.disabledDateColor,
),
),
),
),
),
);
} else {
bool rangeFeatureEnabled = rangeMinDate != null;
bool isSelected = false;
if (rangeFeatureEnabled) {
if (rangeMinDate != null && rangeMaxDate != null) {
isSelected = day.isSameDayOrAfter(rangeMinDate!) &&
day.isSameDayOrBefore(rangeMaxDate!);
} else {
isSelected = day.isAtSameMomentAs(rangeMinDate!);
}
}
return TableCell(
key:
ValueKey(DateFormat('dd-MM-yyyy', widget.locale).format(day)),
child: GestureDetector(
onTap: () {
_onDayClick(day);
},
child: Container(
key: ValueKey(
'${DateFormat('dd-MM-yyyy', widget.locale).format(day)}_container'),
decoration: BoxDecoration(
borderRadius: BorderRadius.horizontal(
left: Radius.circular(
_getRadiusRangeMinDate(isSelected, day),
),
right: Radius.circular(
_getRadiusRangeMaxDate(isSelected, day),
),
),
color: _getBackgroundColor(isSelected, day),
),
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0,
),
child: Text(
DateFormat('d', widget.locale).format(day),
style: widget.dayLabelStyle != null
? widget.dayLabelStyle!(isSelected)
: Theme.of(context).textTheme.bodyText2!.copyWith(
color:
isSelected ? Colors.white : Colors.black,
),
),
),
),
),
),
);
}
},
growable: false,
),
);
}
double _getRadiusRangeMinDate(bool isSelected, DateTime day) {
if (isSelected) {
if (day.compareTo(rangeMinDate!) == 0 && rangeMaxDate != null) {
return widget.selectDateRadius;
}
}
return 0;
}
double _getRadiusRangeMaxDate(bool isSelected, DateTime day) {
if (isSelected) {
if (rangeMaxDate != null && day.compareTo(rangeMaxDate!) == 0) {
return widget.selectDateRadius;
}
}
return 0;
}
Color _getBackgroundColor(bool isSelected, DateTime day) {
if (isSelected) {
if (day.compareTo(rangeMinDate!) == 0 ||
(rangeMaxDate != null && day.compareTo(rangeMaxDate!) == 0)) {
return widget.selectedDateColor;
} else {
return widget.rangeSelectedDateColor;
}
}
return Colors.transparent;
}
TableRow _buildDayWeeksRow(BuildContext context) {
return widget.showDaysWeeks
? TableRow(
children: [
for (var i = 0; i < DateTime.daysPerWeek; i++)
TableCell(
child: Center(
child: Text(
_cleanCalendarController!
.getDaysOfWeek(widget.locale)[i]
.capitalize(),
key: ValueKey("WeekLabel$i"),
style: widget.dayWeekLabelStyle ??
Theme.of(context).textTheme.bodyText1!.copyWith(
color: Colors.grey[300],
fontWeight: FontWeight.bold,
),
),
),
),
],
)
: TableRow(children: [
for (var i = 0; i < DateTime.daysPerWeek; i++)
TableCell(child: SizedBox.shrink())
]);
}
Widget _buildMonthLabelRow(Month month, BuildContext context) {
return Row(
mainAxisAlignment: widget.monthLabelAlign,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Text(
DateFormat('MMMM yyyy', widget.locale)
.format(
DateTime(month.year, month.month),
)
.capitalize(),
style: widget.monthLabelStyle ??
Theme.of(context).textTheme.bodyText1!.copyWith(
color: Colors.grey[800],
),
),
),
],
);
}
void _onDayClick(DateTime date) {
if (widget.isRangeMode) {
if (rangeMinDate == null || rangeMaxDate != null) {
rangeMinDate = date;
rangeMaxDate = null;
} else if (date.isBefore(rangeMinDate!)) {
rangeMaxDate = rangeMinDate;
rangeMinDate = date;
} else if (date.isAfter(rangeMinDate!) || date.isSameDay(rangeMinDate!)) {
rangeMaxDate = date;
}
} else {
rangeMinDate = date;
rangeMaxDate = date;
}
setState(() {});
if (widget.onTapDate != null) {
widget.onTapDate!(date);
}
if (widget.onRangeSelected != null) {
widget.onRangeSelected!(rangeMinDate!, rangeMaxDate);
}
}
}