-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathcalendar_date_picker2.dart
More file actions
438 lines (386 loc) · 14.2 KB
/
Copy pathcalendar_date_picker2.dart
File metadata and controls
438 lines (386 loc) · 14.2 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'package:calendar_date_picker2/calendar_date_picker2.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
part '_impl/_calendar_scroll_view.dart';
part '_impl/_calendar_view.dart';
part '_impl/_date_picker_mode_toggle_button.dart';
part '_impl/_day_picker.dart';
part '_impl/_focus_date.dart';
part '_impl/_month_picker.dart';
part '_impl/year_picker.dart';
const Duration _monthScrollDuration = Duration(milliseconds: 200);
const double _dayPickerRowHeight = 42.0;
const int _maxDayPickerRowCount = 6; // A 31 day month that starts on Saturday.
const double _monthPickerHorizontalPadding = 8.0;
const int _yearPickerColumnCount = 3;
const double _yearPickerPadding = 16.0;
const double _yearPickerRowHeight = 52.0;
const double _yearPickerRowSpacing = 8.0;
const int _monthPickerColumnCount = 3;
const double _monthPickerPadding = 16.0;
const double _monthPickerRowHeight = 52.0;
const double _monthPickerRowSpacing = 8.0;
const double _subHeaderHeight = 52.0;
const double _monthNavButtonsWidth = 108.0;
class CalendarDatePicker2 extends StatefulWidget {
CalendarDatePicker2({
required this.config,
required this.value,
this.onValueChanged,
this.displayedMonthDate,
this.onDisplayedMonthChanged,
Key? key,
}) : super(key: key) {
const valid = true;
const invalid = false;
if (config.calendarType == CalendarDatePicker2Type.single) {
assert(value.length < 2,
'Error: single date picker only allows maximum one initial date');
}
if (config.calendarType == CalendarDatePicker2Type.range &&
value.length > 1) {
final isRangePickerValueValid = value[0] == null
? (value[1] != null ? invalid : valid)
: (value[1] != null
? (value[1]!.isBefore(value[0]!) ? invalid : valid)
: valid);
assert(
isRangePickerValueValid,
'Error: range date picker must has start date set before setting end date, and start date must before end date.',
);
}
}
/// The calendar UI related configurations
final CalendarDatePicker2Config config;
/// The selected [DateTime]s that the picker should display.
final List<DateTime?> value;
/// Called when the selected dates changed
final ValueChanged<List<DateTime>>? onValueChanged;
/// Date to control calendar displayed month
final DateTime? displayedMonthDate;
/// Called when the displayed month changed
final ValueChanged<DateTime>? onDisplayedMonthChanged;
@override
State<CalendarDatePicker2> createState() => _CalendarDatePicker2State();
}
class _CalendarDatePicker2State extends State<CalendarDatePicker2> {
bool _announcedInitialDate = false;
late List<DateTime?> _selectedDates;
late CalendarDatePicker2Mode _mode;
late DateTime _currentDisplayedMonthDate;
final GlobalKey _dayPickerKey = GlobalKey();
final GlobalKey _monthPickerKey = GlobalKey();
final GlobalKey _yearPickerKey = GlobalKey();
late MaterialLocalizations _localizations;
late TextDirection _textDirection;
@override
void initState() {
super.initState();
final config = widget.config;
final initialDate = widget.displayedMonthDate ??
(widget.value.isNotEmpty && widget.value[0] != null
? DateTime(widget.value[0]!.year, widget.value[0]!.month)
: DateUtils.dateOnly(DateTime.now()));
_mode = config.calendarViewMode;
_currentDisplayedMonthDate = DateTime(initialDate.year, initialDate.month);
_selectedDates = widget.value.toList();
}
@override
void didUpdateWidget(CalendarDatePicker2 oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.config.calendarViewMode != oldWidget.config.calendarViewMode) {
_mode = widget.config.calendarViewMode;
}
if (widget.displayedMonthDate != null) {
_currentDisplayedMonthDate = DateTime(
widget.displayedMonthDate!.year,
widget.displayedMonthDate!.month,
);
}
_selectedDates = widget.value.toList();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
assert(debugCheckHasMaterial(context));
assert(debugCheckHasMaterialLocalizations(context));
assert(debugCheckHasDirectionality(context));
_localizations = MaterialLocalizations.of(context);
_textDirection = Directionality.of(context);
if (!_announcedInitialDate && _selectedDates.isNotEmpty) {
_announcedInitialDate = true;
for (final date in _selectedDates) {
if (date != null) {
SemanticsService.announce(
_localizations.formatFullDate(date),
_textDirection,
);
}
}
}
}
void _vibrate() {
if (widget.config.disableVibration == true) return;
switch (Theme.of(context).platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
HapticFeedback.vibrate();
break;
case TargetPlatform.iOS:
case TargetPlatform.macOS:
break;
}
}
void _handleModeChanged(CalendarDatePicker2Mode mode) {
_vibrate();
setState(() {
_mode = mode;
if (_selectedDates.isNotEmpty) {
for (final date in _selectedDates) {
if (date != null) {
SemanticsService.announce(
_mode == CalendarDatePicker2Mode.day
? _localizations.formatMonthYear(date)
: _mode == CalendarDatePicker2Mode.month
? _localizations.formatMonthYear(date)
: _localizations.formatYear(date),
_textDirection,
);
}
}
}
});
}
void _handleDisplayedMonthDateChanged(
DateTime date, {
bool fromYearPicker = false,
}) {
_vibrate();
setState(() {
final currentDisplayedMonthDate = DateTime(
_currentDisplayedMonthDate.year,
_currentDisplayedMonthDate.month,
);
var newDisplayedMonthDate = currentDisplayedMonthDate;
if (_currentDisplayedMonthDate.year != date.year ||
_currentDisplayedMonthDate.month != date.month) {
newDisplayedMonthDate = DateTime(date.year, date.month);
}
if (fromYearPicker) {
final selectedDatesInThisYear = _selectedDates
.where((d) => d?.year == date.year)
.toList()
..sort((d1, d2) => d1!.compareTo(d2!));
if (selectedDatesInThisYear.isNotEmpty) {
newDisplayedMonthDate =
DateTime(date.year, selectedDatesInThisYear[0]!.month);
}
}
if (currentDisplayedMonthDate.year != newDisplayedMonthDate.year ||
currentDisplayedMonthDate.month != newDisplayedMonthDate.month) {
_currentDisplayedMonthDate = DateTime(
newDisplayedMonthDate.year,
newDisplayedMonthDate.month,
);
widget.onDisplayedMonthChanged?.call(_currentDisplayedMonthDate);
}
});
}
void _handleMonthChanged(DateTime value) {
_vibrate();
setState(() {
if (widget.config.isPickMonthMode != true) {
_mode = CalendarDatePicker2Mode.day;
}
_handleDisplayedMonthDateChanged(value);
_selectedDates.removeLast();
_selectedDates.add(_currentDisplayedMonthDate);
});
widget.onValueChanged?.call(_selectedDates.whereType<DateTime>().toList());
}
void _handleYearChanged(DateTime value) {
_vibrate();
if (value.isBefore(widget.config.firstDate)) {
value = widget.config.firstDate;
} else if (value.isAfter(widget.config.lastDate)) {
value = widget.config.lastDate;
}
setState(() {
if (widget.config.calendarViewMode == CalendarDatePicker2Mode.month || widget.config.calendarViewMode == CalendarDatePicker2Mode.year) {
_mode = CalendarDatePicker2Mode.month;
} else {
_mode = CalendarDatePicker2Mode.day;
}
_handleDisplayedMonthDateChanged(value, fromYearPicker: true);
});
_selectedDates.removeLast();
_selectedDates.add(value);
widget.onValueChanged?.call(_selectedDates.whereType<DateTime>().toList());
}
void _handleDayChanged(DateTime value) {
_vibrate();
setState(() {
var selectedDates = [..._selectedDates];
selectedDates.removeWhere((d) => d == null);
final calendarType = widget.config.calendarType;
switch (calendarType) {
case CalendarDatePicker2Type.single:
selectedDates = [value];
break;
case CalendarDatePicker2Type.multi:
final index =
selectedDates.indexWhere((d) => DateUtils.isSameDay(d, value));
if (index != -1) {
selectedDates.removeAt(index);
} else {
selectedDates.add(value);
}
break;
case CalendarDatePicker2Type.range:
if (selectedDates.isEmpty) {
selectedDates.add(value);
break;
}
final isRangeSet =
selectedDates.length > 1 && selectedDates[1] != null;
final isSelectedDayBeforeStartDate =
value.isBefore(selectedDates[0]!);
if (isRangeSet) {
selectedDates = [value, null];
} else if (isSelectedDayBeforeStartDate &&
widget.config.rangeBidirectional != true) {
selectedDates = [value, null];
} else {
selectedDates = [selectedDates[0], value];
}
break;
}
selectedDates
..removeWhere((d) => d == null)
..sort((d1, d2) => d1!.compareTo(d2!));
final isValueDifferent =
widget.config.calendarType != CalendarDatePicker2Type.single ||
!DateUtils.isSameDay(selectedDates[0],
_selectedDates.isNotEmpty ? _selectedDates[0] : null);
if (isValueDifferent || widget.config.allowSameValueSelection == true) {
_selectedDates = [...selectedDates];
widget.onValueChanged
?.call(_selectedDates.whereType<DateTime>().toList());
}
});
}
Widget _buildPicker() {
switch (_mode) {
case CalendarDatePicker2Mode.day:
return _CalendarView(
config: widget.config,
key: _dayPickerKey,
initialMonth: _currentDisplayedMonthDate,
selectedDates: _selectedDates,
onChanged: _handleDayChanged,
onDisplayedMonthChanged: _handleDisplayedMonthDateChanged,
);
case CalendarDatePicker2Mode.month:
return Padding(
padding: EdgeInsets.only(
top: widget.config.controlsHeight ?? _subHeaderHeight),
child: _MonthPicker(
config: widget.config,
key: _monthPickerKey,
initialMonth: _currentDisplayedMonthDate,
selectedDates: _selectedDates,
onChanged: _handleMonthChanged,
),
);
case CalendarDatePicker2Mode.year:
return Padding(
padding: EdgeInsets.only(
top: widget.config.controlsHeight ?? _subHeaderHeight),
child: YearPicker(
config: widget.config,
key: _yearPickerKey,
initialMonth: _currentDisplayedMonthDate,
selectedDates: _selectedDates,
onChanged: _handleYearChanged,
),
);
case CalendarDatePicker2Mode.scroll:
return Container(
constraints: widget.config.scrollViewConstraints,
child: _CalendarScrollView(
config: widget.config,
key: _dayPickerKey,
initialMonth: _currentDisplayedMonthDate,
selectedDates: _selectedDates,
onChanged: _handleDayChanged,
onDisplayedMonthChanged: _handleDisplayedMonthDateChanged,
),
);
}
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
assert(debugCheckHasMaterialLocalizations(context));
assert(debugCheckHasDirectionality(context));
final dayRowsCount = widget.config.dynamicCalendarRows == true
? getDayRowsCount(
_currentDisplayedMonthDate.year,
_currentDisplayedMonthDate.month,
widget.config.firstDayOfWeek ?? _localizations.firstDayOfWeekIndex,
)
: _maxDayPickerRowCount;
final totalRowsCount = dayRowsCount + 1;
final rowHeight = widget.config.dayMaxWidth != null
? (widget.config.dayMaxWidth! + 2)
: _dayPickerRowHeight;
final maxContentHeight = rowHeight * totalRowsCount;
return widget.config.calendarViewMode == CalendarDatePicker2Mode.scroll
? _buildPicker()
: Stack(
children: <Widget>[
SizedBox(
height: (widget.config.controlsHeight ?? _subHeaderHeight) +
maxContentHeight,
child: _buildPicker(),
),
// Put the mode toggle button on top so that it won't be covered up by the _CalendarView
_DatePickerModeToggleButton(
config: widget.config,
mode: _mode,
monthDate: _currentDisplayedMonthDate,
onMonthPressed: () {
if (_mode == CalendarDatePicker2Mode.year || widget.config.isPickMonthMode == true) {
_handleModeChanged(CalendarDatePicker2Mode.month);
} else {
_handleModeChanged(
_mode == CalendarDatePicker2Mode.month
? CalendarDatePicker2Mode.day
: CalendarDatePicker2Mode.month,
);
}
},
onYearPressed: () {
if (_mode == CalendarDatePicker2Mode.month) {
_handleModeChanged(CalendarDatePicker2Mode.year);
} else {
_handleModeChanged(
_mode == CalendarDatePicker2Mode.year
? CalendarDatePicker2Mode.day
: CalendarDatePicker2Mode.year,
);
}
},
),
],
);
}
}