-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add delete from cart and updating database
- Loading branch information
1 parent
30651bb
commit 72cccd1
Showing
3 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
), | ||
], | ||
), | ||
); | ||
})); | ||
} | ||
} |