text
stringlengths 1
372
|
|---|
onCartChanged: _handleCartChanged,
|
);
|
}).tolist(),
|
),
|
);
|
}
|
}
|
void main() {
|
runApp(const MaterialApp(
|
title: 'shopping app',
|
home: ShoppingList(
|
products: [
|
product(name: 'eggs'),
|
product(name: 'flour'),
|
product(name: 'chocolate chips'),
|
],
|
),
|
));
|
}
|
<code_end>
|
the ShoppingList class extends StatefulWidget,
|
which means this widget stores mutable state.
|
when the ShoppingList widget is first inserted
|
into the tree, the framework calls the createState() function
|
to create a fresh instance of _ShoppingListState to associate with that
|
location in the tree. (notice that subclasses of
|
state are typically named with leading underscores to
|
indicate that they are private implementation details.)
|
when this widget’s parent rebuilds, the parent creates a new instance
|
of ShoppingList, but the framework reuses the _ShoppingListState
|
instance that is already in the tree rather than calling
|
createState again.
|
to access properties of the current ShoppingList,
|
the _ShoppingListState can use its widget property.
|
if the parent rebuilds and creates a new ShoppingList,
|
the _ShoppingListState rebuilds with the new widget value.
|
if you wish to be notified when the widget property changes,
|
override the didUpdateWidget() function, which is passed
|
an oldWidget to let you compare the old widget with
|
the current widget.
|
when handling the onCartChanged callback, the _ShoppingListState
|
mutates its internal state by either adding or removing a product from
|
_shoppingCart. to signal to the framework that it changed its internal
|
state, it wraps those calls in a setState() call.
|
calling setState marks this widget as dirty and schedules it to be rebuilt
|
the next time your app needs to update the screen.
|
if you forget to call setState when modifying the internal
|
state of a widget, the framework won’t know your widget is
|
dirty and might not call the widget’s build() function,
|
which means the user interface might not update to reflect
|
the changed state. by managing state in this way,
|
you don’t need to write separate code for creating and
|
updating child widgets. instead, you simply implement the build
|
function, which handles both situations.
|
<topic_end>
|
<topic_start>
|
responding to widget lifecycle events
|
after calling createState() on the StatefulWidget,
|
the framework inserts the new state object into the tree and
|
then calls initState() on the state object.
|
a subclass of state can override initState to do work
|
that needs to happen just once. for example, override initState
|
to configure animations or to subscribe to platform services.
|
implementations of initState are required to start
|
by calling super.initState.
|
when a state object is no longer needed,
|
the framework calls dispose() on the state object.
|
override the dispose function to do cleanup work.
|
for example, override dispose to cancel timers or to
|
unsubscribe from platform services. implementations of
|
dispose typically end by calling super.dispose.
|
for more information, check out state.
|
<topic_end>
|
<topic_start>
|
keys
|
use keys to control which widgets the framework matches up
|
with other widgets when a widget rebuilds. by default, the
|
framework matches widgets in the current and previous build
|
according to their runtimeType and the order in which they appear.
|
with keys, the framework requires that the two widgets have
|
the same key as well as the same runtimeType.
|
keys are most useful in widgets that build many instances of
|
the same type of widget. for example, the ShoppingList widget,
|
which builds just enough ShoppingListItem instances to
|
fill its visible region:
|
without keys, the first entry in the current build
|
would always sync with the first entry in the previous build,
|
even if, semantically, the first entry in the list just
|
scrolled off screen and is no longer visible in the viewport.
|
by assigning each entry in the list a “semantic” key,
|
the infinite list can be more efficient because the
|
framework syncs entries with matching semantic keys
|
and therefore similar (or identical) visual appearances.
|
moreover, syncing the entries semantically means that
|
state retained in stateful child widgets remains attached
|
to the same semantic entry rather than the entry in the
|
same numerical position in the viewport.
|
for more information, check out the key API.
|
<topic_end>
|
<topic_start>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.