Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
52 kB
5
Indexable
Never
import 'dart:developer';

import 'package:animation_wrappers/animation_wrappers.dart';
import 'package:flutter/material.dart';
import 'package:old_barrel/Assets/assets.dart';
import 'package:old_barrel/Components/custom_button2.dart';
import 'package:old_barrel/Components/entry_field2.dart';
import 'package:old_barrel/Locale/locale.dart';
import 'package:old_barrel/Routes/page_routes.dart';
import 'package:old_barrel/constant/common_string.dart';
import 'package:old_barrel/custom_widgets/common_indicator.dart';
import 'package:old_barrel/providers/cart_controller.dart';
import 'package:provider/provider.dart';

import '../BottomNavigation/Home/home_page.dart';
import '../BottomNavigation/Home/item_info.dart';
import '../Components/custom_button.dart';
import '../Components/entry_field.dart';
import '../constant/custom_dialog/confirm_dialog.dart';
import '../models/select_address_model.dart';
import '../providers/add_address_controller.dart';

class CartPage extends StatefulWidget {
  @override
  _CartPageState createState() => _CartPageState();
}

class CartItem {
  String image;
  String name;
  String volume;
  double price;
  int quantity;

  CartItem(this.image, this.name, this.volume, this.price, this.quantity);
}

List<CartItem> cartItems = [
  CartItem(Assets.beer1Shadow, 'Classic Beer Dark\nStrong', '350ml', 8.50, 3),
  CartItem(Assets.beer2, 'Whyte Premium\nWhiskey', '500ml', 10.50, 1),
];

class _CartPageState extends State<CartPage> {
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      Provider.of<CartController>(context, listen: false).cartApi();
      Provider.of<CartController>(context, listen: false).addressListApi();
    });
    super.initState();
  }

  var id;
  var selectedAddress = '';
  var name = '';
  var add1;
  var add2;
  var state;
  var pinCode;
  var mobile;
  bool sel = false;
  List<SelectAddressModel> address = [];
  final GlobalKey<FormState> addressKey = GlobalKey<FormState>();
  var totalPrice;
  @override
  Widget build(BuildContext context) {
    var locale = AppLocalizations.of(context)!;
    return Consumer<CartController>(builder: (context, cartController, child) {
      return WillPopScope(
        onWillPop: () async {
          Navigator.pop(context);
          return false;
        },
        child: Scaffold(
          backgroundColor: Theme.of(context).secondaryHeaderColor,
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(200),
            child: Column(
              children: [
                AppBar(
                  title: Text(locale.myBag!),
                  centerTitle: true,
                  leading: IconButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    icon: Icon(Icons.arrow_back_ios),
                  ),
                ),
                InkWell(
                  onTap: () async {
                    showModalBottomSheet<void>(
                        context: context,
                        builder: (BuildContext context) {
                          return StatefulBuilder(
                            builder: (BuildContext context, setState) {
                              return SingleChildScrollView(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      child: Padding(
                                        padding: const EdgeInsets.only(
                                            left: 20, right: 20),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            SectionText(text: "SELECT ADDRESS"),
                                            Material(
                                              color: Theme.of(context)
                                                  .secondaryHeaderColor,
                                              child: InkWell(
                                                  onTap: () {
                                                    showDialog(
                                                        context: context,
                                                        builder: (context) {
                                                          return Scaffold(
                                                            body: Form(
                                                              key: addressKey,
                                                              child:
                                                                  SingleChildScrollView(
                                                                child: Column(
                                                                  crossAxisAlignment:
                                                                      CrossAxisAlignment
                                                                          .stretch,
                                                                  children: [
                                                                    EntryField(
                                                                      label: AppString
                                                                          .firstName,
                                                                      hint: AppString
                                                                          .enterFirstName,
                                                                      controller:
                                                                          cartController
                                                                              .firstNameController,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourFirstName
                                                                              .toString();
                                                                        }
                                                                        return null;
                                                                      },
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .lastName,
                                                                      hint: AppString
                                                                          .enterLastName,
                                                                      controller:
                                                                          cartController
                                                                              .lastNameController,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourLastName
                                                                              .toString();
                                                                        }
                                                                        return null;
                                                                      },
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .emailAddress,
                                                                      hint: AppString
                                                                          .enterEmailAddress,
                                                                      controller:
                                                                          cartController
                                                                              .emailController,
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .mobileNumber,
                                                                      hint: AppString
                                                                          .enterMobileNumber,
                                                                      controller:
                                                                          cartController
                                                                              .mobileNumberController,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourMobilenumber
                                                                              .toString();
                                                                        } else if (value.length !=
                                                                            10) {
                                                                          return AppString
                                                                              .enter10Char;
                                                                        }
                                                                        return null;
                                                                      },
                                                                      textInputType:
                                                                          TextInputType
                                                                              .number,
                                                                      maxLength:
                                                                          10,
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .add1,
                                                                      hint: AppString
                                                                          .enterAdd1,
                                                                      controller:
                                                                          cartController
                                                                              .add1Controller,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourAdd1
                                                                              .toString();
                                                                        }
                                                                        return null;
                                                                      },
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .add2,
                                                                      hint: AppString
                                                                          .enterAdd2,
                                                                      controller:
                                                                          cartController
                                                                              .add2Controller,
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .pinCode,
                                                                      hint: AppString
                                                                          .enterPinCode,
                                                                      controller:
                                                                          cartController
                                                                              .pinCodeController,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourPinCode
                                                                              .toString();
                                                                        } else if (value.length !=
                                                                            6) {
                                                                          return AppString
                                                                              .enterYour6PinCode;
                                                                        }
                                                                        return null;
                                                                      },
                                                                      textInputType:
                                                                          TextInputType
                                                                              .number,
                                                                      maxLength:
                                                                          6,
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .city,
                                                                      hint: AppString
                                                                          .enterCity,
                                                                      controller:
                                                                          cartController
                                                                              .cityController,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourCity
                                                                              .toString();
                                                                        }
                                                                        return null;
                                                                      },
                                                                    ),
                                                                    EntryField(
                                                                      label: AppString
                                                                          .state,
                                                                      hint: AppString
                                                                          .enterState,
                                                                      controller:
                                                                          cartController
                                                                              .stateController,
                                                                      validator:
                                                                          (value) {
                                                                        if (value!
                                                                            .isEmpty) {
                                                                          return AppString
                                                                              .enterYourState
                                                                              .toString();
                                                                        }
                                                                        return null;
                                                                      },
                                                                      readOnly:
                                                                          true,
                                                                    ),
                                                                    SizedBox(
                                                                      height:
                                                                          20,
                                                                    ),
                                                                    Padding(
                                                                      padding: const EdgeInsets
                                                                              .fromLTRB(
                                                                          20,
                                                                          0,
                                                                          20,
                                                                          20),
                                                                      child:
                                                                          CustomButton(
                                                                        onTap:
                                                                            () {
                                                                          if (addressKey
                                                                              .currentState!
                                                                              .validate()) {
                                                                            showIndicator(context);
                                                                            cartController.addAddressApi(context);
                                                                            // addressController.registerApi(context);
                                                                          }
                                                                        },
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                              ),
                                                            ),
                                                          );
                                                        });
                                                  },
                                                  child: SectionText(
                                                      text: "Add ADDRESS")),
                                            ),
                                          ],
                                        ),
                                      ),
                                      color: Theme.of(context)
                                          .secondaryHeaderColor,
                                      width: MediaQuery.of(context).size.width,
                                    ),
                                    Container(
                                        decoration: BoxDecoration(
                                            color: Theme.of(context)
                                                .secondaryHeaderColor),
                                        child: SizedBox(
                                          height: 400,
                                          width:
                                              MediaQuery.of(context).size.width,
                                          child: cartController
                                                      .addressListModel ==
                                                  null
                                              ? Container()
                                              : ListView.builder(
                                                  scrollDirection:
                                                      Axis.vertical,
                                                  physics: ScrollPhysics(),
                                                  shrinkWrap: true,
                                                  primary: true,
                                                  itemCount: cartController
                                                      .addressListModel!.length,
                                                  itemBuilder:
                                                      (context, index) {
                                                    var data = cartController
                                                            .addressListModel![
                                                        index];
                                                    return InkWell(
                                                      onTap: () {
                                                        this.setState(() {
                                                          name =
                                                              selectedAddress;
                                                        });
                                                        setState(() {
                                                          name =
                                                              selectedAddress;
                                                        });
                                                      },
                                                      child: RadioListTile<
                                                              dynamic>(
                                                          fillColor:
                                                              MaterialStateColor
                                                                  .resolveWith(
                                                            (Set<MaterialState>
                                                                states) {
                                                              if (states.contains(
                                                                  MaterialState
                                                                      .selected)) {
                                                                return Theme.of(
                                                                        context)
                                                                    .primaryColor;
                                                              }
                                                              return Colors
                                                                  .white;
                                                            },
                                                          ),
                                                          title: Text(
                                                            "${cartController.addressListModel![index].fullname}\n${cartController.addressListModel![index].add1}${cartController.addressListModel![index].add2}, ${cartController.addressListModel![index].state} - ${cartController.addressListModel![index].pincode}\n${cartController.addressListModel![index].mobileno}",
                                                            style: TextStyle(
                                                                color: Colors
                                                                    .white),
                                                          ),
                                                          value:
                                                              "${data.id}#${data.fullname}#${data.add1}#${data.add2}#${data.mobileno}#${data.pincode}#${data.state}",
                                                          groupValue:
                                                              cartController
                                                                  .value,
                                                          onChanged: (val) {
                                                            cartController
                                                                .value = val;
                                                            id = val
                                                                .toString()
                                                                .split('#')[0];
                                                            name = val
                                                                .toString()
                                                                .split('#')[1];
                                                            add1 = val
                                                                .toString()
                                                                .split('#')[2];
                                                            add2 = val
                                                                .toString()
                                                                .split('#')[3];
                                                            mobile = val
                                                                .toString()
                                                                .split('#')[4];
                                                            pinCode = val
                                                                .toString()
                                                                .split('#')[5];
                                                            state = val
                                                                .toString()
                                                                .split('#')[6];
                                                            selectedAddress =
                                                                '${name}\n${add1}, ${add2}, ${state} - ${pinCode}\n${mobile}';
                                                            log(selectedAddress
                                                                .toString());
                                                            cartController
                                                                .reload();
                                                            sel = true;
                                                            Navigator.of(
                                                                    context)
                                                                .pop();
                                                            setState(() {});
                                                          }),
                                                    );
                                                  }),
                                        )),
                                  ],
                                ),
                              );
                            },
                          );
                        });
                  },
                  child: Container(
                    width: MediaQuery.of(context).size.width / 1,
                    height: 140,
                    margin: EdgeInsets.symmetric(
                      horizontal: 14,
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4),
                        color: Theme.of(context).scaffoldBackgroundColor),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Image.asset(
                          Assets.locationIcon,
                          scale: 3,
                        ),
                        SizedBox(
                          width: 12,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Text(
                                  locale.deliveryTo!.toUpperCase(),
                                  style: Theme.of(context)
                                      .textTheme
                                      .bodyMedium!
                                      .copyWith(
                                          color: Theme.of(context).primaryColor,
                                          fontSize: 12),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 4,
                            ),
                            SizedBox(
                              width: MediaQuery.of(context).size.width / 1.4,
                              child: Wrap(
                                children: [
                                  Text(
                                    "${sel == true ? selectedAddress : cartController.defaultd}",
                                    style: Theme.of(context)
                                        .textTheme
                                        .bodyMedium!
                                        .copyWith(
                                            fontSize: 12, color: Colors.white),
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                        Icon(
                          Icons.keyboard_arrow_down,
                          color: Theme.of(context).primaryColor,
                          size: 25,
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          body: FadedSlideAnimation(
            child: Stack(
              alignment: Alignment.bottomCenter,
              children: [
                Container(
                  height: MediaQuery.of(context).size.height,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.vertical(
                        top: Radius.circular(20),
                      ),
                      color: Theme.of(context).scaffoldBackgroundColor),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 20.0, vertical: 20.0),
                        child: Column(
                          children: [
                            Text(
                              '${cartController.cartData.length}  ' +
                                  locale.itemsInBag!,
                              style: Theme.of(context).textTheme.bodySmall,
                            ),
                          ],
                        ),
                      ),
                      Container(
                        height: MediaQuery.of(context).size.height / 2,
                        child: ListView.builder(
                            scrollDirection: Axis.vertical,
                            itemCount: cartController.cartData.length,
                            shrinkWrap: true,
                            primary: true,
                            itemBuilder: (context, index) {
                              var data = cartController.cartData[index];
                              var p = int.tryParse(data.price.toString());
                              int q = int.tryParse(data.qty.toString()) ?? 1;
                              totalPrice = (p! * q);
                              Map imgMap = {};
                              if (data.img is Map) imgMap = data.img as Map;
                              print(data.img);
                              return Padding(
                                padding: const EdgeInsetsDirectional.only(
                                    start: 20, end: 16, bottom: 16),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  children: [
                                    Expanded(
                                      flex: 2,
                                      child: Container(
                                        margin: EdgeInsets.only(right: 10),
                                        child: imgMap.isNotEmpty
                                            ? Image.network(
                                                'https://theliquidexchange.in/admin/product_images/' +
                                                    '${data.img['small_img']}',
                                                scale: 15,
                                              )
                                            : Image.asset(
                                                Assets.placeHolder,
                                                scale: 5.5,
                                              ),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 4,
                                      child: Container(
                                        width:
                                            MediaQuery.of(context).size.width /
                                                2,
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              data.productname.toString(),
                                              style: Theme.of(context)
                                                  .textTheme
                                                  .bodyMedium!
                                                  .copyWith(
                                                      overflow:
                                                          TextOverflow.fade),
                                            ),
                                            Padding(
                                              padding:
                                                  const EdgeInsets.symmetric(
                                                      vertical: 5),
                                              child: Text(
                                                data.variationsString
                                                        .toString() +
                                                    ' ml',
                                                style: Theme.of(context)
                                                    .textTheme
                                                    .bodySmall,
                                              ),
                                            ),
                                            Text(
                                              '₹' + '${data.price ?? ''}',
                                              style: Theme.of(context)
                                                  .textTheme
                                                  .bodyMedium!
                                                  .copyWith(
                                                      color: Theme.of(context)
                                                          .primaryColor),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 2,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceEvenly,
                                            children: [
                                              InkWell(
                                                onTap: () {
                                                  if (q > 1) {
                                                    if (q > 1)
                                                      setState(() {
                                                        q--;
                                                      });
                                                    showIndicator(context);
                                                    cartController.updateCart(
                                                      context,
                                                      caryId: data.cartId,
                                                      qty: q.toString(),
                                                    );
                                                    cartController.cartApi();
                                                  } else {
                                                    showIndicator(context);
                                                    confirmDialog(context,
                                                        text: AppString
                                                            .deleteItem,
                                                        onPressed: () {
                                                      cartController.deleteCartItem(
                                                          data.cartId,
                                                          data.shoppingSessionId,
                                                          context);
                                                    });
                                                  }
                                                },
                                                child: Icon(
                                                  Icons.remove,
                                                  color: Colors.white,
                                                  size: 24,
                                                ),
                                              ),
                                              Padding(
                                                padding:
                                                    const EdgeInsets.symmetric(
                                                        horizontal: 8.0),
                                                child: Text(
                                                  q.toString() ?? '',
                                                  style: Theme.of(context)
                                                      .textTheme
                                                      .bodyMedium!
                                                      .copyWith(
                                                          color:
                                                              Theme.of(context)
                                                                  .primaryColor,
                                                          fontSize: 20),
                                                ),
                                              ),
                                              InkWell(
                                                onTap: () {
                                                  setState(() {
                                                    q++;
                                                  });
                                                  showIndicator(context);
                                                  cartController.updateCart(
                                                    context,
                                                    caryId: data.cartId,
                                                    qty: q.toString(),
                                                  );
                                                  cartController.cartApi();
                                                  log(q.toString());
                                                },
                                                child: Icon(
                                                  Icons.add,
                                                  color: Colors.white,
                                                  size: 24,
                                                ),
                                              ),
                                            ],
                                          ),
                                          /* Padding(
                                            padding: const EdgeInsets.symmetric(
                                                vertical: 10),
                                            child: InkWell(
                                              onTap: () {
                                                showIndicator(context);
                                                confirmDialog(context,
                                                    text: AppString.deleteItem,
                                                    onPressed: () {
                                                  cartController.deleteCartItem(
                                                      data.cartId,
                                                      data.shoppingSessionId,
                                                      context);
                                                });
                                              },
                                              child: Icon(
                                                Icons.delete,
                                                color: Colors.red,
                                                size: 18,
                                              ),
                                            ),
                                          ),*/
                                          Text(
                                            '₹' + '${totalPrice}',
                                            style: Theme.of(context)
                                                .textTheme
                                                .bodyLarge,
                                          )
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }),
                      )
                    ],
                  ),
                ),
                PositionedDirectional(
                  bottom: 0,
                  start: 0,
                  end: 0,
                  child: CartPay(
                    sTotal: cartController.calculateTotal().toString(),
                    onTap: () {
                      cartController.checkout(context,
                          address: cartController.value.split('#')[0],
                          shopping_session_id:
                              cartController.cartData[0].shoppingSessionId,
                          gross_amount:
                              cartController.calculateTotal().toString(),
                          net_amt: cartController.calculateTotal().toString(),
                          tax_amount: "0",
                          discount_per: "0",
                          discount_amt: "0");
                    },
                  ),
                ),
              ],
            ),
            beginOffset: Offset(0, 0.3),
            endOffset: Offset(0, 0),
            slideCurve: Curves.linearToEaseOut,
          ),
        ),
      );
    });
  }
}

class CartPay extends StatelessWidget {
  final String? sTotal;
  final String? dCharge;
  final String? toPay;
  final VoidCallback? onTap;
  const CartPay({Key? key, this.sTotal, this.dCharge, this.toPay, this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var locale = AppLocalizations.of(context)!;

    return Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.vertical(
            top: Radius.circular(20),
          ),
          color: Theme.of(context).secondaryHeaderColor),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Total',
                  style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                      color: Theme.of(context).primaryColor, fontSize: 16),
                ),
                Text(
                  '₹${sTotal ?? ''}',
                  style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                      color: Theme.of(context).primaryColor, fontSize: 16),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 12,
          ),
          CustomButton2(
            onTap:
                onTap /*() {
              Navigator.pushNamed(context, PageRoutes.paymentPage);
            }*/
            ,
            centerWidget: Center(
              child: Text(
                'PLACE ORDER',
                style: Theme.of(context)
                    .textTheme
                    .bodyLarge!
                    .copyWith(fontWeight: FontWeight.w700, fontSize: 16),
              ),
            ),
            /*  suffix: Icon(
              Icons.arrow_forward,
              color: Theme.of(context).colorScheme.background,
            ),*/
          )
        ],
      ),
    );
  }
}
Leave a Comment