From 5512ed137d38bb4a40b573019c6d3cf90af7886e Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 19 Feb 2022 14:47:42 +0100 Subject: [PATCH] Add cart item model --- lib/models/cart_item.dart | 14 ++++++++++++++ lib/screens/cart/cart_item_list.dart | 29 ++++++++++++++++------------ lib/screens/cart/cart_screen.dart | 10 +++++++++- 3 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 lib/models/cart_item.dart diff --git a/lib/models/cart_item.dart b/lib/models/cart_item.dart new file mode 100644 index 0000000..9be046a --- /dev/null +++ b/lib/models/cart_item.dart @@ -0,0 +1,14 @@ +import 'package:flutter/foundation.dart'; +import 'package:thesis_shop/models/product.dart'; + +@immutable +class CartItem { + final Product product; + final int amount; + + double get totalPrice => product.price * amount; + String get totalPriceAsString => totalPrice.toStringAsFixed(2); + + const CartItem({required this.product, required this.amount}) + : assert(amount >= 0); +} diff --git a/lib/screens/cart/cart_item_list.dart b/lib/screens/cart/cart_item_list.dart index 7f72ec9..5373bb5 100644 --- a/lib/screens/cart/cart_item_list.dart +++ b/lib/screens/cart/cart_item_list.dart @@ -1,7 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:thesis_shop/models/cart_item.dart'; class CartItemList extends StatelessWidget { - const CartItemList({Key? key}) : super(key: key); + final List items; + const CartItemList({Key? key, required this.items}) : super(key: key); @override Widget build(BuildContext context) { @@ -10,17 +12,20 @@ class CartItemList extends StatelessWidget { child: Padding( padding: const EdgeInsets.all(16.0), child: Table( - columnWidths: const { - 0: FlexColumnWidth(), - 1: FixedColumnWidth(128), - 2: IntrinsicColumnWidth(), - }, - children: const [ - TableRow(children: [Text('Äpfel'), Text('3x3€'), Text('9€')]), - TableRow(children: [Text('Äpfel'), Text('3x3€'), Text('9€')]), - TableRow(children: [Text('Äpfel'), Text('3x3€'), Text('9€')]) - ], - ), + columnWidths: const { + 0: FlexColumnWidth(), + 1: FixedColumnWidth(128), + 2: IntrinsicColumnWidth(), + }, + children: items + .map( + (item) => TableRow(children: [ + Text(item.product.title), + Text("${item.amount}*${item.product.priceAsString}€"), + Text('${item.totalPriceAsString}€'), + ]), + ) + .toList()), ), ); } diff --git a/lib/screens/cart/cart_screen.dart b/lib/screens/cart/cart_screen.dart index 3e7ded1..08e9e45 100644 --- a/lib/screens/cart/cart_screen.dart +++ b/lib/screens/cart/cart_screen.dart @@ -1,9 +1,17 @@ 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/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); @@ -17,7 +25,7 @@ class CartScreen extends StatelessWidget { body: Column( mainAxisSize: MainAxisSize.max, children: const [ - Expanded(child: CartItemList()), + Expanded(child: CartItemList(items: _placeHolderItems)), TotalPriceText(), ], ),