Skip to content

Commit

Permalink
task: adding/removing from cart
Browse files Browse the repository at this point in the history
  • Loading branch information
Atuoha committed Oct 10, 2022
1 parent 0401abc commit 421efe3
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 448 deletions.
24 changes: 12 additions & 12 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ Future<void> main() async {
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(
const MultiVendor(),
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => CartData(),
),
ChangeNotifierProvider(
create: (context) => OrderData(),
),
],
child: const MultiVendor(),
),
);
}

Expand All @@ -36,17 +46,7 @@ class _MultiVendorState extends State<MultiVendor> {
primaryColor: primaryColor,
),
debugShowCheckedModeBanner: false,
home: MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => CartData(),
),
ChangeNotifierProvider(
create: (context) => OrderData(),
),
],
child: const EntryScreen(),
),
home: const EntryScreen(),
routes: routes,
);
}
Expand Down
20 changes: 16 additions & 4 deletions lib/providers/cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ 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);
}

void incrementProductQuantity(String id) {
var cartItem = _cartItems.firstWhere((item) => item.id == id);
var cartItem = _cartItems.firstWhere(
(item) => item.id == id,
);
cartItem.incrementQuantity();
notifyListeners();
}

void decrementProductQuantity(String id) {
var cartItem = _cartItems.firstWhere((item) => item.id == id);
var cartItem = _cartItems.firstWhere(
(item) => item.id == id,
);
cartItem.decrementQuantity();
notifyListeners();
}
Expand All @@ -28,12 +36,16 @@ class CartData extends ChangeNotifier {

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

void removeFromCart(String id) {
var cartItem = _cartItems.firstWhere((item) => item.id == id);
void removeFromCart(String prodId) {
var cartItem = _cartItems.firstWhere(
(item) => item.prodId == prodId,
);
_cartItems.remove(cartItem);
notifyListeners();
print('ITEM IS SUCCESSFULLY REMOVED FROM CART');
}

get cartItemCount {
Expand Down
4 changes: 3 additions & 1 deletion lib/providers/order.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class OrderData extends ChangeNotifier {
}

void removeFromOrder(String id) {
var orderItem = _orderItems.firstWhere((item) => item.id == id);
var orderItem = _orderItems.firstWhere(
(item) => item.id == id,
);
_orderItems.remove(orderItem);
notifyListeners();
}
Expand Down
Loading

0 comments on commit 421efe3

Please sign in to comment.