Skip to content

Commit 30c42f5

Browse files
Add All products on HOME VIEW
1 parent f0e50f8 commit 30c42f5

File tree

4 files changed

+84
-41
lines changed

4 files changed

+84
-41
lines changed

lib/consts/strings.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const loggedout = "Logged ou successfully";
1919
const updated = "Updated";
2020
const typeMessage = "Type a message...";
2121
const orderPlaced = "Order placed";
22+
const allProducts = "All Products";
2223

2324
const createNewAccount = "or, create new account";
2425
const loginWith = "Log in with";

lib/services/firestore_service.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,8 @@ class FirestoreService {
7373
(value) => value.docs.length,
7474
),
7575
]);
76+
77+
// get all products
78+
static getAllProducts() =>
79+
firestore.collection(productsCollection).snapshots();
7680
}

lib/views/category_screen_view/item_details.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ItemDetails extends StatelessWidget {
1818

1919
@override
2020
Widget build(BuildContext context) {
21-
var controller = Get.find<ProductController>();
21+
var controller = Get.put(ProductController());
2222

2323
return WillPopScope(
2424
onWillPop: () async {

lib/views/home_screen_view/home_screen.dart

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
12
import 'package:emart/common_widgets/home_buttons.dart';
3+
import 'package:emart/common_widgets/loading_indicator.dart';
24
import 'package:emart/consts/consts.dart';
35
import 'package:emart/consts/list.dart';
6+
import 'package:emart/services/firestore_service.dart';
7+
import 'package:emart/views/category_screen_view/item_details.dart';
48
import 'package:emart/views/home_screen_view/components/featured_button.dart';
9+
import 'package:get/get.dart';
510

611
class HomeScreen extends StatelessWidget {
712
const HomeScreen({super.key});
@@ -190,7 +195,8 @@ class HomeScreen extends StatelessWidget {
190195
.box
191196
.white
192197
.roundedSM
193-
.margin(const EdgeInsets.symmetric(horizontal: 4))
198+
.margin(const EdgeInsets.symmetric(
199+
horizontal: 4))
194200
.padding(const EdgeInsets.all(8))
195201
.make(),
196202
),
@@ -221,45 +227,77 @@ class HomeScreen extends StatelessWidget {
221227

222228
// All product section
223229
20.heightBox,
224-
GridView.builder(
225-
shrinkWrap: true,
226-
physics: const NeverScrollableScrollPhysics(),
227-
itemCount: 6,
228-
gridDelegate:
229-
const SliverGridDelegateWithFixedCrossAxisCount(
230-
crossAxisCount: 2,
231-
mainAxisSpacing: 8,
232-
crossAxisSpacing: 8,
233-
mainAxisExtent: 300,
234-
),
235-
itemBuilder: (context, index) {
236-
return Column(
237-
crossAxisAlignment: CrossAxisAlignment.start,
238-
children: [
239-
Image.asset(imgP5,
240-
width: 200, height: 200, fit: BoxFit.cover),
241-
const Spacer(),
242-
"Laptop 8GB/64GB"
243-
.text
244-
.fontFamily(semibold)
245-
.color(darkFontGrey)
246-
.make(),
247-
10.heightBox,
248-
"\$600"
249-
.text
250-
.color(redColor)
251-
.fontFamily(bold)
252-
.size(16)
253-
.make(),
254-
],
255-
)
256-
.box
257-
.white
258-
.roundedSM
259-
.margin(const EdgeInsets.all(4))
260-
.padding(const EdgeInsets.all(12))
261-
.make();
262-
})
230+
Align(
231+
alignment: Alignment.centerLeft,
232+
child: allProducts.text
233+
.fontFamily(bold)
234+
.color(darkFontGrey)
235+
.size(18)
236+
.make(),
237+
),
238+
20.heightBox,
239+
StreamBuilder(
240+
stream: FirestoreService.getAllProducts(),
241+
builder: (BuildContext context,
242+
AsyncSnapshot<QuerySnapshot> snapshot) {
243+
if (!snapshot.hasData) return loadingIndicator();
244+
var allProductData = snapshot.data!.docs;
245+
return GridView.builder(
246+
shrinkWrap: true,
247+
physics: const NeverScrollableScrollPhysics(),
248+
itemCount: allProductData.length,
249+
gridDelegate:
250+
const SliverGridDelegateWithFixedCrossAxisCount(
251+
crossAxisCount: 2,
252+
mainAxisSpacing: 8,
253+
crossAxisSpacing: 8,
254+
mainAxisExtent: 300,
255+
),
256+
itemBuilder: (context, index) {
257+
return Column(
258+
crossAxisAlignment: CrossAxisAlignment.start,
259+
children: [
260+
Image.network(
261+
allProductData[index]['p_images'][0],
262+
width: 200,
263+
height: 200,
264+
fit: BoxFit.cover,
265+
),
266+
const Spacer(),
267+
"${allProductData[index]['p_name']}"
268+
.text
269+
.fontFamily(semibold)
270+
.color(darkFontGrey)
271+
.make(),
272+
10.heightBox,
273+
"${allProductData[index]['p_price']}"
274+
.numCurrency
275+
.text
276+
.color(redColor)
277+
.fontFamily(bold)
278+
.size(16)
279+
.make(),
280+
],
281+
)
282+
.box
283+
.white
284+
.roundedSM
285+
.margin(const EdgeInsets.all(4))
286+
.padding(const EdgeInsets.all(12))
287+
.make()
288+
.onTap(
289+
() {
290+
Get.to(
291+
() => ItemDetails(
292+
title: allProductData[index]['p_name'],
293+
data: allProductData[index]),
294+
);
295+
},
296+
);
297+
},
298+
);
299+
},
300+
),
263301
],
264302
),
265303
),

0 commit comments

Comments
 (0)