build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
final DateTime end = _normalize(endDate ?? DateTime.now());
final DateTime start =
_normalize(startDate ?? end.subtract(const Duration(days: 90)));
// Default color range: shades of primary
final List<Color> colors = colorRange ??
<Color>[
const Color(0xFFC7D2FE),
const Color(0xFF818CF8),
const Color(0xFF4F46E5),
const Color(0xFF312E81),
];
final Color inactiveColor = emptyColor ??
(Theme.of(context).brightness == Brightness.dark
? const Color(0xFF1E293B)
: const Color(0xFFF1F5F9));
// Group dates into weeks (7 days each, starting Sunday=7 or Monday=1)
// Find previous Monday or start of week
int daysOffset = start.weekday - DateTime.monday;
if (daysOffset < 0) daysOffset += 7;
final DateTime gridStart = start.subtract(Duration(days: daysOffset));
final int totalDays = end.difference(gridStart).inDays + 1;
final int totalWeeks = (totalDays / 7).ceil();
final Map<DateTime, int> normalizedData = <DateTime, int>{};
for (final MapEntry<DateTime, int> entry in data.entries) {
normalizedData[_normalize(entry.key)] = entry.value;
}
// Determine max value for auto scaling
int maxVal = 1;
for (final int v in normalizedData.values) {
if (v > maxVal) maxVal = v;
}
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (showDayLabels) ...<Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
if (showMonthLabels) SizedBox(height: cellSize + 4),
for (int d = 0; d < 7; d++)
SizedBox(
height: cellSize + cellSpacing,
child: Center(
child: Text(
d == 1
? 'Mon'
: (d == 3 ? 'Wed' : (d == 5 ? 'Fri' : '')),
style: TextStyle(
fontSize: cellSize * 0.7,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
),
),
],
),
SizedBox(width: cellSpacing * 2),
],
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: List<Widget>.generate(
totalWeeks,
(int weekIndex) => Padding(
padding: EdgeInsets.only(right: cellSpacing),
child: Column(
children: List<Widget>.generate(7, (int dayIndex) {
final DateTime currentDay = gridStart.add(
Duration(days: weekIndex * 7 + dayIndex),
);
if (currentDay.isAfter(end) ||
currentDay.isBefore(start)) {
return SizedBox(
width: cellSize, height: cellSize + cellSpacing);
}
final int count = normalizedData[currentDay] ?? 0;
Color cellColor = inactiveColor;
if (count > 0) {
final double intensity =
(count / maxVal).clamp(0.0, 1.0);
final int colorIdx = (intensity * (colors.length - 1))
.round()
.clamp(0, colors.length - 1);
cellColor = colors[colorIdx];
}
return Padding(
padding: EdgeInsets.only(bottom: cellSpacing),
child: InkWell(
onTap: onDayTap != null
? () => onDayTap!(currentDay, count)
: null,
borderRadius: BorderRadius.circular(borderRadius),
child: Tooltip(
message:
'${currentDay.year}-${currentDay.month.toString().padLeft(2, '0')}-${currentDay.day.toString().padLeft(2, '0')}: $count',
child: Container(
width: cellSize,
height: cellSize,
decoration: BoxDecoration(
color: cellColor,
borderRadius:
BorderRadius.circular(borderRadius),
),
),
),
),
);
}),
),
),
),
),
],
),
],
),
);
}