From c54a7833ea15c10d22fcfc81ec78c49f39c1a5ed Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 19 Feb 2022 15:17:12 +0100 Subject: [PATCH] Implement cart store for cart screen --- lib/screens/cart/cart_screen.dart | 14 ++++---------- lib/screens/cart/total_price_text.dart | 4 +++- lib/stores/cart_store.dart | 5 +++++ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/screens/cart/cart_screen.dart b/lib/screens/cart/cart_screen.dart index e369485..48ce447 100644 --- a/lib/screens/cart/cart_screen.dart +++ b/lib/screens/cart/cart_screen.dart @@ -1,22 +1,16 @@ import 'package:flutter/material.dart'; -import 'package:thesis_shop/models/cart_item.dart'; -import 'package:thesis_shop/models/product.dart'; import 'package:thesis_shop/screens/cart/total_price_text.dart'; +import 'package:thesis_shop/stores/cart_store.dart'; import 'package:thesis_shop/widgets/user_switch.dart'; import 'cart_item_list.dart'; -const _placeHolderItems = [ - CartItem(product: Product(title: 'Äpfel', price: 3), amount: 3), - CartItem(product: Product(title: 'Äpfel', price: 3), amount: 3), - CartItem(product: Product(title: 'Äpfel', price: 3), amount: 3), -]; - class CartScreen extends StatelessWidget { const CartScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { + final cartStore = CartStore.of(context); return Scaffold( appBar: AppBar( title: const Text('Warenkorb'), @@ -24,8 +18,8 @@ class CartScreen extends StatelessWidget { ), body: Column( mainAxisSize: MainAxisSize.max, - children: const [ - Expanded(child: CartItemList(items: _placeHolderItems)), + children: [ + Expanded(child: CartItemList(items: cartStore.cartItems)), TotalPriceText(), ], ), diff --git a/lib/screens/cart/total_price_text.dart b/lib/screens/cart/total_price_text.dart index a08b1e6..a84b045 100644 --- a/lib/screens/cart/total_price_text.dart +++ b/lib/screens/cart/total_price_text.dart @@ -1,15 +1,17 @@ import 'package:flutter/material.dart'; +import 'package:thesis_shop/stores/cart_store.dart'; class TotalPriceText extends StatelessWidget { const TotalPriceText({Key? key}) : super(key: key); @override Widget build(BuildContext context) { + final cartStore = CartStore.of(context); return SafeArea( child: Padding( padding: const EdgeInsets.all(8.0), child: Text( - 'Gesamtpreis: 27€', + 'Gesamtpreis: ${cartStore.totalPriceAsText}€', style: Theme.of(context).textTheme.labelLarge?.copyWith(fontSize: 24), ), ), diff --git a/lib/stores/cart_store.dart b/lib/stores/cart_store.dart index 6c56c0c..d403a8e 100644 --- a/lib/stores/cart_store.dart +++ b/lib/stores/cart_store.dart @@ -61,6 +61,11 @@ class CartStore extends InheritedWidget { final Function(Product) increaseAmount; final Function(Product) decreaseAmount; + String get totalPriceAsText => cartItems + .fold( + 0.0, (previousValue, item) => previousValue + item.totalPrice) + .toStringAsFixed(2); + int amountOfProduct(Product product) { final amounts = { for (final item in cartItems) item.product: item.amount,