Skip to content

Commit b0e9a0f

Browse files
committed
task: introducing provider for cart and order
1 parent 040d2e3 commit b0e9a0f

File tree

9 files changed

+128
-4
lines changed

9 files changed

+128
-4
lines changed

lib/main.dart

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import 'dart:async';
2-
import 'package:cloud_firestore/cloud_firestore.dart';
3-
import 'package:firebase_auth/firebase_auth.dart';
42
import 'package:firebase_core/firebase_core.dart';
53
import 'package:flutter/material.dart';
4+
import 'package:multivendor_shop/providers/cart.dart';
5+
import 'package:multivendor_shop/providers/order.dart';
66
import 'package:multivendor_shop/views/splash/entry.dart';
7+
import 'package:provider/provider.dart';
78
import 'constants/colors.dart';
89
import 'routes/routes.dart';
910
import 'firebase_options.dart';
@@ -35,7 +36,17 @@ class _MultiVendorState extends State<MultiVendor> {
3536
primaryColor: primaryColor,
3637
),
3738
debugShowCheckedModeBanner: false,
38-
home: const EntryScreen(),
39+
home: MultiProvider(
40+
providers: [
41+
ChangeNotifierProvider(
42+
create: (context) => CartData(),
43+
),
44+
ChangeNotifierProvider(
45+
create: (context) => OrderData(),
46+
),
47+
],
48+
child: const EntryScreen(),
49+
),
3950
routes: routes,
4051
);
4152
}

lib/models/cart.dart

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class CartItem {
2+
final String id;
3+
final String prodId;
4+
final String prodName;
5+
final String prodImgUrl;
6+
double prodPrice;
7+
double totalPrice;
8+
int quantity;
9+
10+
CartItem({
11+
required this.id,
12+
required this.prodId,
13+
required this.prodName,
14+
required this.prodPrice,
15+
required this.prodImgUrl,
16+
this.quantity = 1,
17+
required this.totalPrice,
18+
});
19+
20+
incrementQuantity() {
21+
quantity += 1;
22+
totalPrice += prodPrice;
23+
}
24+
25+
decrementQuantity() {
26+
quantity -= 1;
27+
totalPrice -= prodPrice;
28+
}
29+
}

lib/models/order.dart

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Order {
2+
final String id;
3+
final String prodId;
4+
final String prodName;
5+
final String prodImg;
6+
final int quantity;
7+
8+
Order({
9+
required this.id,
10+
required this.prodId,
11+
required this.prodName,
12+
required this.prodImg,
13+
required this.quantity,
14+
});
15+
}

lib/providers/cart.dart

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:flutter/material.dart';
2+
3+
class CartData extends ChangeNotifier {
4+
var totalPrice = 0.0;
5+
6+
void incrementProductQuantity(String id) {
7+
var cartItem = _cartItems.firstWhere((item) => item.id == id);
8+
cartItem.incrementQuantity();
9+
notifyListeners();
10+
}
11+
12+
void decrementProductQuantity(String id) {
13+
var cartItem = _cartItems.firstWhere((item) => item.id == id);
14+
cartItem.decrementQuantity();
15+
notifyListeners();
16+
}
17+
18+
void removeFromCart(String id) {
19+
var cartItem = _cartItems.firstWhere((item) => item.id == id);
20+
_cartItems.remove(cartItem);
21+
notifyListeners();
22+
}
23+
24+
get cartItemCount {
25+
return _cartItems.length;
26+
}
27+
28+
get cartTotalPrice {
29+
for (var item in _cartItems) {
30+
totalPrice += item.totalPrice;
31+
}
32+
return totalPrice;
33+
}
34+
35+
get cartItems {
36+
return [..._cartItems];
37+
}
38+
39+
final _cartItems = [];
40+
}

lib/providers/order.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter/material.dart';
2+
3+
class OrderData extends ChangeNotifier {
4+
var totalPrice = 0.0;
5+
6+
void removeFromOrder(String id) {
7+
var orderItem = _orderItems.firstWhere((item) => item.id == id);
8+
_orderItems.remove(orderItem);
9+
notifyListeners();
10+
}
11+
12+
get orderItemCount {
13+
return _orderItems.length;
14+
}
15+
16+
get orderItems {
17+
return [..._orderItems];
18+
}
19+
20+
get orderTotalPrice {
21+
for (var item in _orderItems) {
22+
totalPrice += item.totalPrice;
23+
}
24+
return totalPrice;
25+
}
26+
27+
final _orderItems = [];
28+
}

pubspec.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ packages:
507507
source: hosted
508508
version: "4.2.4"
509509
provider:
510-
dependency: transitive
510+
dependency: "direct main"
511511
description:
512512
name: provider
513513
url: "https://pub.dartlang.org"

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies:
5252
badges: ^2.0.3
5353
floating_action_bubble: ^1.1.4
5454
font_awesome_flutter: ^10.2.1
55+
provider: ^6.0.3
5556

5657
dev_dependencies:
5758
flutter_test:

screenshots/product_adj.png

1.28 MB
Loading

screenshots/store_adj.png

2.26 MB
Loading

0 commit comments

Comments
 (0)