|
| 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'; |
1 | 6 | 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'; |
2 | 10 |
|
3 | 11 | class CartScreen extends StatelessWidget {
|
4 | 12 | const CartScreen({super.key});
|
5 | 13 |
|
6 | 14 | @override
|
7 | 15 | 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 | + })); |
16 | 109 | }
|
17 | 110 | }
|
0 commit comments