File tree 9 files changed +128
-4
lines changed
9 files changed +128
-4
lines changed Original file line number Diff line number Diff line change 1
1
import 'dart:async' ;
2
- import 'package:cloud_firestore/cloud_firestore.dart' ;
3
- import 'package:firebase_auth/firebase_auth.dart' ;
4
2
import 'package:firebase_core/firebase_core.dart' ;
5
3
import 'package:flutter/material.dart' ;
4
+ import 'package:multivendor_shop/providers/cart.dart' ;
5
+ import 'package:multivendor_shop/providers/order.dart' ;
6
6
import 'package:multivendor_shop/views/splash/entry.dart' ;
7
+ import 'package:provider/provider.dart' ;
7
8
import 'constants/colors.dart' ;
8
9
import 'routes/routes.dart' ;
9
10
import 'firebase_options.dart' ;
@@ -35,7 +36,17 @@ class _MultiVendorState extends State<MultiVendor> {
35
36
primaryColor: primaryColor,
36
37
),
37
38
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
+ ),
39
50
routes: routes,
40
51
);
41
52
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -507,7 +507,7 @@ packages:
507
507
source: hosted
508
508
version: "4.2.4"
509
509
provider:
510
- dependency: transitive
510
+ dependency: "direct main"
511
511
description:
512
512
name: provider
513
513
url: "https://pub.dartlang.org"
Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ dependencies:
52
52
badges : ^2.0.3
53
53
floating_action_bubble : ^1.1.4
54
54
font_awesome_flutter : ^10.2.1
55
+ provider : ^6.0.3
55
56
56
57
dev_dependencies :
57
58
flutter_test :
You can’t perform that action at this time.
0 commit comments