text
stringlengths
1
372
runApp() persists for the lifetime of the
application.
when the parent receives the onCartChanged callback,
the parent updates its internal state, which triggers
the parent to rebuild and create a new instance
of ShoppingListItem with the new inCart value.
although the parent creates a new instance of
ShoppingListItem when it rebuilds, that operation is cheap
because the framework compares the newly built widgets with the previously
built widgets and applies only the differences to the underlying
RenderObject.
here’s an example parent widget that stores mutable state:
<code_start>
import 'package:flutter/material.dart';
class product {
const product({required this.name});
final string name;
}
typedef CartChangedCallback = Function(Product product, bool inCart);
class ShoppingListItem extends StatelessWidget {
ShoppingListItem({
required this.product,
required this.inCart,
required this.onCartChanged,
}) : super(key: ObjectKey(product));
final product product;
final bool inCart;
final CartChangedCallback onCartChanged;
color _getColor(BuildContext context) {
// the theme depends on the BuildContext because different
// parts of the tree can have different themes.
// the BuildContext indicates where the build is
// taking place and therefore which theme to use.
return inCart //
? colors.black54
: Theme.of(context).primaryColor;
}
TextStyle? _getTextStyle(BuildContext context) {
if (!incart) return null;
return const TextStyle(
color: colors.black54,
decoration: TextDecoration.lineThrough,
);
}
@override
widget build(BuildContext context) {
return ListTile(
onTap: () {
onCartChanged(product, inCart);
},
leading: CircleAvatar(
backgroundColor: _getColor(context),
child: text(product.name[0]),
),
title: text(
product.name,
style: _getTextStyle(context),
),
);
}
}
class ShoppingList extends StatefulWidget {
const ShoppingList({required this.products, super.key});
final List<Product> products;
// the framework calls createState the first time
// a widget appears at a given location in the tree.
// if the parent rebuilds and uses the same type of
// widget (with the same key), the framework re-uses
// the state object instead of creating a new state object.
@override
State<ShoppingList> createState() => _ShoppingListState();
}
class _ShoppingListState extends State<ShoppingList> {
final _shoppingCart = <product>{};
void _handleCartChanged(Product product, bool inCart) {
setState(() {
// when a user changes what's in the cart, you need
// to change _shoppingCart inside a setState call to
// trigger a rebuild.
// the framework then calls build, below,
// which updates the visual appearance of the app.
if (!incart) {
_shoppingCart.add(product);
} else {
_shoppingCart.remove(product);
}
});
}
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: const Text('Shopping list'),
),
body: ListView(
padding: const EdgeInsets.symmetric(vertical: 8),
children: widget.products.map((product) {
return ShoppingListItem(
product: product,
inCart: _shoppingCart.contains(product),