mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +02:00
add receipt when finishing shopping
This commit is contained in:
parent
d74fe3a706
commit
1db101a26c
@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:mobile/repos/cart.dart';
|
import 'package:mobile/repos/cart.dart';
|
||||||
|
import 'package:mobile/repos/receipt.dart';
|
||||||
import 'package:mobile/widgets/primary_button.dart';
|
import 'package:mobile/widgets/primary_button.dart';
|
||||||
import 'package:mobile/widgets/receipt_item.dart';
|
import 'package:mobile/widgets/receipt_item.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
@ -10,6 +11,7 @@ class FinishShoppingPage extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final CartRepo cartRepo = context.read<CartRepo>();
|
final CartRepo cartRepo = context.read<CartRepo>();
|
||||||
|
final ReceiptRepo receiptRepo = context.read<ReceiptRepo>();
|
||||||
final cart = cartRepo.allCartItems();
|
final cart = cartRepo.allCartItems();
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -43,8 +45,17 @@ class FinishShoppingPage extends StatelessWidget {
|
|||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
Center(
|
Center(
|
||||||
child:
|
child: PrimaryButton(
|
||||||
PrimaryButton(onPressed: () {}, child: const Text("Betal")))
|
onPressed: () {
|
||||||
|
receiptRepo.createReceipt(cart);
|
||||||
|
cartRepo.clearCart();
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: const Text("Betal"))),
|
||||||
|
//const CircularProgressIndicator(
|
||||||
|
// valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
|
||||||
|
// strokeWidth: 6.0,
|
||||||
|
//)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -22,45 +22,10 @@ class CartRepo extends ChangeNotifier {
|
|||||||
amount: 6),
|
amount: 6),
|
||||||
CartItem(
|
CartItem(
|
||||||
product: Product(
|
product: Product(
|
||||||
id: 1,
|
id: 3,
|
||||||
name: "Letmælk",
|
name: "Minimælk",
|
||||||
price: 13,
|
price: 12,
|
||||||
description: "Konventionel letmælk med fedtprocent på 1,5%"),
|
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
||||||
amount: 1),
|
|
||||||
CartItem(
|
|
||||||
product: Product(
|
|
||||||
id: 1,
|
|
||||||
name: "Letmælk",
|
|
||||||
price: 13,
|
|
||||||
description: "Konventionel letmælk med fedtprocent på 1,5%"),
|
|
||||||
amount: 1),
|
|
||||||
CartItem(
|
|
||||||
product: Product(
|
|
||||||
id: 1,
|
|
||||||
name: "Letmælk",
|
|
||||||
price: 13,
|
|
||||||
description: "Konventionel letmælk med fedtprocent på 1,5%"),
|
|
||||||
amount: 1),
|
|
||||||
CartItem(
|
|
||||||
product: Product(
|
|
||||||
id: 1,
|
|
||||||
name: "Letmælk",
|
|
||||||
price: 13,
|
|
||||||
description: "Konventionel letmælk med fedtprocent på 1,5%"),
|
|
||||||
amount: 1),
|
|
||||||
CartItem(
|
|
||||||
product: Product(
|
|
||||||
id: 1,
|
|
||||||
name: "Letmælk",
|
|
||||||
price: 13,
|
|
||||||
description: "Konventionel letmælk med fedtprocent på 1,5%"),
|
|
||||||
amount: 1),
|
|
||||||
CartItem(
|
|
||||||
product: Product(
|
|
||||||
id: 1,
|
|
||||||
name: "Letmælk",
|
|
||||||
price: 13,
|
|
||||||
description: "Konventionel letmælk med fedtprocent på 1,5%"),
|
|
||||||
amount: 1),
|
amount: 1),
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -115,7 +80,7 @@ class CartRepo extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
addToCart(Product product) {
|
void addToCart(Product product) {
|
||||||
final cartItem = withProductId(product.id);
|
final cartItem = withProductId(product.id);
|
||||||
if (cartItem == null) {
|
if (cartItem == null) {
|
||||||
cart.add(CartItem(product: product, amount: 1));
|
cart.add(CartItem(product: product, amount: 1));
|
||||||
@ -125,14 +90,19 @@ class CartRepo extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
totalItemsInCart() {
|
int totalItemsInCart() {
|
||||||
return cart.fold<int>(0, (prev, cartItem) => prev + cartItem.amount);
|
return cart.fold<int>(0, (prev, cartItem) => prev + cartItem.amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
totalPrice() {
|
int totalPrice() {
|
||||||
return cart.fold<int>(
|
return cart.fold<int>(
|
||||||
0, (prev, cartItem) => prev + cartItem.amount * cartItem.product.price);
|
0, (prev, cartItem) => prev + cartItem.amount * cartItem.product.price);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clearCart() {
|
||||||
|
cart.clear();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CartItem {
|
class CartItem {
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:mobile/repos/cart.dart';
|
||||||
import 'package:mobile/repos/product.dart';
|
import 'package:mobile/repos/product.dart';
|
||||||
|
|
||||||
class ReceiptRepo extends ChangeNotifier {
|
class ReceiptRepo extends ChangeNotifier {
|
||||||
|
int nextId = 0;
|
||||||
final List<Receipt> receipts = [
|
final List<Receipt> receipts = [
|
||||||
Receipt(
|
Receipt(
|
||||||
id: 0,
|
id: 0,
|
||||||
@ -9,30 +11,33 @@ class ReceiptRepo extends ChangeNotifier {
|
|||||||
receiptItems: [
|
receiptItems: [
|
||||||
ReceiptItem(
|
ReceiptItem(
|
||||||
product: Product(
|
product: Product(
|
||||||
id: 1,
|
id: 1243,
|
||||||
name: "Letmælk",
|
name: "Letmælk",
|
||||||
price: 13,
|
price: 13,
|
||||||
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
||||||
amount: 1),
|
amount: 1),
|
||||||
ReceiptItem(
|
ReceiptItem(
|
||||||
product: Product(
|
product: Product(
|
||||||
id: 0,
|
id: 340,
|
||||||
name: "Minimælk",
|
name: "Minimælk",
|
||||||
price: 12,
|
price: 12,
|
||||||
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
||||||
amount: 3),
|
amount: 3),
|
||||||
]),
|
]),
|
||||||
Receipt(id: 1, date: DateTime.now(), receiptItems: [
|
Receipt(
|
||||||
|
id: 1,
|
||||||
|
date: DateTime.fromMillisecondsSinceEpoch(1735031200000),
|
||||||
|
receiptItems: [
|
||||||
ReceiptItem(
|
ReceiptItem(
|
||||||
product: Product(
|
product: Product(
|
||||||
id: 1,
|
id: 12341,
|
||||||
name: "Letmælk",
|
name: "Letmælk",
|
||||||
price: 13,
|
price: 13,
|
||||||
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
||||||
amount: 3),
|
amount: 3),
|
||||||
ReceiptItem(
|
ReceiptItem(
|
||||||
product: Product(
|
product: Product(
|
||||||
id: 0,
|
id: 1234443,
|
||||||
name: "Minimælk",
|
name: "Minimælk",
|
||||||
price: 12,
|
price: 12,
|
||||||
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
description: "Konventionel minimælk med fedtprocent på 0,4%"),
|
||||||
@ -52,6 +57,18 @@ class ReceiptRepo extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void createReceipt(List<CartItem> cartItems) {
|
||||||
|
List<ReceiptItem> receiptItems = [];
|
||||||
|
for (var i = 0; i < cartItems.length; i++) {
|
||||||
|
final ReceiptItem receiptItem = ReceiptItem(
|
||||||
|
amount: cartItems[i].amount, product: cartItems[i].product);
|
||||||
|
receiptItems.add(receiptItem);
|
||||||
|
}
|
||||||
|
receipts.add(Receipt(
|
||||||
|
date: DateTime.now(), receiptItems: receiptItems, id: nextId++));
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Receipt {
|
class Receipt {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user