Skip to content

Commit

Permalink
task: cart operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Atuoha committed Oct 10, 2022
1 parent f5200c3 commit 3bbdcb5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
9 changes: 5 additions & 4 deletions lib/providers/cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:multivendor_shop/models/cart.dart';
class CartData extends ChangeNotifier {
var totalPrice = 0.0;

bool isItemOnCart(String id) {
return _cartItems.any((item) => item.prodId == id);
bool isItemOnCart(String prodId) {
return _cartItems.any((item) => item.prodId == prodId);
}

void incrementProductQuantity(String id) {
Expand All @@ -21,6 +21,9 @@ class CartData extends ChangeNotifier {
(item) => item.id == id,
);
cartItem.decrementQuantity();
if (cartItem.quantity < 1) {
_cartItems.remove(cartItem);
}
notifyListeners();
}

Expand All @@ -36,7 +39,6 @@ class CartData extends ChangeNotifier {

_cartItems.add(item);
notifyListeners();
print('ITEM IS SUCCESSFULLY ADDED TO CART');
}

void removeFromCart(String prodId) {
Expand All @@ -45,7 +47,6 @@ class CartData extends ChangeNotifier {
);
_cartItems.remove(cartItem);
notifyListeners();
print('ITEM IS SUCCESSFULLY REMOVED FROM CART');
}

get cartItemCount {
Expand Down
45 changes: 29 additions & 16 deletions lib/views/main/customer/cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,27 @@ class _CartScreenState extends State<CartScreen> {

@override
Widget build(BuildContext context) {
var cartData = Provider.of<CartData>(context, listen: false);

// remove from cart
void removeFromCart(String prodId) {
cartData.removeFromCart(prodId);
}

// increase item quantity
void increaseQuantity(String id) {
cartData.incrementProductQuantity(id);
}

// decrease item quantity
void reduceQuantity(String id) {
cartData.decrementProductQuantity(id);
}

Size size = MediaQuery.of(context).size;

return Padding(
padding: const EdgeInsets.fromLTRB(
0,
40,
0,
0,
),
padding: const EdgeInsets.only(top: 40),
child: Stack(
children: [
Column(
Expand Down Expand Up @@ -160,7 +172,7 @@ class _CartScreenState extends State<CartScreen> {
scrollDirection: Axis.vertical,
itemCount: data.cartItemCount,
itemBuilder: (context, index) {
var prod = data.cartItems[index];
var item = data.cartItems[index];
return Card(
elevation: 3,
child: ListTile(
Expand All @@ -172,10 +184,10 @@ class _CartScreenState extends State<CartScreen> {
leading: CircleAvatar(
backgroundColor: primaryColor,
backgroundImage:
NetworkImage(prod.prodImgUrl),
NetworkImage(item.prodImgUrl),
),
title: Text(
prod.prodName,
item.prodName,
style: const TextStyle(
fontSize: 16,
),
Expand All @@ -184,28 +196,28 @@ class _CartScreenState extends State<CartScreen> {
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text('\$${prod.prodPrice}'),
Text('\$${item.totalPrice}'),
const SizedBox(height: 5),
Row(
children: [
GestureDetector(
onTap: () {},
onTap: ()=>increaseQuantity(item.id),
child: const Icon(
Icons.add,
color: primaryColor,
),
),
const SizedBox(width: 10),
Text(
'1',
style: TextStyle(
item.quantity.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
const SizedBox(width: 10),
GestureDetector(
onTap: () {},
onTap: ()=>reduceQuantity(item.id),
child: const Icon(
Icons.remove,
color: primaryColor,
Expand All @@ -216,8 +228,9 @@ class _CartScreenState extends State<CartScreen> {
],
),
trailing: IconButton(
onPressed: null,
icon: Icon(
onPressed: () =>
removeFromCart(item.prodId),
icon: const Icon(
Icons.delete_forever,
color: primaryColor,
),
Expand Down

0 comments on commit 3bbdcb5

Please sign in to comment.