StateListener<S> class
A widget that listens to a StateReadable and calls a listener when the state changes.
StateListener is used for performing side effects in response to state changes such as showing dialogs, navigating to another screen, or logging events. It does not rebuild its child when the state changes.
The listener function is called whenever the state changes and the optional listenWhen condition returns true.
{@tool snippet} This example shows how to use StateListener to log state changes and perform navigation:
class CounterWidget extends StatelessWidget {
final CounterReadable counterReadable;
final NavigatorState navigator;
const CounterWidget({
required this.counterReadable,
required this.navigator,
super.key,
});
@override
Widget build(BuildContext context) => StateListener<int>(
stateReadable: counterReadable,
listenWhen: (previous, current) => current > 5, // Only listen when counter exceeds 5
listener: (context, count) {
// Log the current count
debugPrint('Counter reached milestone: $count');
// Navigate to a different screen when count reaches 10
if (count >= 10) {
navigator.pushNamed('/milestone-reached');
}
},
child: const SizedBox(), // A simple placeholder widget
);
}
class CounterReadable implements StateReadable<int> {
final StreamController<int> _controller = StreamController<int>.broadcast();
int _count = 0;
@override
int get state => _count;
@override
Stream<int> get stream => _controller.stream;
void increment() {
_count++;
_controller.add(_count);
}
Future<void> dispose() => _controller.close();
}
{@end-tool}
See also:
- StateBuilder, which rebuilds when the state changes instead of performing side effects.
- StateConsumer, which combines both rebuilding and side effects.
- StateSelector, which rebuilds only when a specific part of the state changes.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- StateListener
Constructors
-
StateListener({required StateReadable<
S> stateReadable, required StateWidgetListener<S> listener, required Widget child, StateListenerCondition<S> ? listenWhen, Key? key}) -
Creates a new StateListener.
const
Properties
- child → Widget
-
The widget below this widget in the tree.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
-
listener
→ StateWidgetListener<
S> -
The function that is called when the state changes.
final
-
listenWhen
→ StateListenerCondition<
S> ? -
Optional condition to determine when to call listener.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stateReadable
→ StateReadable<
S> -
The source of the state.
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< StateListener< S> > -
Creates the mutable state for this widget at a given location in the tree.
override
-
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