build method
Describes the part of the user interface represented by this component.
The framework calls this method when this component is inserted into the tree in a given BuildContext and when the dependencies of this component change (e.g., an InheritedComponent referenced by this component changes). This method can potentially be called in every frame and should not have any side effects beyond building a component.
The framework replaces the subtree below this component with the component returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the component returned by this method can update the root of the existing subtree, as determined by calling Component.canUpdate.
Typically implementations return a newly created constellation of components that are configured with information from this component's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this component is being built. For example, the context provides the set of inherited components for this location in the tree. A given component might be built with multiple different BuildContext arguments over time if the component is moved around the tree or if the component is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the component, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedComponentOfExactType.
If a component's build method is to depend on anything else, use a StatefulComponent instead.
See also:
- StatelessComponent, which contains the discussion on performance considerations.
Implementation
@override
Component build(BuildContext context) {
final days = _getDaysInMonth();
final weekDays = props.firstDayOfWeek == 1
? [..._weekDays.sublist(1), _weekDays[0]]
: _weekDays;
final displayMonth = props.displayMonth;
return dom.div(
classes: 'codex-calendar',
attributes: {
'role': 'application',
'aria-label': 'Calendar',
},
styles: const dom.Styles(raw: {
'display': 'flex',
'flex-direction': 'column',
'gap': '10px',
'padding': '16px',
'background-color': 'var(--card)',
'border': '1px solid var(--border)',
'border-radius': 'var(--radius)',
'width': 'fit-content',
}),
[
// Header with navigation
dom.div(
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'justify-content': 'space-between',
'gap': '10px',
}),
[
dom.button(
classes: 'codex-calendar-nav-btn',
attributes: {
'type': 'button',
'aria-label': 'Previous month',
},
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'justify-content': 'center',
'width': '36px',
'height': '36px',
'background': 'transparent',
'border': 'none',
'border-radius': 'var(--radius)',
'cursor': 'pointer',
'color': 'var(--foreground)',
'transition': 'background-color var(--arcane-transition)',
}),
events: props.onPreviousMonth != null
? {'click': (_) => props.onPreviousMonth!()}
: null,
[const Component.text('<')],
),
dom.span(
styles: const dom.Styles(raw: {
'font-weight': 'var(--font-weight-semibold)',
'font-size': 'var(--font-size-sm)',
'color': 'var(--foreground)',
}),
[Component.text('${_months[displayMonth.month - 1]} ${displayMonth.year}')],
),
dom.button(
classes: 'codex-calendar-nav-btn',
attributes: {
'type': 'button',
'aria-label': 'Next month',
},
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'justify-content': 'center',
'width': '36px',
'height': '36px',
'background': 'transparent',
'border': 'none',
'border-radius': 'var(--radius)',
'cursor': 'pointer',
'color': 'var(--foreground)',
'transition': 'background-color var(--arcane-transition)',
}),
events: props.onNextMonth != null
? {'click': (_) => props.onNextMonth!()}
: null,
[const Component.text('>')],
),
],
),
// Week day headers
dom.div(
styles: dom.Styles(raw: {
'display': 'grid',
'grid-template-columns': props.showWeekNumbers
? 'auto repeat(7, 1fr)'
: 'repeat(7, 1fr)',
'gap': '4px',
}),
[
if (props.showWeekNumbers)
const dom.div(
styles: dom.Styles(raw: {'width': '36px'}),
[],
),
for (final day in weekDays)
dom.div(
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'justify-content': 'center',
'height': '36px',
'font-size': 'var(--font-size-xs)',
'font-weight': 'var(--font-weight-medium)',
'color': 'var(--muted-foreground)',
}),
[Component.text(day)],
),
],
),
// Calendar grid
dom.div(
styles: dom.Styles(raw: {
'display': 'grid',
'grid-template-columns': props.showWeekNumbers
? 'auto repeat(7, 1fr)'
: 'repeat(7, 1fr)',
'gap': '4px',
}),
[
for (var i = 0; i < days.length; i++) ...[
if (props.showWeekNumbers && i % 7 == 0)
dom.div(
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'justify-content': 'center',
'width': '36px',
'height': '36px',
'font-size': 'var(--font-size-xs)',
'color': 'var(--muted-foreground)',
}),
[Component.text('${_getWeekNumber(days[i])}')],
),
_buildDay(days[i]),
],
],
),
// Today button
if (props.showToday)
dom.button(
attributes: {'type': 'button'},
styles: const dom.Styles(raw: {
'padding': '6px 12px',
'background': 'transparent',
'border': '1px solid var(--border)',
'border-radius': 'var(--radius)',
'font-size': '0.8125rem',
'color': 'var(--muted-foreground)',
'cursor': 'pointer',
'transition': 'background-color var(--arcane-transition)',
'align-self': 'center',
}),
events: props.onGoToToday != null
? {'click': (_) => props.onGoToToday!()}
: null,
[const Component.text('Today')],
),
],
);
}