Skip to content

Commit 16adfa0

Browse files
Add Product controller, item details and cart screen functionality
1 parent 8f29bc9 commit 16adfa0

File tree

8 files changed

+249
-158
lines changed

8 files changed

+249
-158
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:emart/consts/consts.dart';
2+
3+
Widget loadingIndicator() => const Center(
4+
child: CircularProgressIndicator(
5+
valueColor: AlwaysStoppedAnimation(redColor),
6+
),
7+
);

lib/consts/firebase_consts.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ FirebaseFirestore firestore = FirebaseFirestore.instance;
66
User? currentUser = auth.currentUser;
77

88
// collections
9-
const usersCollections = "users";
9+
const usersCollection = "users";
10+
const productsCollection = "products";

lib/controllers/auth_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AuthController extends GetxController {
3434
// store user data
3535
storeUserdata({name, password, email}) async {
3636
DocumentReference store =
37-
firestore.collection(usersCollections).doc(currentUser!.uid);
37+
firestore.collection(usersCollection).doc(currentUser!.uid);
3838
await store.set({
3939
'name': name,
4040
'password': password,

lib/controllers/product_controller.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import 'package:get/get.dart';
44

55
class ProductController extends GetxController {
66
var subCat = [];
7+
var quantity = 0.obs;
8+
var colorIndex = 0.obs;
9+
var totalPrice = 0.obs;
710

811
getSubCategories(title) async {
912
subCat.clear();
@@ -16,4 +19,18 @@ class ProductController extends GetxController {
1619
subCat.add(category);
1720
}
1821
}
22+
23+
changeColorIndex(index) {
24+
colorIndex.value = index;
25+
}
26+
27+
increaseQuantity(totalQuantity) {
28+
if (quantity.value < totalQuantity) quantity.value++;
29+
}
30+
31+
decreaseQuantity() {
32+
if (quantity.value > 0) quantity.value--;
33+
}
34+
35+
calculateTotalPrice(price) => totalPrice.value = price * quantity.value;
1936
}

lib/controllers/profile_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ProfileController extends GetxController {
4545
// update profile
4646

4747
updateProfile() async {
48-
var store = firestore.collection(usersCollections).doc(currentUser!.uid);
48+
var store = firestore.collection(usersCollection).doc(currentUser!.uid);
4949
await store.set({
5050
'name': nameController.text,
5151
'password': newPassController.text,

lib/services/firestore_service.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import 'package:emart/consts/consts.dart';
33
class FirestoreService {
44
// get users data
55
static getUser(uid) => firestore
6-
.collection(usersCollections)
6+
.collection(usersCollection)
77
.where('id', isEqualTo: uid)
88
.snapshots();
9+
10+
// get products according category
11+
static getProducts(category) => firestore
12+
.collection(productsCollection)
13+
.where('p_category', isEqualTo: category)
14+
.snapshots();
915
}

lib/views/category_screen_view/category_details.dart

Lines changed: 100 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
// ignore_for_file: curly_braces_in_flow_control_structures
2+
3+
import 'package:cloud_firestore/cloud_firestore.dart';
14
import 'package:emart/common_widgets/bg_widget.dart';
5+
import 'package:emart/common_widgets/loading_indicator.dart';
26
import 'package:emart/consts/consts.dart';
37
import 'package:emart/controllers/product_controller.dart';
8+
import 'package:emart/services/firestore_service.dart';
49
import 'package:emart/views/category_screen_view/item_details.dart';
510
import 'package:get/get.dart';
611

@@ -18,86 +23,105 @@ class CategoryDetails extends StatelessWidget {
1823
return bgWidget(
1924
child: Scaffold(
2025
appBar: AppBar(title: title!.text.white.fontFamily(bold).make()),
21-
body: Container(
22-
padding: const EdgeInsets.all(12),
23-
child: Column(
24-
crossAxisAlignment: CrossAxisAlignment.start,
25-
children: [
26-
SingleChildScrollView(
27-
physics: const BouncingScrollPhysics(),
28-
scrollDirection: Axis.horizontal,
29-
child: Row(
30-
children: List.generate(
31-
controller.subCat.length,
32-
(index) => "${controller.subCat[index]}"
33-
.text
34-
.color(darkFontGrey)
35-
.size(12)
36-
.fontFamily(semibold)
37-
.makeCentered()
38-
.box
39-
.white
40-
.padding(const EdgeInsets.symmetric(horizontal: 4))
41-
.margin(const EdgeInsets.all(4))
42-
.roundedSM
43-
.size(120, 60)
44-
.make(),
45-
),
46-
),
47-
),
26+
body: StreamBuilder(
27+
stream: FirestoreService.getProducts(title),
28+
builder:
29+
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
30+
if (!snapshot.hasData)
31+
return loadingIndicator();
32+
else if (snapshot.data!.docs.isEmpty)
33+
return Center(
34+
child: "No Products found".text.color(darkFontGrey).make(),
35+
);
4836

49-
// products
50-
20.heightBox,
51-
Expanded(
52-
child: GridView.builder(
53-
physics: const BouncingScrollPhysics(),
54-
shrinkWrap: true,
55-
itemCount: 6,
56-
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
57-
crossAxisCount: 2,
58-
mainAxisExtent: 250,
59-
mainAxisSpacing: 8,
60-
crossAxisSpacing: 8,
61-
),
62-
itemBuilder: (context, index) {
63-
return Column(
64-
crossAxisAlignment: CrossAxisAlignment.start,
65-
children: [
66-
Image.asset(
67-
imgP5,
68-
width: 200,
69-
height: 150,
70-
fit: BoxFit.cover,
71-
),
72-
"Laptop 8GB/64GB"
37+
var data = snapshot.data!.docs;
38+
39+
return Container(
40+
padding: const EdgeInsets.all(12),
41+
child: Column(
42+
crossAxisAlignment: CrossAxisAlignment.start,
43+
children: [
44+
SingleChildScrollView(
45+
physics: const BouncingScrollPhysics(),
46+
scrollDirection: Axis.horizontal,
47+
child: Row(
48+
children: List.generate(
49+
controller.subCat.length,
50+
(index) => "${controller.subCat[index]}"
7351
.text
74-
.fontFamily(semibold)
7552
.color(darkFontGrey)
53+
.size(12)
54+
.fontFamily(semibold)
55+
.makeCentered()
56+
.box
57+
.white
58+
.padding(const EdgeInsets.symmetric(horizontal: 4))
59+
.margin(const EdgeInsets.all(4))
60+
.roundedSM
61+
.size(120, 60)
7662
.make(),
77-
10.heightBox,
78-
"\$600"
79-
.text
80-
.color(redColor)
81-
.fontFamily(bold)
82-
.size(16)
83-
.make(),
84-
],
85-
)
86-
.box
87-
.white
88-
.roundedSM
89-
.outerShadowSm
90-
.margin(const EdgeInsets.all(4))
91-
.padding(const EdgeInsets.all(12))
92-
.make()
93-
.onTap(() {
94-
Get.to(() => const ItemDetails(title: "Dumy Title"));
95-
});
96-
},
97-
),
98-
)
99-
],
100-
),
63+
),
64+
),
65+
),
66+
67+
// products
68+
20.heightBox,
69+
Expanded(
70+
child: GridView.builder(
71+
physics: const BouncingScrollPhysics(),
72+
shrinkWrap: true,
73+
itemCount: data.length,
74+
gridDelegate:
75+
const SliverGridDelegateWithFixedCrossAxisCount(
76+
crossAxisCount: 2,
77+
mainAxisExtent: 250,
78+
mainAxisSpacing: 8,
79+
crossAxisSpacing: 8,
80+
),
81+
itemBuilder: (context, index) {
82+
return Column(
83+
crossAxisAlignment: CrossAxisAlignment.start,
84+
children: [
85+
Image.network(
86+
data[index]['p_images'][0],
87+
width: 200,
88+
height: 150,
89+
fit: BoxFit.cover,
90+
),
91+
"${data[index]['p_name']}"
92+
.text
93+
.fontFamily(semibold)
94+
.color(darkFontGrey)
95+
.make(),
96+
10.heightBox,
97+
"${data[index]['p_price']}"
98+
.numCurrency
99+
.text
100+
.color(redColor)
101+
.fontFamily(bold)
102+
.size(16)
103+
.make(),
104+
],
105+
)
106+
.box
107+
.white
108+
.roundedSM
109+
.outerShadowSm
110+
.margin(const EdgeInsets.all(4))
111+
.padding(const EdgeInsets.all(12))
112+
.make()
113+
.onTap(() {
114+
Get.to(() => ItemDetails(
115+
title: "${data[index]['p_name']}",
116+
data: data[index]));
117+
});
118+
},
119+
),
120+
)
121+
],
122+
),
123+
);
124+
},
101125
),
102126
),
103127
);

0 commit comments

Comments
 (0)