diff --git a/lib/screens/product_list/cart_button.dart b/lib/screens/product_list/cart_button.dart index 5256e5e..0431155 100644 --- a/lib/screens/product_list/cart_button.dart +++ b/lib/screens/product_list/cart_button.dart @@ -1,17 +1,24 @@ import 'package:flutter/material.dart'; +import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:thesis_shop/benchmark_counter.dart'; import 'package:thesis_shop/route_key.dart'; +import 'package:thesis_shop/stores/store_injector.dart'; class CartButton extends StatelessWidget { const CartButton() : super(key: const Key('cart_button')); @override Widget build(BuildContext context) { - BenchmarkCounters.cartButton++; - return ElevatedButton.icon( - onPressed: () => Navigator.of(context).pushRouteKey(RouteKey.cart), - icon: const Icon(Icons.shopping_basket), - label: const Text('Warenkorb (3 Produkte)'), + final cartStore = StoreInjector.of(context).cartStore; + return Observer( + builder: (context) { + BenchmarkCounters.cartButton++; + return ElevatedButton.icon( + onPressed: () => Navigator.of(context).pushRouteKey(RouteKey.cart), + icon: const Icon(Icons.shopping_basket), + label: Text('Warenkorb (${cartStore.numberOfItems} Produkte)'), + ); + }, ); } } diff --git a/lib/stores/cart_store.dart b/lib/stores/cart_store.dart index 5e49397..367447e 100644 --- a/lib/stores/cart_store.dart +++ b/lib/stores/cart_store.dart @@ -29,6 +29,9 @@ abstract class _CartStore with Store { return ObservableList.of(items); } + @computed + int get numberOfItems => cart.length; + @action void _changeAmountOfProduct(Product product, int amount) { final oldValue = _productQuantities.putIfAbsent(product.title, () => 0); diff --git a/lib/stores/cart_store.g.dart b/lib/stores/cart_store.g.dart index fd4663e..cbd976d 100644 --- a/lib/stores/cart_store.g.dart +++ b/lib/stores/cart_store.g.dart @@ -16,6 +16,13 @@ mixin _$CartStore on _CartStore, Store { (_$cartComputed ??= Computed>(() => super.cart, name: '_CartStore.cart')) .value; + Computed? _$numberOfItemsComputed; + + @override + int get numberOfItems => + (_$numberOfItemsComputed ??= Computed(() => super.numberOfItems, + name: '_CartStore.numberOfItems')) + .value; final _$_productQuantitiesAtom = Atom(name: '_CartStore._productQuantities'); @@ -48,7 +55,8 @@ mixin _$CartStore on _CartStore, Store { @override String toString() { return ''' -cart: ${cart} +cart: ${cart}, +numberOfItems: ${numberOfItems} '''; } }