mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
remove cart item dialog
This commit is contained in:
parent
ddedaada31
commit
d74fe3a706
@ -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<void> removeCartItemDialog(BuildContext context) {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
content: Text(
|
||||
"Er du sikker på, at du vil slette ${name.toLowerCase()} fra indkøbskurven?"),
|
||||
actions: <Widget>[
|
||||
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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user