Builder class
A stateless utility component whose build method uses its builder callback to create the component's child.
This component is a simple inline alternative to defining a StatelessComponent subclass. For example a component defined and used like this:
class Foo extends StatelessComponent {
@override
Component build(BuildContext context) => Text('foo');
}
Center(child: Foo())
Could equally well be defined and used like this, without defining a new component class:
Center(
child: Builder(
builder: (BuildContext context) => Text('foo');
),
)
The difference between either of the previous examples and simply
creating a child directly, without an intervening component, is the
extra BuildContext element that the additional component adds. This
is particularly noticeable when the tree contains an inherited
component that is referred to by a method like Scaffold.of
,
which visits the child component's BuildContext ancestors.
In the following example the button's onPressed
callback is unable
to find the enclosing ScaffoldState
with Scaffold.of
:
Component build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: () {
// Fails because Scaffold.of() doesn't find anything
// above this component's context.
print(Scaffold.of(context).hasAppBar);
},
child: Text('hasAppBar'),
)
),
);
}
A Builder component introduces an additional BuildContext element
and so the Scaffold.of
method succeeds.
Component build(BuildContext context) {
return Scaffold(
body: Builder(
builder: (BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
print(Scaffold.of(context).hasAppBar);
},
child: Text('hasAppBar'),
),
);
},
),
);
}
See also:
- StatefulBuilder, A stateful utility component whose build method uses its builder callback to create the component's child.
- Inheritance
-
- Object
- Component
- StatelessComponent
- Builder
Constructors
- Builder({Key? key, required ComponentBuilder builder})
-
Creates a component that delegates its build to a callback.
const
- Builder.single({Key? key, required SingleComponentBuilder builder})
- Creates a component that delegates its build to a callback that returns a single child component.
Properties
- builder → ComponentBuilder
-
Called to obtain the child component.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one component replaces another component in the tree.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Iterable< Component> -
Describes the part of the user interface represented by this component.
override
-
createElement(
) → Element -
Creates a StatelessElement to manage this component's location in the tree.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited