Skip to content

Commit 72cccd1

Browse files
Add delete from cart and updating database
1 parent 30651bb commit 72cccd1

File tree

3 files changed

+125
-8
lines changed

3 files changed

+125
-8
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:get/get.dart';
2+
3+
class CartCotroller extends GetxController {
4+
var totalPrice = 0.obs;
5+
6+
// calculate total price of item present in cart
7+
calculateTotalPrice(data) {
8+
totalPrice.value = 0;
9+
for (var price = 0; price < data.length; price++) {
10+
totalPrice.value =
11+
totalPrice.value + int.parse(data[price]['total_price'].toString());
12+
}
13+
}
14+
}

lib/services/firestore_service.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ class FirestoreService {
1212
.collection(productsCollection)
1313
.where('p_category', isEqualTo: category)
1414
.snapshots();
15+
16+
// get cart
17+
static getCart(uid) => firestore
18+
.collection(cartCollection)
19+
.where('added_by', isEqualTo: uid)
20+
.snapshots();
21+
22+
// delete cart document from databse
23+
static deleteCartDocument(docId) =>
24+
firestore.collection(cartCollection).doc(docId).delete();
1525
}
Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,110 @@
1+
// ignore_for_file: curly_braces_in_flow_control_structures
2+
3+
import 'package:cloud_firestore/cloud_firestore.dart';
4+
import 'package:emart/common_widgets/custom_button.dart';
5+
import 'package:emart/common_widgets/loading_indicator.dart';
16
import 'package:emart/consts/consts.dart';
7+
import 'package:emart/controllers/cart_controller.dart';
8+
import 'package:emart/services/firestore_service.dart';
9+
import 'package:get/get.dart';
210

311
class CartScreen extends StatelessWidget {
412
const CartScreen({super.key});
513

614
@override
715
Widget build(BuildContext context) {
8-
return Container(
9-
color: Colors.white,
10-
child: "Cart is empty"
11-
.text
12-
.fontFamily(semibold)
13-
.color(darkFontGrey)
14-
.makeCentered(),
15-
);
16+
var controlller = Get.put(CartCotroller());
17+
18+
return Scaffold(
19+
backgroundColor: whiteColor,
20+
appBar: AppBar(
21+
automaticallyImplyLeading: false,
22+
title: "Shopping Cart"
23+
.text
24+
.fontFamily(semibold)
25+
.color(darkFontGrey)
26+
.make(),
27+
),
28+
body: StreamBuilder(
29+
stream: FirestoreService.getCart(currentUser!.uid),
30+
builder:
31+
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
32+
if (!snapshot.hasData)
33+
return loadingIndicator();
34+
else if (snapshot.data!.docs.isEmpty)
35+
return Center(
36+
child: "Cart is Empty".text.color(darkFontGrey).make(),
37+
);
38+
var data = snapshot.data!.docs;
39+
controlller.calculateTotalPrice(data);
40+
return Padding(
41+
padding: const EdgeInsets.all(8.0),
42+
child: Column(
43+
children: [
44+
Expanded(
45+
child: ListView.builder(
46+
itemCount: data.length,
47+
itemBuilder: (context, index) => ListTile(
48+
leading: Image.network("${data[index]['image']}"),
49+
title:
50+
"${data[index]['title']} (x${data[index]['quantity']})"
51+
.text
52+
.fontFamily(semibold)
53+
.size(16)
54+
.make(),
55+
subtitle: "${data[index]['total_price']}"
56+
.numCurrency
57+
.text
58+
.fontFamily(semibold)
59+
.size(16)
60+
.color(redColor)
61+
.make(),
62+
trailing: IconButton(
63+
onPressed: () =>
64+
FirestoreService.deleteCartDocument(
65+
data[index].id),
66+
icon: const Icon(Icons.delete, color: redColor),
67+
),
68+
),
69+
),
70+
),
71+
Row(
72+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
73+
children: [
74+
"Total Price"
75+
.text
76+
.fontFamily(semibold)
77+
.color(darkFontGrey)
78+
.make(),
79+
Obx(
80+
() => "${controlller.totalPrice.value}"
81+
.numCurrency
82+
.text
83+
.fontFamily(semibold)
84+
.color(redColor)
85+
.make(),
86+
)
87+
],
88+
)
89+
.box
90+
.padding(const EdgeInsets.all(12))
91+
.color(lightGolden)
92+
.width(context.screenWidth - 60)
93+
.roundedSM
94+
.make(),
95+
10.heightBox,
96+
SizedBox(
97+
width: context.screenWidth - 60,
98+
child: customButton(
99+
color: redColor,
100+
onPressed: () {},
101+
textColor: whiteColor,
102+
title: "Proceed to Shipping",
103+
),
104+
),
105+
],
106+
),
107+
);
108+
}));
16109
}
17110
}

0 commit comments

Comments
 (0)