mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +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)),
|
icon: const Icon(Icons.add)),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
if (cartRepo.willRemoveOnNextDecrement(
|
||||||
|
productId)) {
|
||||||
|
removeCartItemDialog(context);
|
||||||
|
} else {
|
||||||
cartRepo.decrementAmount(productId);
|
cartRepo.decrementAmount(productId);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
icon: const Icon(Icons.remove))
|
icon: const Icon(Icons.remove))
|
||||||
],
|
],
|
||||||
@ -75,15 +80,54 @@ class CartItemView extends StatelessWidget {
|
|||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () => removeCartItemDialog(context),
|
||||||
cartRepo.removeCartItem(productId);
|
|
||||||
},
|
|
||||||
icon: const Icon(Icons.delete_outline)),
|
icon: const Icon(Icons.delete_outline)),
|
||||||
Image(width: 100, image: AssetImage(imagePath))
|
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 {
|
class CartPage extends StatelessWidget {
|
||||||
|
@ -78,7 +78,7 @@ class CartRepo extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void incrementAmount(int productId) {
|
void incrementAmount(int productId) {
|
||||||
var cartItem = withProductId(productId);
|
final cartItem = withProductId(productId);
|
||||||
if (cartItem == null) {
|
if (cartItem == null) {
|
||||||
throw ProductIdException();
|
throw ProductIdException();
|
||||||
}
|
}
|
||||||
@ -87,18 +87,27 @@ class CartRepo extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void decrementAmount(int productId) {
|
void decrementAmount(int productId) {
|
||||||
var cartItem = withProductId(productId);
|
final cartItem = withProductId(productId);
|
||||||
if (cartItem == null) {
|
if (cartItem == null) {
|
||||||
throw ProductIdException();
|
throw ProductIdException();
|
||||||
}
|
}
|
||||||
if (--cartItem.amount <= 0) {
|
cartItem.amount -= 1;
|
||||||
|
if (cartItem.amount <= 0) {
|
||||||
cart.remove(cartItem);
|
cart.remove(cartItem);
|
||||||
}
|
}
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool willRemoveOnNextDecrement(int productId) {
|
||||||
|
final cartItem = withProductId(productId);
|
||||||
|
if (cartItem == null) {
|
||||||
|
throw ProductIdException();
|
||||||
|
}
|
||||||
|
return cartItem.amount <= 1;
|
||||||
|
}
|
||||||
|
|
||||||
void removeCartItem(int productId) {
|
void removeCartItem(int productId) {
|
||||||
var cartItem = withProductId(productId);
|
final cartItem = withProductId(productId);
|
||||||
if (cartItem == null) {
|
if (cartItem == null) {
|
||||||
throw ProductIdException();
|
throw ProductIdException();
|
||||||
}
|
}
|
||||||
@ -107,7 +116,7 @@ class CartRepo extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addToCart(Product product) {
|
addToCart(Product product) {
|
||||||
var 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));
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user