-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathheatmap_calendar.dart
More file actions
251 lines (219 loc) · 7.24 KB
/
heatmap_calendar.dart
File metadata and controls
251 lines (219 loc) · 7.24 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
import 'package:flutter/material.dart';
import './data/heatmap_color_mode.dart';
import './widget/heatmap_calendar_page.dart';
import './widget/heatmap_color_tip.dart';
import './util/date_util.dart';
import './util/widget_util.dart';
class HeatMapCalendar extends StatefulWidget {
/// The datasets which fill blocks based on its value.
final Map<DateTime, int>? datasets;
/// The color value of every block's default color.
final Color? defaultColor;
/// The colorsets which give the color value for its thresholds key value.
///
/// Be aware that first Color is the maximum value if [ColorMode] is [ColorMode.opacity].
/// Also colorsets must have at least one color.
final Map<int, Color> colorsets;
/// The double value of every block's borderRadius.
final double? borderRadius;
/// The date values of initial year and month.
final DateTime? initDate;
/// The double value of every block's size.
final double? size;
/// The text color value of every blocks.
final Color? textColor;
/// The double value of every block's fontSize.
final double? fontSize;
/// The double value of month label's fontSize.
final double? monthFontSize;
/// The double value of week label's fontSize.
final double? weekFontSize;
/// The text color value of week labels.
final Color? weekTextColor;
final Color? headerColor;
/// Make block size flexible if value is true.
///
/// Default value is false.
final bool? flexible;
/// The margin value for every block.
final EdgeInsets? margin;
/// ColorMode changes the color mode of blocks.
///
/// [ColorMode.opacity] requires just one colorsets value and changes color
/// dynamically based on hightest value of [datasets].
/// [ColorMode.color] changes colors based on [colorsets] thresholds key value.
///
/// Default value is [ColorMode.opacity].
final ColorMode colorMode;
/// Function that will be called when a block is clicked.
///
/// Paratmeter gives clicked [DateTime] value.
final Function(DateTime)? onClick;
/// Function that will be called when month is changed.
///
/// Paratmeter gives [DateTime] value of current month.
final Function(DateTime)? onMonthChange;
/// Show color tip which represents the color range at the below.
///
/// Default value is true.
final bool? showColorTip;
/// Widgets which shown at left and right side of colorTip.
///
/// First value is the left side widget and second value is the right side widget.
/// Be aware that [colorTipHelper.length] have to greater or equal to 2.
/// Give null value makes default 'less' and 'more' [Text].
final List<Widget?>? colorTipHelper;
/// The integer value which represents the number of [HeatMapColorTip]'s tip container.
final int? colorTipCount;
/// The double value of [HeatMapColorTip]'s tip container's size.
final double? colorTipSize;
const HeatMapCalendar({
Key? key,
required this.colorsets,
this.colorMode = ColorMode.opacity,
this.defaultColor,
this.datasets,
this.initDate,
this.size = 42,
this.fontSize,
this.monthFontSize,
this.textColor,
this.weekFontSize,
this.weekTextColor,
this.borderRadius,
this.flexible = false,
this.margin,
this.onClick,
this.onMonthChange,
this.showColorTip = true,
this.colorTipHelper,
this.colorTipCount,
this.colorTipSize,
this.headerColor,
}) : super(key: key);
@override
State<StatefulWidget> createState() => _HeatMapCalendar();
}
class _HeatMapCalendar extends State<HeatMapCalendar> {
// The DateTime value of first day of the current month.
DateTime? _currentDate;
@override
void initState() {
super.initState();
setState(() {
// Set _currentDate value to first day of initialized date or
// today's month if widget.initDate is null.
_currentDate =
DateUtil.startDayOfMonth(widget.initDate ?? DateTime.now());
});
}
void changeMonth(int direction) {
setState(() {
_currentDate =
DateUtil.changeMonth(_currentDate ?? DateTime.now(), direction);
});
if (widget.onMonthChange != null) widget.onMonthChange!(_currentDate!);
}
/// Header widget which shows left, right buttons and year/month text.
Widget _header() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
// Previous month button.
IconButton(
icon: const Icon(
Icons.arrow_back_ios,
size: 14,
),
color: widget.headerColor,
onPressed: () => changeMonth(-1),
),
// Text which shows the current year and month
Text(
DateUtil.MONTH_LABEL[_currentDate?.month ?? 0] +
' ' +
(_currentDate?.year).toString(),
style: TextStyle(
fontSize: widget.monthFontSize ?? 12,
color: widget.headerColor,
),
),
// Next month button.
IconButton(
icon: const Icon(
Icons.arrow_forward_ios,
size: 14,
),
color: widget.headerColor,
onPressed: () => changeMonth(1),
),
],
);
}
Widget _weekLabel() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
for (String label in DateUtil.WEEK_LABEL.skip(1))
WidgetUtil.flexibleContainer(
widget.flexible ?? false,
false,
Container(
margin: EdgeInsets.only(
left: widget.margin?.left ?? 2,
right: widget.margin?.right ?? 2),
width: widget.size ?? 42,
alignment: Alignment.center,
child: Text(
label,
style: TextStyle(
fontSize: widget.weekFontSize ?? 12,
color: widget.weekTextColor ?? const Color(0xFF758EA1),
),
),
),
),
],
);
}
/// Expand width dynamically if [flexible] is true.
Widget _intrinsicWidth({
required Widget child,
}) =>
(widget.flexible ?? false) ? child : IntrinsicWidth(child: child);
@override
Widget build(BuildContext context) {
return _intrinsicWidth(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_header(),
_weekLabel(),
HeatMapCalendarPage(
baseDate: _currentDate ?? DateTime.now(),
colorMode: widget.colorMode,
flexible: widget.flexible,
size: widget.size,
fontSize: widget.fontSize,
defaultColor: widget.defaultColor,
textColor: widget.textColor,
margin: widget.margin,
datasets: widget.datasets,
colorsets: widget.colorsets,
borderRadius: widget.borderRadius,
onClick: widget.onClick,
),
if (widget.showColorTip == true)
HeatMapColorTip(
colorMode: widget.colorMode,
colorsets: widget.colorsets,
leftWidget: widget.colorTipHelper?[0],
rightWidget: widget.colorTipHelper?[1],
containerCount: widget.colorTipCount,
size: widget.colorTipSize,
),
],
),
);
}
}