Skip to content

Commit c4fd662

Browse files
Add featured product on Home page
1 parent d985122 commit c4fd662

File tree

2 files changed

+69
-34
lines changed

2 files changed

+69
-34
lines changed

lib/services/firestore_service.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,10 @@ class FirestoreService {
7777
// get all products
7878
static getAllProducts() =>
7979
firestore.collection(productsCollection).snapshots();
80+
81+
// get featured products
82+
static getFeaturedProduct() => firestore
83+
.collection(productsCollection)
84+
.where('p_is_featured', isEqualTo: true)
85+
.get();
8086
}

lib/views/home_screen_view/home_screen.dart

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: curly_braces_in_flow_control_structures
2+
13
import 'package:cloud_firestore/cloud_firestore.dart';
24
import 'package:emart/common_widgets/home_buttons.dart';
35
import 'package:emart/common_widgets/loading_indicator.dart';
@@ -152,8 +154,9 @@ class HomeScreen extends StatelessWidget {
152154
Container(
153155
width: double.infinity,
154156
padding: const EdgeInsets.all(12),
155-
decoration: const BoxDecoration(
157+
decoration: BoxDecoration(
156158
color: redColor,
159+
borderRadius: BorderRadius.circular(8),
157160
),
158161
child: Column(
159162
crossAxisAlignment: CrossAxisAlignment.start,
@@ -166,40 +169,66 @@ class HomeScreen extends StatelessWidget {
166169
10.heightBox,
167170
SingleChildScrollView(
168171
scrollDirection: Axis.horizontal,
169-
child: Row(
170-
children: List.generate(
171-
6,
172-
(index) => Column(
173-
crossAxisAlignment: CrossAxisAlignment.start,
174-
children: [
175-
Image.asset(
176-
imgP1,
177-
width: 130,
178-
fit: BoxFit.cover,
172+
child: FutureBuilder(
173+
future: FirestoreService.getFeaturedProduct(),
174+
builder: (BuildContext context,
175+
AsyncSnapshot<QuerySnapshot> snapshot) {
176+
if (!snapshot.hasData)
177+
return loadingIndicator();
178+
else if (snapshot.data!.docs.isEmpty)
179+
return "No Featured Product"
180+
.text
181+
.white
182+
.makeCentered();
183+
var featuredData = snapshot.data!.docs;
184+
return Row(
185+
children: List.generate(
186+
featuredData.length,
187+
(index) => Column(
188+
crossAxisAlignment:
189+
CrossAxisAlignment.start,
190+
children: [
191+
Image.network(
192+
featuredData[index]['p_images'][0],
193+
fit: BoxFit.cover,
194+
width: 130,
195+
height: 140,
196+
),
197+
10.heightBox,
198+
"${featuredData[index]['p_name']}"
199+
.text
200+
.fontFamily(semibold)
201+
.color(darkFontGrey)
202+
.make(),
203+
10.heightBox,
204+
"\$${featuredData[index]['p_price']}"
205+
.text
206+
.color(redColor)
207+
.fontFamily(bold)
208+
.size(16)
209+
.make(),
210+
],
211+
)
212+
.box
213+
.white
214+
.roundedSM
215+
.margin(const EdgeInsets.symmetric(
216+
horizontal: 4))
217+
.padding(const EdgeInsets.all(8))
218+
.make()
219+
.onTap(
220+
() {
221+
Get.to(
222+
() => ItemDetails(
223+
title: featuredData[index]
224+
['p_name'],
225+
data: featuredData[index]),
226+
);
227+
},
179228
),
180-
10.heightBox,
181-
"Laptop 8GB/64GB"
182-
.text
183-
.fontFamily(semibold)
184-
.color(darkFontGrey)
185-
.make(),
186-
10.heightBox,
187-
"\$600"
188-
.text
189-
.color(redColor)
190-
.fontFamily(bold)
191-
.size(16)
192-
.make(),
193-
],
194-
)
195-
.box
196-
.white
197-
.roundedSM
198-
.margin(const EdgeInsets.symmetric(
199-
horizontal: 4))
200-
.padding(const EdgeInsets.all(8))
201-
.make(),
202-
),
229+
),
230+
);
231+
},
203232
),
204233
),
205234
],

0 commit comments

Comments
 (0)