class ShoppingList extends StatefulWidget {
ShoppingList({Key key, this.products}) : super(key: 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
_ShoppingListState createState() => _ShoppingListState();
}
class _ShoppingListState extends State<ShoppingList> {
Set<Product> _shoppingCart = Set<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)
flutter 有状态组件传值方式
最新推荐文章于 2024-08-20 11:25:13 发布
这篇博客展示了如何在Flutter中创建一个购物应用。应用包含一个`ShoppingList`类,它扩展了`StatefulWidget`,并维护了一个产品列表。`_ShoppingListState`类处理购物车状态,当用户更改购物车中的商品时,通过`_handleCartChanged`方法更新 `_shoppingCart` 并触发视图重建。应用的主要组件包括`AppBar`、`ListView`,用于显示每个产品的`ShoppingListItem`。

最低0.47元/天 解锁文章
2032

被折叠的 条评论
为什么被折叠?



