add logic example
This commit is contained in:
parent
597db5d51f
commit
96c89170a0
2 changed files with 25 additions and 12 deletions
|
@ -20,7 +20,7 @@ class ShowBasket extends StatelessWidget {
|
|||
return Card(
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.abc),
|
||||
title: Text(SampleData().basket.purchases[index].product.name),
|
||||
title: Text(SampleData().basket.purchases.keys.elementAt(index).name),
|
||||
//trailing: Text(),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -40,9 +40,22 @@ class Purchase {
|
|||
}
|
||||
|
||||
class Basket {
|
||||
List<Purchase> purchases;
|
||||
int price;
|
||||
Map<Product, int> purchases;
|
||||
double price;
|
||||
|
||||
Basket(this.purchases, this.price);
|
||||
|
||||
void addItem(Product product, int quantity) {
|
||||
if (purchases.containsKey(product)) {
|
||||
purchases.update(product, (existingQuantity) => existingQuantity + quantity);
|
||||
} else {
|
||||
purchases[product] = quantity;
|
||||
}
|
||||
}
|
||||
|
||||
void removeItem(Product product) {
|
||||
purchases.remove(product);
|
||||
}
|
||||
}
|
||||
|
||||
//sample data
|
||||
|
@ -90,13 +103,13 @@ class SampleData {
|
|||
Product('Schokolade', Unit.menge, 0.23, 7, Category.suessesUndKnabbereien)
|
||||
];
|
||||
|
||||
Basket basket = Basket([
|
||||
Purchase(products[0], 20),
|
||||
Purchase(products[1], 2200),
|
||||
Purchase(products[2], 2),
|
||||
Purchase(products[3], 1),
|
||||
Purchase(products[4], 1),
|
||||
Purchase(products[5], 2),
|
||||
Purchase(products[6], 202)
|
||||
], 304);
|
||||
Basket basket = Basket({
|
||||
products[0]: 20,
|
||||
products[1]: 2200,
|
||||
products[2]: 2,
|
||||
products[3]: 1,
|
||||
products[4]: 1,
|
||||
products[5]: 2,
|
||||
products[6]: 202
|
||||
}, 304);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue