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
context
using 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 theme = Theme.of(context);
// since the calendar grid starts from sunday
// theres a lot of stuff that needs to be tweaked
// since dart DateTime starts from monday
final calendarValue = value;
int weekDayStart = (DateTime(view.year, view.month).weekday + 1);
int daysInMonth = DateTime(view.year, view.month + 1, 0).day;
ShadcnLocalizations localizations =
Localizations.of(context, ShadcnLocalizations);
List<Widget> rows = [];
// Weekdays Row
List<Widget> weekDays = [];
for (int i = 0; i < 7; i++) {
int weekday = ((i - 1) % 7) + 1;
weekDays.add(
Container(
width: theme.scaling * 32,
height: theme.scaling * 32,
alignment: Alignment.center,
child: Text(localizations.getAbbreviatedWeekday(weekday))
.muted()
.xSmall(),
),
);
}
rows.add(Row(
mainAxisSize: MainAxisSize.min,
children: weekDays,
));
// Days
List<Widget> days = [];
// reduce the amount of unnecessary rows
while (weekDayStart > 7) {
weekDayStart -= 7;
}
// start from the first day of the week
for (int i = 1; i < weekDayStart; i++) {
int previousMonthDay = daysInMonth - (weekDayStart - i);
var dateTime = DateTime(view.year, view.month - 1, previousMonthDay);
int indexAtRow = i - 1;
CalendarItemType type = CalendarItemType.none;
if (calendarValue != null) {
final lookup =
calendarValue.lookup(dateTime.year, dateTime.month, dateTime.day);
switch (lookup) {
case CalendarValueLookup.none:
if (now != null && now!.isAtSameMomentAs(dateTime)) {
type = CalendarItemType.today;
}
break;
case CalendarValueLookup.selected:
type = CalendarItemType.selected;
break;
case CalendarValueLookup.start:
type = CalendarItemType.startRange;
break;
case CalendarValueLookup.end:
type = CalendarItemType.endRange;
break;
case CalendarValueLookup.inRange:
type = CalendarItemType.inRange;
break;
}
} else {
if (now != null && now!.isAtSameMomentAs(dateTime)) {
type = CalendarItemType.today;
}
}
days.add(Opacity(
opacity: 0.5,
child: CalendarItem(
key: ValueKey(dateTime),
type: type,
indexAtRow: indexAtRow,
rowCount: 7,
onTap: () {
_handleTap(dateTime);
},
state: stateBuilder?.call(dateTime) ?? DateState.enabled,
child: Text('$previousMonthDay'),
),
));
}
// then the days of the month
for (int i = 1; i <= daysInMonth; i++) {
DateTime date = DateTime(view.year, view.month, i);
CalendarItemType type = CalendarItemType.none;
int indexAtRow = (weekDayStart + i - 2) % 7;
if (calendarValue != null) {
final lookup = calendarValue.lookup(date.year, date.month, date.day);
switch (lookup) {
case CalendarValueLookup.none:
if (now != null && now!.isAtSameMomentAs(date)) {
type = CalendarItemType.today;
}
break;
case CalendarValueLookup.selected:
type = CalendarItemType.selected;
break;
case CalendarValueLookup.start:
type = CalendarItemType.startRangeSelected;
break;
case CalendarValueLookup.end:
type = CalendarItemType.endRangeSelected;
break;
case CalendarValueLookup.inRange:
type = CalendarItemType.inRange;
break;
}
} else {
if (now != null && now!.isAtSameMomentAs(date)) {
type = CalendarItemType.today;
}
}
var state = stateBuilder?.call(date) ?? DateState.enabled;
days.add(CalendarItem(
key: ValueKey(date),
type: type,
indexAtRow: indexAtRow,
rowCount: 7,
onTap: () {
_handleTap(date);
},
state: state,
child: Text('$i'),
));
}
// actual needed rows
int neededRows = (days.length / 7).ceil();
// then fill the rest of the row with the next month
int totalDaysGrid = 7 * neededRows; // 42
var length = days.length;
for (int i = length; i < totalDaysGrid; i++) {
int nextMonthDay = i - length + 1;
var dateTime = DateTime(view.year, view.month + 1, nextMonthDay);
int indexAtRow = i % 7;
CalendarItemType type = CalendarItemType.none;
if (calendarValue != null) {
final lookup =
calendarValue.lookup(dateTime.year, dateTime.month, dateTime.day);
switch (lookup) {
case CalendarValueLookup.none:
if (now != null && now!.isAtSameMomentAs(dateTime)) {
type = CalendarItemType.today;
}
break;
case CalendarValueLookup.selected:
type = CalendarItemType.selected;
break;
case CalendarValueLookup.start:
type = CalendarItemType.startRange;
break;
case CalendarValueLookup.end:
type = CalendarItemType.endRange;
break;
case CalendarValueLookup.inRange:
type = CalendarItemType.inRange;
break;
}
} else {
if (now != null && now!.isAtSameMomentAs(dateTime)) {
type = CalendarItemType.today;
}
}
days.add(Opacity(
opacity: 0.5,
child: CalendarItem(
type: type,
indexAtRow: indexAtRow,
rowCount: 7,
onTap: () {
_handleTap(dateTime);
},
state: stateBuilder?.call(dateTime) ?? DateState.enabled,
child: Text('$nextMonthDay'),
),
));
}
// split the days into rows
for (int i = 0; i < days.length; i += 7) {
// there won't be any array out of bounds error
// because we made sure that the total days is 42
rows.add(Gap(theme.scaling * 8));
rows.add(Row(
mainAxisSize: MainAxisSize.min,
children: days.sublist(i, i + 7),
));
}
return Column(
mainAxisSize: MainAxisSize.min,
children: rows,
);
}