1
0
Fork 0

Connect CartButton

mobx
Jonas Franz 2 years ago
parent f9d2a889f2
commit 0d972b3126
  1. 17
      lib/screens/product_list/cart_button.dart
  2. 3
      lib/stores/cart_store.dart
  3. 10
      lib/stores/cart_store.g.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)'),
);
},
);
}
}

@ -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);

@ -16,6 +16,13 @@ mixin _$CartStore on _CartStore, Store {
(_$cartComputed ??= Computed<ObservableList<CartItem>>(() => super.cart,
name: '_CartStore.cart'))
.value;
Computed<int>? _$numberOfItemsComputed;
@override
int get numberOfItems =>
(_$numberOfItemsComputed ??= Computed<int>(() => 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}
''';
}
}

Loading…
Cancel
Save