1
0
Fork 0

Implement cart store for cart screen

inheritedwidget
Jonas Franz 2 years ago
parent 1ddb63525f
commit c54a7833ea
  1. 14
      lib/screens/cart/cart_screen.dart
  2. 4
      lib/screens/cart/total_price_text.dart
  3. 5
      lib/stores/cart_store.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(),
],
),

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

@ -61,6 +61,11 @@ class CartStore extends InheritedWidget {
final Function(Product) increaseAmount;
final Function(Product) decreaseAmount;
String get totalPriceAsText => cartItems
.fold<double>(
0.0, (previousValue, item) => previousValue + item.totalPrice)
.toStringAsFixed(2);
int amountOfProduct(Product product) {
final amounts = {
for (final item in cartItems) item.product: item.amount,

Loading…
Cancel
Save