ListenableBuilder class
A general-purpose widget for building a widget subtree when a Listenable changes.
ListenableBuilder is useful for more complex widgets that wish to listen to changes in other objects as part of a larger build function. To use ListenableBuilder, construct the widget and pass it a builder function.
Any subtype of Listenable (such as a ChangeNotifier, ValueNotifier, or Animation) can be used with a ListenableBuilder to rebuild only certain parts of a widget when the Listenable notifies its listeners. Although they have identical implementations, if an Animation is being listened to, consider using an AnimatedBuilder instead for better readability.
To create a local project with this code sample, run:
flutter create --sample=widgets.ListenableBuilder.1 mysample
To create a local project with this code sample, run:
flutter create --sample=widgets.ListenableBuilder.2 mysample
Performance optimizations
If the builder function contains a subtree that does not depend on the listenable, it is more efficient to build that subtree once instead of rebuilding it on every change of the listenable.
Performance is therefore improved by specifying any widgets that don't need to change using the prebuilt child attribute. The ListenableBuilder passes this child back to the builder callback so that it can be incorporated into the build.
Using this pre-built child is entirely optional, but can improve performance significantly in some cases and is therefore a good practice.
To create a local project with this code sample, run:
flutter create --sample=widgets.ListenableBuilder.3 mysample
See also:
- AnimatedBuilder, which has the same functionality, but is named more appropriately for a builder triggered by Animations.
- ValueListenableBuilder, which is specialized for ValueNotifiers and reports the new value in its builder callback.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- AnimatedWidget
- ListenableBuilder
- Implementers
Constructors
- ListenableBuilder({Key? key, required Listenable listenable, required TransitionBuilder builder, Widget? child})
-
Creates a builder that responds to changes in
listenable
.const
Properties
- builder → TransitionBuilder
-
Called every time the listenable notifies about a change.
final
- child → Widget?
-
The child widget to pass to the builder.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- listenable → Listenable
-
The Listenable supplied to the constructor.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Widget -
Override this method to build widgets that depend on the state of the
listenable (e.g., the current value of the animation).
override
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< AnimatedWidget> -
Subclasses typically do not override this method.
inherited
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited