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