Skip to content

Commit

Permalink
Add delete from cart and updating database
Browse files Browse the repository at this point in the history
  • Loading branch information
codewith-usama committed Nov 17, 2023
1 parent 30651bb commit 72cccd1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 8 deletions.
14 changes: 14 additions & 0 deletions lib/controllers/cart_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:get/get.dart';

class CartCotroller extends GetxController {
var totalPrice = 0.obs;

// calculate total price of item present in cart
calculateTotalPrice(data) {
totalPrice.value = 0;
for (var price = 0; price < data.length; price++) {
totalPrice.value =
totalPrice.value + int.parse(data[price]['total_price'].toString());
}
}
}
10 changes: 10 additions & 0 deletions lib/services/firestore_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ class FirestoreService {
.collection(productsCollection)
.where('p_category', isEqualTo: category)
.snapshots();

// get cart
static getCart(uid) => firestore
.collection(cartCollection)
.where('added_by', isEqualTo: uid)
.snapshots();

// delete cart document from databse
static deleteCartDocument(docId) =>
firestore.collection(cartCollection).doc(docId).delete();
}
109 changes: 101 additions & 8 deletions lib/views/cart_screen_view/cart_screen.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,110 @@
// ignore_for_file: curly_braces_in_flow_control_structures

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:emart/common_widgets/custom_button.dart';
import 'package:emart/common_widgets/loading_indicator.dart';
import 'package:emart/consts/consts.dart';
import 'package:emart/controllers/cart_controller.dart';
import 'package:emart/services/firestore_service.dart';
import 'package:get/get.dart';

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

@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: "Cart is empty"
.text
.fontFamily(semibold)
.color(darkFontGrey)
.makeCentered(),
);
var controlller = Get.put(CartCotroller());

return Scaffold(
backgroundColor: whiteColor,
appBar: AppBar(
automaticallyImplyLeading: false,
title: "Shopping Cart"
.text
.fontFamily(semibold)
.color(darkFontGrey)
.make(),
),
body: StreamBuilder(
stream: FirestoreService.getCart(currentUser!.uid),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData)
return loadingIndicator();
else if (snapshot.data!.docs.isEmpty)
return Center(
child: "Cart is Empty".text.color(darkFontGrey).make(),
);
var data = snapshot.data!.docs;
controlller.calculateTotalPrice(data);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) => ListTile(
leading: Image.network("${data[index]['image']}"),
title:
"${data[index]['title']} (x${data[index]['quantity']})"
.text
.fontFamily(semibold)
.size(16)
.make(),
subtitle: "${data[index]['total_price']}"
.numCurrency
.text
.fontFamily(semibold)
.size(16)
.color(redColor)
.make(),
trailing: IconButton(
onPressed: () =>
FirestoreService.deleteCartDocument(
data[index].id),
icon: const Icon(Icons.delete, color: redColor),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
"Total Price"
.text
.fontFamily(semibold)
.color(darkFontGrey)
.make(),
Obx(
() => "${controlller.totalPrice.value}"
.numCurrency
.text
.fontFamily(semibold)
.color(redColor)
.make(),
)
],
)
.box
.padding(const EdgeInsets.all(12))
.color(lightGolden)
.width(context.screenWidth - 60)
.roundedSM
.make(),
10.heightBox,
SizedBox(
width: context.screenWidth - 60,
child: customButton(
color: redColor,
onPressed: () {},
textColor: whiteColor,
title: "Proceed to Shipping",
),
),
],
),
);
}));
}
}

0 comments on commit 72cccd1

Please sign in to comment.