mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
cart icon badge
This commit is contained in:
parent
135c9f9429
commit
9b74452b94
@ -75,7 +75,7 @@ class CartItemView extends StatelessWidget {
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
cartRepo.removeCartItem(cartRepo.withProductId(productId));
|
||||
cartRepo.removeCartItem(productId);
|
||||
},
|
||||
icon: const Icon(Icons.delete_outline)),
|
||||
Image(width: 100, image: AssetImage(imagePath))
|
||||
|
@ -3,6 +3,7 @@ import 'package:mobile/pages/all_products_page.dart';
|
||||
import 'package:mobile/pages/cart_page.dart';
|
||||
import 'package:mobile/pages/receipts_page.dart';
|
||||
import 'package:mobile/repos/bottom_navigation_bar.dart';
|
||||
import 'package:mobile/repos/cart.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class Dashboard extends StatelessWidget {
|
||||
@ -18,6 +19,7 @@ class Dashboard extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final pageIndexProvider = Provider.of<BottomNavigationBarRepo>(context);
|
||||
int currentIndex = pageIndexProvider.currentIndex;
|
||||
final CartRepo cartRepo = context.watch<CartRepo>();
|
||||
|
||||
return Scaffold(
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
@ -28,9 +30,12 @@ class Dashboard extends StatelessWidget {
|
||||
icon: Icon(currentIndex == 0 ? Icons.home : Icons.home_outlined),
|
||||
label: "Home"),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(currentIndex == 1
|
||||
? Icons.shopping_cart
|
||||
: Icons.shopping_cart_outlined),
|
||||
icon: Badge.count(
|
||||
count: cartRepo.totalItemsInCart(),
|
||||
child: Icon(currentIndex == 1
|
||||
? Icons.shopping_cart
|
||||
: Icons.shopping_cart_outlined),
|
||||
),
|
||||
label: "Cart"),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(currentIndex == 2
|
||||
|
@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mobile/repos/cart.dart';
|
||||
import 'package:mobile/repos/product.dart';
|
||||
import 'package:mobile/widgets/primary_button.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class ProductPage extends StatelessWidget {
|
||||
final Product product;
|
||||
@ -70,7 +72,10 @@ class ProductPage extends StatelessWidget {
|
||||
PrimaryButton(
|
||||
onPressed: () {}, child: const Text("Find i butik")),
|
||||
PrimaryButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
var cartRepo = context.read<CartRepo>();
|
||||
cartRepo.addToCart(product);
|
||||
},
|
||||
child: const Text("Tilføj til indkøbskurv")),
|
||||
],
|
||||
),
|
||||
|
@ -68,33 +68,57 @@ class CartRepo extends ChangeNotifier {
|
||||
return cart;
|
||||
}
|
||||
|
||||
CartItem withProductId(int productId) {
|
||||
CartItem? withProductId(int productId) {
|
||||
for (var i = 0; i < cart.length; i++) {
|
||||
if (cart[i].product.id == productId) {
|
||||
return cart[i];
|
||||
}
|
||||
}
|
||||
throw ProductIdException();
|
||||
return null;
|
||||
}
|
||||
|
||||
void incrementAmount(int productId) {
|
||||
var cartItem = withProductId(productId);
|
||||
if (cartItem == null) {
|
||||
throw ProductIdException();
|
||||
}
|
||||
cartItem.amount++;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void decrementAmount(int productId) {
|
||||
var cartItem = withProductId(productId);
|
||||
if (cartItem == null) {
|
||||
throw ProductIdException();
|
||||
}
|
||||
if (--cartItem.amount <= 0) {
|
||||
removeCartItem(cartItem);
|
||||
cart.remove(cartItem);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void removeCartItem(CartItem cartItem) {
|
||||
void removeCartItem(int productId) {
|
||||
var cartItem = withProductId(productId);
|
||||
if (cartItem == null) {
|
||||
throw ProductIdException();
|
||||
}
|
||||
cart.remove(cartItem);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
addToCart(Product product) {
|
||||
var cartItem = withProductId(product.id);
|
||||
if (cartItem == null) {
|
||||
cart.add(CartItem(product: product, amount: 1));
|
||||
} else {
|
||||
cartItem.amount++;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
totalItemsInCart() {
|
||||
return cart.fold<int>(0, (prev, cartItem) => prev + cartItem.amount);
|
||||
}
|
||||
}
|
||||
|
||||
class CartItem {
|
||||
|
Loading…
x
Reference in New Issue
Block a user