Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
35 kB
1
Indexable
Never
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:iketfaa_delivery/App/Common/Modules/SignIn/binding/signin_binding.dart';
import 'package:iketfaa_delivery/App/Common/Modules/SignIn/view/signin_view.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Constants/AppColors.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Constants/AppStyles.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Constants/Strings/Backend.dart';
import 'package:iketfaa_delivery/App/Common/Widgets/no_items_empty.dart';
import 'package:iketfaa_delivery/App/Common/Widgets/primary_button.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/PersonalAddress.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryCart/Controller/delivery_cart_conroller.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryUser/DeliveryUserAddressBook/binding/delivery_address_book_binding.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryUser/DeliveryUserAddressBook/view/delivery_add_new_address_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Utilities/Widgets/Drawer/delivery_tile_button_for_drawer.dart';
import 'package:iketfaa_delivery/App/Delivery/Utilities/Widgets/LocationTile.dart';
import 'package:iketfaa_delivery/App/Delivery/Utilities/Widgets/custimized_cupertino_dialog.dart';
import 'package:iketfaa_delivery/App/Delivery/Utilities/Widgets/DeliveryCartListCard.dart';
import 'package:iketfaa_delivery/App/Delivery/Utilities/Widgets/delivery_white_button.dart';
import 'package:iketfaa_delivery/App/Delivery/Utilities/Widgets/primary_app_bar.dart';

class DeliveryCartView extends GetView<DeliveryCartController> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(15),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            AppColors.white,
            AppColors.greenyWhite,
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
      child: Obx(
        () => Scaffold(
            appBar: PrimaryAppBar(
              title: 'shoppingCart'.tr,
              function: () {
                Get.back();
              },
            ),
            backgroundColor: Colors.transparent,
            body: controller.deliveryHomeController.deliveryHubController.cart
                    .value.orders!.isEmpty
                ? EmptyCartView()
                : FilledCartView()),
      ),
    );
  }
}

class EmptyCartView extends GetView<DeliveryCartController> {
  const EmptyCartView({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {},
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(),
          Icon(
            Icons.shopping_cart,
            color: AppColors.primary,
            size: 80,
          ),
          SizedBox(
            height: 30.0,
          ),
          Text(
            'noItemToShow'.tr,
            style: Get.textTheme.headline3,
          ),
        ],
      ),
    );
  }
}

class RdButton extends GetView<DeliveryCartController> {
  const RdButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Obx(
        () => Column(
          children: [
            Row(
              children: [
                Radio<PaymentMethod>(
                    value: PaymentMethod.cash,
                    groupValue: controller.character!.value,
                    activeColor: AppColors.primary,
                    onChanged: (PaymentMethod? value) {
                      controller.character!.value = value!;
                      controller.deliveryHomeController.deliveryHubController
                          .cart.value.paymentType = 0;
                    }),
                SizedBox(height: 7),
                Text('cash'.tr, style: Get.textTheme.headline5),
              ],
            ),
            Row(
              children: [
                Radio<PaymentMethod>(
                    value: PaymentMethod.Credit_card,
                    groupValue: controller.character!.value,
                    activeColor: AppColors.primary,
                    onChanged: (PaymentMethod? value) {
                      controller.character!.value = value!;
                      controller.deliveryHomeController.deliveryHubController
                          .cart.value.paymentType = 1;
                    }),
                SizedBox(height: 7),
                Text('cridet card'.tr, style: Get.textTheme.headline5),
              ],
            ),
            Row(
              children: [
                Radio<PaymentMethod>(
                    value: PaymentMethod.wallet,
                    groupValue: controller.character!.value,
                    activeColor: AppColors.primary,
                    onChanged: (PaymentMethod? value) {
                      controller.character!.value = value!;
                      controller.deliveryHomeController.deliveryHubController
                          .cart.value.paymentType = 2;
                    }),
                SizedBox(height: 7),
                Text('wallet'.tr, style: Get.textTheme.headline5),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class FilledCartView extends GetView<DeliveryCartController> {
  const FilledCartView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        child: Column(
          children: [
            Obx(
              () => ListView.builder(
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemCount: controller.deliveryHomeController
                    .deliveryHubController.cart.value.orders!.length,
                itemBuilder: (context, index) {
                  final item = controller.deliveryHomeController
                      .deliveryHubController.cart.value.orders!.length;
                  return Dismissible(
                    background: Container(
                      height: 100,
                      color: Colors.red,
                      child: Center(
                        child: Text(
                          'delete'.tr,
                          style: Get.textTheme.headline5!
                              .copyWith(color: AppColors.white),
                        ),
                      ),
                    ),
                    key: Key(item.toString()),
                    confirmDismiss: (direction) async {
                      await showDialog(
                        context: context,
                        builder: (context) => CustimizedCupertinoDialog(
                          onPressed: () async {
                            controller
                                .deliveryHomeController.deliveryHubController
                                .removeItem(index);
                            controller.deliveryHomeController
                                .deliveryHubController.cart
                                .refresh();
                            controller.deliveryHomeController
                                    .deliveryHubController.cartLength -
                                1;

                            controller.deliveryHomeController
                                .deliveryHubController.cartLength
                                .refresh();

                            Get.back();
                            if (controller
                                    .deliveryHomeController
                                    .deliveryHubController
                                    .cart
                                    .value
                                    .orders!
                                    .length !=
                                0) {
                              controller.deliveryHomeController.distance.value =
                                  await controller.authManager.deliveryTools
                                      .calculateAllDistanceForPrice(controller
                                          .deliveryHomeController
                                          .deliveryHubController
                                          .cart
                                          .value
                                          .orders!);
                              await controller.deliveryHomeController
                                  .deliveryHubController.globalStorage
                                  .write(
                                      'deliveryCartFromLocal',
                                      controller.deliveryHomeController
                                          .deliveryHubController.cart
                                          .toJson());
                            } else {
                              await controller.deliveryHomeController
                                  .deliveryHubController.globalStorage
                                  .remove('deliveryCartFromLocal');
                              controller.deliveryHomeController.distance.value =
                                  await controller.authManager.deliveryTools
                                      .calculateAllDistanceForPrice(controller
                                          .deliveryHomeController
                                          .deliveryHubController
                                          .cart
                                          .value
                                          .orders!);
                            }

                            controller.authManager.commonTools
                                .showSuccessSnackBar(
                                    'order has been deleted successfully'.tr);
                          },
                          title: 'do you want to remove this order?'.tr,
                        ),
                      );
                    },
                    child: DeliveryCartListCard(
                      index: index,
                      orderItem: controller
                          .deliveryHomeController
                          .deliveryHubController
                          .cart
                          .value
                          .orders![index]
                          .orderItems!,
                      storeOrder: controller.deliveryHomeController
                          .deliveryHubController.cart.value.orders![index],
                      onTap: () {},
                    ),
                  );
                },
              ),
            ),
            const SizedBox(height: 30),
            Row(
              children: [
                SvgPicture.asset(
                  'assets/svg/person location.svg',
                  width: 30,
                ),
                Text(
                  'location'.tr,
                  style: Get.textTheme.headline6,
                ),
              ],
            ),
            SizedBox(height: 10),
            Obx(
              () => DeliveryWhiteButton(
                onTap: () {
                  controller.authManager.commonTools.showCustomBottomSheet(
                      context,
                      controller.authManager.isLogged.value
                          ? "chooseDeliveryLocation".tr
                          : 'pleaseSignIn'.tr,
                      controller.authManager.isLogged.value
                          ? StreamBuilder<QuerySnapshot>(
                              stream: FirebaseFirestore.instance
                                  .collection(DELIVERY_USERS_COLLECTION)
                                  .doc(controller.authManager.appUser.value.id)
                                  .collection(PERSONAL_ADDRESS_COLLECTION)
                                  .orderBy(DATE_FIELD, descending: true)
                                  .snapshots(),
                              builder: (context, snapshot) {
                                if (snapshot.data == null) {
                                  return Container(
                                    height: Get.width * 0.6,
                                    width: Get.width,
                                    child: CupertinoActivityIndicator(),
                                  );
                                }
                                if (!snapshot.hasData) {
                                  return Container(
                                    height: Get.width * 0.6,
                                    width: Get.width,
                                    child: CupertinoActivityIndicator(),
                                  );
                                }
                                return !snapshot.data!.docs.isEmpty
                                    ? Container(
                                        height: Get.width * 0.8,
                                        padding: EdgeInsets.symmetric(
                                            horizontal: 25),
                                        child: ListView(
                                          shrinkWrap: true,
                                          children: [
                                            Container(
                                              child: ListView.builder(
                                                shrinkWrap: true,
                                                physics:
                                                    NeverScrollableScrollPhysics(),
                                                itemCount:
                                                    snapshot.data!.docs.length,
                                                itemBuilder: (context, index) {
                                                  DocumentSnapshot doc =
                                                      snapshot
                                                          .data!.docs[index];
                                                  PersonalAddress? address =
                                                      PersonalAddress.fromMap(
                                                          doc.data() as Map<
                                                              String, dynamic>);
                                                  return LocationTile(
                                                    withCricle: true,
                                                    address: address,
                                                    active: controller
                                                            .deliveryHomeController
                                                            .deliveryHubController
                                                            .currentPersonalAddress
                                                            .value
                                                            .adID ==
                                                        address.adID,
                                                    onTap: () async {
                                                      await controller
                                                          .setCurrentAddress(
                                                              address);
                                                      controller
                                                          .deliveryHomeController
                                                          .calculateAllData();
                                                      controller
                                                          .deliveryHomeController
                                                          .deliveryHubController
                                                          .cart
                                                          .refresh();
                                                      Get.back();
                                                    },
                                                  );
                                                },
                                              ),
                                            ),
                                            ListTile(
                                              onTap: () async {
                                                controller
                                                    .deliveryHomeController
                                                    .cartAddressFlag
                                                    .value = true;
                                                Get.to(
                                                  () =>
                                                      DeliveryAddNewAddressView(),
                                                  binding:
                                                      DeliveryAddressBookBinding(),
                                                );
                                                controller
                                                    .deliveryHomeController
                                                    .deliveryHubController
                                                    .cart
                                                    .refresh();
                                              },
                                              title: Text('addNewAddress'.tr,
                                                  style:
                                                      Get.textTheme.headline4),
                                              leading: SvgPicture.asset(
                                                'assets/svg/gen011-021.svg',
                                                width: 25,
                                              ),
                                            ),
                                            Divider(),
                                            ListTile(
                                              onTap: () async {
                                                controller
                                                    .deliveryHomeController
                                                    .deliveryHubController
                                                    .currentPersonalAddress
                                                    .value = PersonalAddress();
                                                await controller
                                                    .setCurrentAddress(controller
                                                        .deliveryHomeController
                                                        .deliveryHubController
                                                        .currentPersonalAddress
                                                        .value);
                                                controller
                                                    .deliveryHomeController
                                                    .calculateAllData();
                                                controller
                                                    .deliveryHomeController
                                                    .deliveryHubController
                                                    .cart
                                                    .refresh();
                                                Get.back();
                                              },
                                              title: Text('currentLocation'.tr,
                                                  style:
                                                      Get.textTheme.headline4),
                                              leading: SvgPicture.asset(
                                                'assets/svg/gen011-021.svg',
                                                width: 25,
                                              ),
                                            )
                                          ],
                                        ),
                                      )
                                    : Container(
                                        padding: EdgeInsets.all(10),
                                        child: Column(
                                          children: [
                                            ListTile(
                                              onTap: () {
                                                controller
                                                    .deliveryHomeController
                                                    .cartAddressFlag
                                                    .value = true;
                                                Get.to(
                                                  () =>
                                                      DeliveryAddNewAddressView(),
                                                  binding:
                                                      DeliveryAddressBookBinding(),
                                                );
                                              },
                                              title: Text('addNewAddress'.tr,
                                                  style:
                                                      Get.textTheme.headline4),
                                              leading: SvgPicture.asset(
                                                'assets/svg/gen011-021.svg',
                                                width: 25,
                                              ),
                                            ),
                                            Divider(),
                                            ListTile(
                                              onTap: () {
                                                controller
                                                    .deliveryHomeController
                                                    .deliveryHubController
                                                    .currentPersonalAddress
                                                    .value = PersonalAddress();
                                                print(controller
                                                    .deliveryHomeController
                                                    .deliveryHubController
                                                    .currentPersonalAddress
                                                    .value
                                                    .lat);
                                                Get.back();
                                              },
                                              title: Text('currentLocation'.tr,
                                                  style:
                                                      Get.textTheme.headline4),
                                              leading: SvgPicture.asset(
                                                'assets/svg/gen011-021.svg',
                                                width: 25,
                                              ),
                                            )
                                          ],
                                        ),
                                      );
                              },
                            )
                          : Column(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Container(
                                  height: 10,
                                  color: AppColors.white,
                                ),
                                Container(
                                  color: AppColors.white,
                                  child: Column(
                                    children: [
                                      DelvieryTileButtonForDrawer(
                                        title: 'login'.tr,
                                        onTap: () {
                                          Get.offAll(() => SignInView(),
                                              binding: SignInBinding());
                                        },
                                        icon: Icon(Icons.login,
                                            color: AppColors.primary),
                                      ),
                                      Container(
                                        height: 30,
                                        color: AppColors.white,
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ));
                },
                title: controller.deliveryHomeController.deliveryHubController
                            .currentPersonalAddress.value.adID ==
                        null
                    ? 'currentLocation'.tr
                    : controller.deliveryHomeController.deliveryHubController
                        .currentPersonalAddress.value.title!,
              ),
            ),
            const SizedBox(height: 25),
            Column(
              children: [
                Row(
                  children: [
                    SvgPicture.asset(
                      'assets/svg/Icon payment.svg',
                      width: 18,
                    ),
                    const SizedBox(width: 5),
                    Text(
                      'Choose your preferred payment method'.tr,
                      style: Get.textTheme.headline6,
                    ),
                  ],
                ),
                const SizedBox(height: 10),
                RdButton(),
                const SizedBox(height: 10),
                Divider(
                  endIndent: 2,
                  indent: 2,
                  thickness: 1.2,
                ),
                controller.deliveryHomeController.deliveryHubController.cart
                            .value.orderType ==
                        0
                    ? Obx(
                        () => controller
                                    .deliveryHomeController
                                    .deliveryHubController
                                    .currentPersonalAddress
                                    .value
                                    .adID !=
                                null
                            ? SizedBox()
                            : Container(
                                margin: EdgeInsets.only(
                                    bottom: 15.0,
                                    top: 15.0,
                                    right: 5.0,
                                    left: 5.0),
                                padding: EdgeInsets.all(10.0),
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(10.0),
                                    color: AppColors.white,
                                    boxShadow: [AppStyles.primaryShadow]),
                                child: Row(
                                  children: [
                                    Icon(
                                      Icons.report_outlined,
                                      color: AppColors.notification,
                                    ),
                                    SizedBox(
                                      width: 10.0,
                                    ),
                                    Expanded(
                                      child: Text(
                                        "pleaseSelectOrderAddressInCart".tr,
                                        style: Get.textTheme.headline4!
                                            .copyWith(height: 1.5),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                      )
                    : SizedBox(),
                controller.deliveryHomeController.deliveryHubController.cart
                            .value.orders![0].storeName! !=
                        'directOrder'
                    ? Column(
                        children: [
                          const SizedBox(height: 10),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                'Estimate Delivery Fee'.tr,
                                style: Get.textTheme.headline5,
                              ),
                              Obx(
                                () => Text(
                                  controller.deliveryHomeController.price
                                              .value ==
                                          0.0
                                      ? '-'
                                      : controller.deliveryHomeController.price
                                              .value
                                              .toString() +
                                          ' - ' +
                                          (controller.deliveryHomeController
                                                      .price.value +
                                                  5.0)
                                              .toString() +
                                          ' ' +
                                          'SAR'.tr,
                                  style: Get.textTheme.headline5,
                                ),
                              ),
                            ],
                          ),
                          const SizedBox(height: 10),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                'Total Distance'.tr,
                                style: Get.textTheme.headline5,
                              ),
                              Obx(
                                () => Text(
                                  controller.deliveryHomeController.distance
                                              .value ==
                                          0.0
                                      ? '-'
                                      : controller.authManager.deliveryTools
                                          .getDistanceString(controller
                                              .deliveryHomeController
                                              .distance
                                              .value),
                                  style: Get.textTheme.headline5,
                                ),
                              ),
                            ],
                          ),
                        ],
                      )
                    : Container(),
                const SizedBox(height: 30),
                Column(
                  children: [
                    PrimaryButton(
                      text: 'Send',
                      function: () async {
                        if (!controller.authManager.isLogged.value) {
                          controller.authManager.commonTools
                              .showFailedSnackBar('pleaseLogin');
                          return;
                        }
                        // if (controller
                        //         .deliveryHomeController
                        //         .deliveryHubController
                        //         .currentPersonalAddress
                        //         .value
                        //         .lat ==
                        //     null) {
                        //   controller.authManager.commonTools.showFailedSnackBar(
                        //       'pleaseSelectOrderAddressInCart');
                        //   return;
                        // }
                        controller.authManager.commonTools.showLoading();
                        await controller.sendOrder();
                      },
                    ),
                    const SizedBox(height: 15),
                    PrimaryButton(
                      text: 'clearthebasket'.tr,
                      function: () {
                        showDialog(
                          context: context,
                          builder: (context) => CustimizedCupertinoDialog(
                            onPressed: () async {
                              controller
                                  .deliveryHomeController.deliveryHubController
                                  .clearCart();
                              Get.back();
                              Get.back();
                              if (controller
                                      .deliveryHomeController
                                      .deliveryHubController
                                      .cart
                                      .value
                                      .orderType ==
                                  0) {
                                controller
                                        .deliveryHomeController.distance.value =
                                    await controller.authManager.deliveryTools
                                        .calculateAllDistanceForPrice(controller
                                            .deliveryHomeController
                                            .deliveryHubController
                                            .cart
                                            .value
                                            .orders!);
                              }
                              await controller.deliveryHomeController
                                  .deliveryHubController.globalStorage
                                  .remove('deliveryCartFromLocal');
                            },
                            title: 'do you want to clear this cart?'.tr,
                          ),
                        );
                      },
                      buttonColor: AppColors.white,
                      fontColor: AppColors.black,
                    )
                  ],
                ),
                const SizedBox(height: 30),
              ],
            ),
          ],
        ),
      ),
    );
  }
}