merged a simple logic for the basket

This commit is contained in:
esche 2023-07-04 22:07:00 +02:00
parent f0415a22da
commit 7388527c43
5 changed files with 94 additions and 50 deletions

View file

@ -3,13 +3,12 @@ import 'package:flutter/services.dart';
import 'sample_data.dart'; import 'sample_data.dart';
class ShowBasket extends StatelessWidget { class ShowBasket extends StatelessWidget {
final int index; final Basket basket;
final bool editable; final bool editable;
const ShowBasket(this.index, {this.editable = false, super.key}); const ShowBasket(this.basket, {this.editable = false, super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final List<Basket> basket = SampleData().transactions[index].basket!;
return Column( return Column(
children: [ children: [
Expanded( Expanded(
@ -24,7 +23,7 @@ class ShowBasket extends StatelessWidget {
'Kosten:', 'Kosten:',
), ),
subtitle: Text( subtitle: Text(
'${SampleData().transactions[index].amount / 100}', '${basket.price}',
), ),
trailing: editable trailing: editable
? Row( ? Row(
@ -47,19 +46,19 @@ class ShowBasket extends StatelessWidget {
); );
} }
ListView getBasket(List<Basket> basket) { ListView getBasket(Basket basket) {
return ListView.builder( return ListView.builder(
itemCount: basket.length, itemCount: basket.purchases.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
return Card( return Card(
child: ListTile( child: ListTile(
leading: Text( leading: Text(
basket[index].products.category.icon, basket.purchases.keys.elementAt(index).category.icon,
style: const TextStyle(fontSize: 24), style: const TextStyle(fontSize: 24),
), ),
title: Text(basket[index].products.name), title: Text(basket.purchases.keys.elementAt(index).name),
subtitle: Text( subtitle: Text(
'${basket[index].amount} ${basket[index].products.unit}'), '${basket.purchases.values.elementAt(index)} ${basket.purchases.keys.elementAt(index).unit}'),
trailing: editable trailing: editable
? SizedBox( ? SizedBox(
width: 100, width: 100,

View file

@ -43,7 +43,9 @@ class Finance extends StatelessWidget {
showBottomSheet( showBottomSheet(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return ShowBasket(index); return ShowBasket(SampleData()
.transactions[index]
.basket!);
}); });
} }
: null)); : null));
@ -68,8 +70,7 @@ class Finance extends StatelessWidget {
Text getSubtitle(Transaction transaction) { Text getSubtitle(Transaction transaction) {
String text = '${transaction.amount / 100}'; String text = '${transaction.amount / 100}';
text += ''; text += '';
text += DateFormat("EEEE, dd. MMMM yyyy HH:mm") text += DateFormat("EEEE, dd. MMMM yyyy HH:mm").format(transaction.date);
.format(transaction.date);
if (transaction.description != null) { if (transaction.description != null) {
(text += '\n${transaction.description}'); (text += '\n${transaction.description}');
} }
@ -98,7 +99,8 @@ class Finance extends StatelessWidget {
showBottomSheet( showBottomSheet(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return ShowBasket(index, editable: true); return ShowBasket(SampleData().transactions[index].basket!,
editable: true);
}); });
}, },
); );

View file

@ -1,3 +1,5 @@
import 'package:uuid/uuid.dart';
final DateTime now = DateTime.now(); final DateTime now = DateTime.now();
enum TransaktionArt { monatlBeitrag, aufladung, einkauf, korrektur } enum TransaktionArt { monatlBeitrag, aufladung, einkauf, korrektur }
@ -15,7 +17,7 @@ class Transaction {
int amount; int amount;
TransaktionArt type; TransaktionArt type;
DateTime date; DateTime date;
List<Basket>? basket; Basket? basket;
String? description; String? description;
Transaction( Transaction(
{required this.type, {required this.type,
@ -28,17 +30,31 @@ class Transaction {
class Product { class Product {
final int id = 0; final int id = 0;
final String name; final String name;
final Unit unit; final Unit unit; //pro Kg oder Stück
final double price; //pro Kilogramm oder Stück final double price;
final double vat; final double vat;
final Category category; final Category category;
const Product(this.name, this.unit, this.price, this.vat, this.category); const Product(this.name, this.unit, this.price, this.vat, this.category);
} }
class Basket { class Basket {
Product products; Map<Product, int> purchases;
int amount; double price;
Basket(this.products, this.amount); String guid;
Basket(this.purchases, this.price) : guid = const Uuid().v4();
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);
}
} }
class SampleData { class SampleData {
@ -63,39 +79,41 @@ class SampleData {
Product('Flaschenpfand', Unit.stueck, -0.15, 0, categories[3]) Product('Flaschenpfand', Unit.stueck, -0.15, 0, categories[3])
]; ];
static List<Basket> basket = [ //such a basket can not exist later. It is for testing purposes
Basket(products[0], 20), // when scrolling through a long basket
Basket(products[1], 2200), static Basket basket = Basket({
Basket(products[2], 2), products[0]: 20,
Basket(products[3], 1), products[1]: 2200,
Basket(products[4], 1), products[2]: 2,
Basket(products[5], 2), products[3]: 1,
Basket(products[6], 222), products[4]: 1,
Basket(products[0], 20), products[5]: 2,
Basket(products[1], 2200), products[6]: 222,
Basket(products[2], 2), products[0]: 20,
Basket(products[3], 1), products[1]: 2200,
Basket(products[4], 1), products[2]: 2,
Basket(products[5], 2), products[3]: 1,
Basket(products[6], 222) products[4]: 1,
]; products[5]: 2,
products[6]: 222
}, 27.9);
static List<Basket> basket2 = [ static Basket basket2 = Basket({
Basket(products[0], 22), products[0]: 22,
Basket(products[1], 2241), products[1]: 2241,
Basket(products[3], 2), products[3]: 2,
Basket(products[4], 4), products[4]: 4,
Basket(products[6], 2), products[6]: 2,
Basket(products[7], 5) products[7]: 5,
]; }, 34);
static List<Basket> basket3 = [ static Basket basket3 = Basket({
Basket(products[0], -2), products[0]: 2,
Basket(products[1], 21), products[1]: 21,
Basket(products[3], -4), products[3]: 4,
Basket(products[4], 1), products[4]: 1,
Basket(products[6], 5) products[6]: 5,
]; }, -1);
List<Transaction> transactions = [ List<Transaction> transactions = [
Transaction( Transaction(

View file

@ -41,6 +41,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.17.1" version: "1.17.1"
crypto:
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
url: "https://pub.dev"
source: hosted
version: "3.0.3"
cupertino_icons: cupertino_icons:
dependency: "direct main" dependency: "direct main"
description: description:
@ -184,6 +192,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.5.1" version: "0.5.1"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
source: hosted
version: "1.3.2"
uuid:
dependency: "direct main"
description:
name: uuid
sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313"
url: "https://pub.dev"
source: hosted
version: "3.0.7"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:

View file

@ -36,6 +36,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
intl: ^0.18.1 intl: ^0.18.1
uuid: ^3.0.7
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: