From d74fe3a7066c115b10adfb0e450bca5b7dbdb109 Mon Sep 17 00:00:00 2001 From: Mikkel Troels Kongsted Date: Mon, 3 Feb 2025 13:14:28 +0100 Subject: [PATCH] remove cart item dialog --- mobile/lib/pages/cart_page.dart | 52 ++++++++++++++++++++++++++++++--- mobile/lib/repos/cart.dart | 19 ++++++++---- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/mobile/lib/pages/cart_page.dart b/mobile/lib/pages/cart_page.dart index ac1869f..51c11f3 100644 --- a/mobile/lib/pages/cart_page.dart +++ b/mobile/lib/pages/cart_page.dart @@ -62,7 +62,12 @@ class CartItemView extends StatelessWidget { icon: const Icon(Icons.add)), IconButton( onPressed: () { - cartRepo.decrementAmount(productId); + if (cartRepo.willRemoveOnNextDecrement( + productId)) { + removeCartItemDialog(context); + } else { + cartRepo.decrementAmount(productId); + } }, icon: const Icon(Icons.remove)) ], @@ -75,15 +80,54 @@ class CartItemView extends StatelessWidget { )), ), IconButton( - onPressed: () { - cartRepo.removeCartItem(productId); - }, + onPressed: () => removeCartItemDialog(context), icon: const Icon(Icons.delete_outline)), Image(width: 100, image: AssetImage(imagePath)) ], ), ); } + + Future removeCartItemDialog(BuildContext context) { + return showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: Text( + "Er du sikker på, at du vil slette ${name.toLowerCase()} fra indkøbskurven?"), + actions: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Container( + margin: const EdgeInsets.only(right: 5), + child: TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.red, + foregroundColor: Colors.white), + child: const Text('Slet'), + onPressed: () { + cartRepo.removeCartItem(productId); + Navigator.of(context).pop(); + }, + ), + ), + Container( + margin: const EdgeInsets.only(left: 5), + child: PrimaryButton( + child: const Text('Annullér'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ), + ], + ) + ], + ); + }, + ); + } } class CartPage extends StatelessWidget { diff --git a/mobile/lib/repos/cart.dart b/mobile/lib/repos/cart.dart index dd6b581..5d04576 100644 --- a/mobile/lib/repos/cart.dart +++ b/mobile/lib/repos/cart.dart @@ -78,7 +78,7 @@ class CartRepo extends ChangeNotifier { } void incrementAmount(int productId) { - var cartItem = withProductId(productId); + final cartItem = withProductId(productId); if (cartItem == null) { throw ProductIdException(); } @@ -87,18 +87,27 @@ class CartRepo extends ChangeNotifier { } void decrementAmount(int productId) { - var cartItem = withProductId(productId); + final cartItem = withProductId(productId); if (cartItem == null) { throw ProductIdException(); } - if (--cartItem.amount <= 0) { + cartItem.amount -= 1; + if (cartItem.amount <= 0) { cart.remove(cartItem); } notifyListeners(); } + bool willRemoveOnNextDecrement(int productId) { + final cartItem = withProductId(productId); + if (cartItem == null) { + throw ProductIdException(); + } + return cartItem.amount <= 1; + } + void removeCartItem(int productId) { - var cartItem = withProductId(productId); + final cartItem = withProductId(productId); if (cartItem == null) { throw ProductIdException(); } @@ -107,7 +116,7 @@ class CartRepo extends ChangeNotifier { } addToCart(Product product) { - var cartItem = withProductId(product.id); + final cartItem = withProductId(product.id); if (cartItem == null) { cart.add(CartItem(product: product, amount: 1)); } else {