CellHooks mixin
Provides the cell method for creating and retrieving instances of ValueCell
's.
The cell method can be used within build to obtain an instance of
a ValueCell
that is persisted between builds of the widget.
During the first build every call to cell will create a new ValueCell
instance using the provided cell creation function. In subsequent builds
calls to cell return the existing instance that was created during the
first build using the corresponding cell creation function.
Example:
class Example extends CellWidget {
@override
Widget build(BuildContext context) {
final a = cell(() => MutableCell(0));
final b = cell(() => MutableCell(1));
final sum = cell(() => a + b);
final product = cell(() => a * b);
return Column(
children: [
Text('a + b = ${sum()}'),
Text('a * b = ${product()}')
...
]
);
}
}
In the example above, when the widget is built for the first time, two
mutable cells are created and assigned to a
and b
, and two computed
cells a + b
, a * b
are assigned to sum
and product
, respectively.
In subsequent builds, the first two calls to cell return the same
MutableCell
instances created during the first build and
cell(() => a + b)
returns the existing instance of the cell a + b
, while
cell(() => a * b)
returns the existing instance of the cell a * b
.
With this class cell definitions can be kept directly within the build function without having to use a StatefulWidget.
NOTE:
Uses of cell have to obey the following rules:
- Calls to cell must not appear within conditionals or loops.
- Calls to cell must not appear within widget builder functions, such as those used with Builder or ValueListenableBuilder.
- Superclass constraints
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- restorationId → String?
-
Restoration ID to use for restoring the cell state
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
inherited
-
cell<
T, V extends ValueCell< (T> >CreateCell< V> create) → V -
Return an instance of a
ValueCell
that is kept between builds of the widget. -
createElement(
) → StatelessElement -
Creates a StatelessElement to manage this widget's 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}) → 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
-
watch(
VoidCallback watch) → void - Register a callback to be called whenever the values of cells change.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited