ValueNotifierWrapper<T> class
A StatefulWidget that manages a ValueNotifier and provides it to its child builder.
ValueNotifierWrapper simplifies working with ValueNotifier by handling its creation, disposal, and optionally reacting to value changes via onChange. This widget is useful for local, ephemeral state that doesn't require a full-blown state management solution, and for building simple, reactive UI patterns.
The child builder receives the ValueNotifier so it can be used with widgets like ValueListenableBuilder.
Example
ValueNotifierWrapper<int>(
initialValue: 0,
onChange: (value) => print('Value changed: $value'),
child: (notifier) => ValueListenableBuilder<int>(
valueListenable: notifier,
builder: (context, value, _) => Text('Current value: $value'),
),
)
See also:
- ValueNotifier, a simple class for holding a single value and notifying listeners.
- ValueListenableBuilder, which rebuilds when a ValueListenable changes.
- StreamControllerWrapper, for managing a StreamController in a similar pattern.
- CallbackWidgetBuilder, the typedef used for the child builder.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- ValueNotifierWrapper
Constructors
-
ValueNotifierWrapper({required T initialValue, required CallbackWidgetBuilder<
ValueNotifier< child, ValueChanged<T> >T> ? onChange, Key? key}) -
Creates a ValueNotifierWrapper.
const
Properties
-
child
→ CallbackWidgetBuilder<
ValueNotifier< T> > -
Called to build the widget subtree and provides the ValueNotifier.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialValue → T
-
The initial value for the ValueNotifier.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
-
onChange
→ ValueChanged<
T> ? -
Callback called when the ValueNotifier value changes.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< ValueNotifierWrapper< T> > -
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