Skip to content

Commit 0f2d351

Browse files
Add filter products functionality
1 parent 1a9a86d commit 0f2d351

File tree

2 files changed

+88
-51
lines changed

2 files changed

+88
-51
lines changed

lib/services/firestore_service.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,10 @@ class FirestoreService {
8787
// get search products
8888
static getSearchProducts(title) =>
8989
firestore.collection(productsCollection).get();
90+
91+
// get subcategory products
92+
static getSubcategoryProducts(title) => firestore
93+
.collection(productsCollection)
94+
.where('p_subcategory', isEqualTo: title)
95+
.snapshots();
9096
}

lib/views/category_screen_view/category_details.dart

Lines changed: 82 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,95 @@ import 'package:emart/services/firestore_service.dart';
99
import 'package:emart/views/category_screen_view/item_details.dart';
1010
import 'package:get/get.dart';
1111

12-
class CategoryDetails extends StatelessWidget {
12+
class CategoryDetails extends StatefulWidget {
1313
final String? title;
1414
const CategoryDetails({
1515
super.key,
1616
required this.title,
1717
});
1818

1919
@override
20-
Widget build(BuildContext context) {
21-
var controller = Get.find<ProductController>();
20+
State<CategoryDetails> createState() => _CategoryDetailsState();
21+
}
2222

23-
return bgWidget(
24-
child: Scaffold(
25-
appBar: AppBar(title: title!.text.white.fontFamily(bold).make()),
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-
);
23+
class _CategoryDetailsState extends State<CategoryDetails> {
24+
@override
25+
void initState() {
26+
super.initState();
27+
switchCategory(widget.title);
28+
}
3629

37-
var data = snapshot.data!.docs;
30+
switchCategory(title) {
31+
if (controller.subCat.contains(title))
32+
productMethod = FirestoreService.getSubcategoryProducts(title);
33+
else
34+
productMethod = FirestoreService.getProducts(title);
35+
}
3836

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]}"
51-
.text
52-
.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)
62-
.make(),
63-
),
64-
),
37+
var controller = Get.find<ProductController>();
38+
39+
dynamic productMethod;
40+
41+
@override
42+
Widget build(BuildContext context) {
43+
return bgWidget(
44+
child: Scaffold(
45+
appBar: AppBar(title: widget.title!.text.white.fontFamily(bold).make()),
46+
body: Column(
47+
crossAxisAlignment: CrossAxisAlignment.start,
48+
children: [
49+
SingleChildScrollView(
50+
physics: const BouncingScrollPhysics(),
51+
scrollDirection: Axis.horizontal,
52+
child: Padding(
53+
padding: const EdgeInsets.only(left: 12),
54+
child: Row(
55+
children: List.generate(
56+
controller.subCat.length,
57+
(index) => "${controller.subCat[index]}"
58+
.text
59+
.color(darkFontGrey)
60+
.size(12)
61+
.fontFamily(semibold)
62+
.makeCentered()
63+
.box
64+
.white
65+
.padding(const EdgeInsets.symmetric(horizontal: 4))
66+
.margin(const EdgeInsets.all(4))
67+
.roundedSM
68+
.size(120, 60)
69+
.make()
70+
.onTap(() {
71+
switchCategory("${controller.subCat[index]}");
72+
print(index);
73+
setState(() {});
74+
}),
6575
),
76+
),
77+
),
78+
),
79+
20.heightBox,
80+
StreamBuilder(
81+
stream: productMethod,
82+
builder: (BuildContext context,
83+
AsyncSnapshot<QuerySnapshot> snapshot) {
84+
if (!snapshot.hasData)
85+
return Expanded(child: loadingIndicator());
86+
else if (snapshot.data!.docs.isEmpty)
87+
return Expanded(
88+
child: "No Products found"
89+
.text
90+
.color(darkFontGrey)
91+
.makeCentered(),
92+
);
93+
94+
var data = snapshot.data!.docs;
6695

67-
// products
68-
20.heightBox,
69-
Expanded(
96+
return
97+
// products
98+
Expanded(
99+
child: Padding(
100+
padding: const EdgeInsets.all(12.0),
70101
child: GridView.builder(
71102
physics: const BouncingScrollPhysics(),
72103
shrinkWrap: true,
@@ -118,11 +149,11 @@ class CategoryDetails extends StatelessWidget {
118149
});
119150
},
120151
),
121-
)
122-
],
123-
),
124-
);
125-
},
152+
),
153+
);
154+
},
155+
),
156+
],
126157
),
127158
),
128159
);

0 commit comments

Comments
 (0)