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) {
// `/schema` withheld the options, so they cannot be rendered as a dropdown
// at all — there is nothing to put in it until the user searches. The
// remote picker is that search.
if (component.optionsUrl != null && state.searchOptions != null) {
return RemoteSelectField(component: component, state: state);
}
if (component.multiple) return _multi(context);
// A value the option list no longer offers is shown as no selection, never
// handed to the dropdown: `DropdownButtonFormField` *asserts* when its
// value matches no item, which takes down the whole form with a red
// screen. That is not a corner case — a dependent picker (`company_id`
// narrowed by the chosen `user_id`) shrinks its options on every `/state`
// response, and a record being edited can hold a foreign key the query
// modifier filters out.
final offered = component.options.any(
(option) => option.value == state.value,
);
// The open menu is a route pushed onto the Navigator, mounted ABOVE this
// field, so it does not inherit the screen's `Directionality` on its own
// — the same overlay class as the dialogs, sheets and pickers elsewhere
// in this file (whole-branch review finding 1: an RTL panel inside an LTR
// host rendered the closed field RTL and every open option list LTR).
// `DropdownButtonFormField` has no `builder:` to override, so each item
// carries the direction itself: `Directionality` for the label's own text
// and an already-resolved `Alignment` for which edge it hugs, since
// `DropdownMenuItem`'s default `AlignmentDirectional` would resolve
// against the route's direction too.
//
// `Directionality.of(context)` is correct HERE — this is a
// `StatelessWidget`'s own `build` context, genuinely below the screen's
// wrap, not the `State`-method ancestor trap `textDirectionOf` exists for.
final direction = Directionality.of(context);
return DropdownButtonFormField<Object?>(
initialValue: offered ? state.value : null,
onChanged: state.enabled ? state.onChanged : null,
decoration: InputDecoration(
labelText: component.label,
helperText: component.helperText,
errorText: state.error,
),
items: [
for (final option in component.options)
DropdownMenuItem(
value: option.value,
alignment: direction == TextDirection.rtl
? Alignment.centerRight
: Alignment.centerLeft,
child: Directionality(
textDirection: direction,
child: Text(option.label),
),
),
],
);
}