import 'package:after_layout/after_layout.dart'; import 'package:diplomaticquarterapp/config/shared_pref_kay.dart'; import 'package:diplomaticquarterapp/core/model/ResponseModel.dart'; import 'package:diplomaticquarterapp/core/model/hospitals/hospitals_model.dart'; import 'package:diplomaticquarterapp/core/model/packages_offers/requests/AddProductToCartRequestModel.dart'; import 'package:diplomaticquarterapp/core/viewModels/packages_offers/PackagesOffersViewModel.dart'; import 'package:diplomaticquarterapp/pages/ToDoList/payment_method_select.dart'; import 'package:diplomaticquarterapp/pages/base/base_view.dart'; import 'package:diplomaticquarterapp/pages/packages_offers/PackageOrderCompletedPage.dart'; import 'package:diplomaticquarterapp/pages/pharmacies/screens/pharmacy-terms-conditions-page.dart'; import 'package:diplomaticquarterapp/theme/colors.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/uitl/utils.dart' as utils; import 'package:diplomaticquarterapp/uitl/utils_new.dart'; import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart'; import 'package:diplomaticquarterapp/widgets/data_display/text.dart'; import 'package:diplomaticquarterapp/widgets/dialogs/radio_selection_dialog.dart'; import 'package:diplomaticquarterapp/widgets/in_app_browser/InAppBrowser.dart'; import 'package:diplomaticquarterapp/widgets/offers_packages/PackagesCartItemCard.dart'; import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart'; import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; dynamic languageID; const _columnCount = 1; bool _agreeTerms = false; String _selectedPaymentMethod; Map _selectedPaymentParams; AnimationController _animationController; class PackagesCartPage extends StatefulWidget { PackagesCartPage(); @override _PackagesCartPageState createState() => _PackagesCartPageState(); } class _PackagesCartPageState extends State with AfterLayoutMixin, SingleTickerProviderStateMixin { getLanguageID() async { languageID = await sharedPref.getString(APP_LANGUAGE); } double subtotal, tax, total; int _selectedHospitalIndex = -1; HospitalsModel _selectedHospital; @override void initState() { _agreeTerms = false; _selectedPaymentMethod = null; _animationController = AnimationController(vsync: this, duration: Duration(seconds: 500)); super.initState(); } @override void dispose() { _animationController.dispose(); viewModel.cartItemList.clear(); super.dispose(); } PackagesViewModel viewModel; bool loadWidgets = false; onTermsClick(bool isAgree) { setState(() => _agreeTerms = isAgree); } onTermsInfoClick() { Navigator.push(context, FadePage(page: PharmacyTermsConditions())); } onPayNowClick() async { await viewModel.service.placeOrder(context: context, projectID: _selectedHospital.iD, paymentParams: _selectedPaymentParams).then((orderId) { if (orderId.runtimeType == int) { var browser = MyInAppBrowser(context: context, onExitCallback: (data, isDone) => paymentClosed(orderId: orderId, withStatus: isDone, data: data)); browser.openPackagesPaymentBrowser(customer_id: viewModel.service.customer.id, order_id: orderId); } else { utils.Utils.showErrorToast('Failed to place order, please try again later'); } }).catchError((error) { utils.Utils.showErrorToast(error.toString()); }); } @override void afterFirstLayout(BuildContext context) { fetchData(); viewModel.service.getHospitals(); } @override Widget build(BuildContext context) { return BaseView( allowAny: true, onModelReady: (model) => viewModel = model, builder: (_, model, wi) { return AppScaffold( appBarTitle: TranslationBase.of(context).offerAndPackages, isShowAppBar: true, isPharmacy: false, showPharmacyCart: false, showHomeAppBarIcon: false, isOfferPackages: true, showOfferPackagesCart: false, isShowDecPage: false, showNewAppBar: true, showNewAppBarTitle: true, body: viewModel.cartItemList.length > 0 ? Column( children: [ Expanded( child: StaggeredGridView.countBuilder( crossAxisCount: (_columnCount * _columnCount), itemCount: viewModel.cartItemList.length, itemBuilder: (BuildContext context, int index) { var item = viewModel.cartItemList[index]; return Dismissible( key: Key(index.toString()), direction: DismissDirection.startToEnd, background: _cartItemDeleteContainer(), secondaryBackground: _cartItemDeleteContainer(), confirmDismiss: (direction) async { bool status = await viewModel.service.deleteProductFromCart(item.id, context: context, showLoading: false); return status; }, onDismissed: (direction) { viewModel.cartItemList.removeAt(index); }, child: PackagesCartItemCard( itemModel: item, viewModel: viewModel, getCartItems: fetchData, shouldStepperChangeApply: (apply, total) async { var request = AddProductToCartRequestModel(product_id: item.productId, quantity: apply); ResponseModel response = await viewModel.service.addProductToCart(request, context: context, showLoading: false).catchError((error) { utils.Utils.showErrorToast(error); }); if (response.status) { fetchData(); } return response.status ?? false; }, )); }, staggeredTileBuilder: (int index) => StaggeredTile.fit(_columnCount), mainAxisSpacing: 0, crossAxisSpacing: 10, ), ), Container( height: 0.25, color: Theme.of(context).primaryColor, ), ], ) : getNoDataWidget(context), bottomSheet: viewModel.cartItemList.length > 0 ? Container( padding: EdgeInsets.all(21.0), width: double.infinity, color: Colors.white, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Container( margin: EdgeInsets.only(bottom: 12.0), child: InkWell( onTap: () => confirmSelectHospitalDialog(model.hospitals), child: Container( padding: EdgeInsets.all(12), width: double.infinity, height: 50, decoration: BoxDecoration(borderRadius: BorderRadius.circular(12), border: Border.all(color: CustomColors.devider), color: Colors.white), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( getHospitalName(), style: TextStyle( fontSize: 14, fontWeight: FontWeight.w600, letterSpacing: -0.46, ), ), Icon(Icons.arrow_drop_down), ], ), ), ), ), Container( margin: EdgeInsets.fromLTRB(0.0, 0.0, 20.0, 0.0), child: Text(TranslationBase.of(context).YouCanPayByTheFollowingOptions, style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w600)), ), Container( width: MediaQuery.of(context).size.width * 0.75, margin: EdgeInsets.fromLTRB(0.0, 8.0, 20.0, 5.0), child: getPaymentMethods(), ), Container( margin: EdgeInsets.only(top: 14.0), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ SizedBox( height: 24.0, width: 24.0, child: Checkbox( value: _agreeTerms, onChanged: (v) { setState(() => _agreeTerms = v); }), ), Expanded( child: Text( TranslationBase.of(context).iAcceptTermsConditions, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: CustomColors.textColor, letterSpacing: -0.48), ), ), ], ), ), Container( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: double.infinity, padding: EdgeInsets.only(top: 12, bottom: 3), child: Row( children: [ Expanded( child: _getNormalText(TranslationBase.of(context).patientShareToDo), ), Expanded( child: _getNormalText(TranslationBase.of(context).sar + " " + (subtotal ?? 0.0).toStringAsFixed(2), isBold: true), ) ], ), ), mDivider(Colors.grey[200]), Container( width: double.infinity, padding: EdgeInsets.only(top: 3, bottom: 3), child: Row( children: [ Expanded( child: _getNormalText(TranslationBase.of(context).patientTaxToDo), ), Expanded( child: _getNormalText(TranslationBase.of(context).sar + ' ' + (tax ?? 0.0).toStringAsFixed(2), isBold: true), ) ], ), ), mDivider(Colors.grey[200]), Container( width: double.infinity, padding: EdgeInsets.only(top: 3, bottom: 3), child: Row( children: [ Expanded( child: _getNormalText(TranslationBase.of(context).patientShareTotalToDo), ), Expanded( child: _getNormalText(TranslationBase.of(context).sar + ' ' + (total ?? 0.0).toStringAsFixed(2), isBold: true, isTotal: true), ) ], ), ), ], ), ), Container( padding: EdgeInsets.only(top: 21, bottom: 21), child: DefaultButton( TranslationBase.of(context).payNow, (_agreeTerms && _selectedHospital != null) ? () { Navigator.push(context, FadePage(page: PaymentMethod(onSelectedMethod: (String metohd) { setState(() {}); }))).then((value) { print(value); if (value != null) { _selectedPaymentMethod = value; _selectedPaymentParams = {"payment_method_system_name": "Payments.PayFort", "payment_option": value}; onPayNowClick(); } }); } : null, color: CustomColors.green, disabledColor: CustomColors.grey2, ), ), ], ), ) : SizedBox(), ); }, ); } void confirmSelectHospitalDialog(List hospitals) { List list = [ for (int i = 0; i < hospitals.length; i++) RadioSelectionDialogModel(hospitals[i].name + ' ${hospitals[i].distanceInKilometers} ' + TranslationBase.of(context).km, i), ]; showDialog( context: context, child: RadioSelectionDialog( listData: list, selectedIndex: _selectedHospitalIndex, isScrollable: true, onValueSelected: (index) { _selectedHospitalIndex = index; _selectedHospital = hospitals[index]; setState(() {}); }, ), ); } String getHospitalName() { if (_selectedHospital != null) return _selectedHospital.name; else return TranslationBase.of(context).selectHospital; } fetchData() async { await viewModel.service.cartItems(context: context).then((value) { subtotal = value['subtotal'] ?? 0.0; tax = value['tax'] ?? 0.0; total = value['total'] ?? 0.0; }).catchError((error) {}); setState(() {}); } paymentClosed({@required int orderId, @required bool withStatus, dynamic data}) async { viewModel.service.getOrderById(orderId, context: context).then((value) { var heading = withStatus ? "Success" : "Failed"; var title = withStatus ? "Your order has been placed successfully" : "Failed to place your order"; var subTitle = "Order# ${value.data.customOrderNumber}"; Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => PackageOrderCompletedPage(heading: heading, title: title, subTitle: subTitle))); }).catchError((error) { debugPrint(error); }); } } // Widget _payNow(BuildContext context, {double subtotal, double tax, double total, @required VoidCallback onPayNowClick}) { // bool isPayNowAQctive = (_agreeTerms && (_selectedPaymentMethod != null)); // // String _subtotal = (subtotal ?? 0.0).toStringAsFixed(2); // String _tax = (tax ?? 0.0).toStringAsFixed(2); // String _total = (total ?? 0).toStringAsFixed(2); // // return Padding( // padding: const EdgeInsets.all(5), // child: Container( // child: Row( // crossAxisAlignment: CrossAxisAlignment.end, // children: [ // Padding( // padding: const EdgeInsets.all(5), // child: Column( // crossAxisAlignment: CrossAxisAlignment.start, // children: [ // Texts('${TranslationBase.of(context).subtotal}: $_subtotal ${TranslationBase.of(context).sar}', heightFactor: 1.5, fontWeight: FontWeight.bold, color: Colors.grey, fontSize: 8), // Texts('${TranslationBase.of(context).vat}: $_tax ${TranslationBase.of(context).sar}', heightFactor: 1.5, fontWeight: FontWeight.bold, color: Colors.grey, fontSize: 8), // Padding( // padding: const EdgeInsets.all(3), // child: Container( // height: 0.25, // width: 120, // color: Colors.grey[300], // ), // ), // Texts('${TranslationBase.of(context).total}: $_total ${TranslationBase.of(context).sar}', heightFactor: 1.5, fontWeight: FontWeight.bold, color: Colors.black54, fontSize: 15) // ], // ), // ), // Expanded(child: Container()), // RaisedButton( // elevation: 0, // child: Texts( // TranslationBase.of(context).payNow, // fontSize: 15, // color: Colors.white, // fontWeight: FontWeight.bold, // ), // padding: EdgeInsets.only(top: 5, bottom: 5, left: 0, right: 0), // shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5), side: BorderSide(color: Theme.of(context).primaryColor, width: 0.5)), // color: Theme.of(context).primaryColor, // onPressed: isPayNowAQctive ? onPayNowClick : null, // ), // ], // )), // ); // } _getNormalText(text, {bool isBold = false, bool isTotal = false}) { return Text( text, style: TextStyle( fontSize: isBold ? isTotal ? 16 : 12 : 10, letterSpacing: -0.5, color: isBold ? Colors.black : Colors.grey[700], fontWeight: FontWeight.w600, ), ); } Widget _cartItemDeleteContainer() { _animationController.duration = Duration(milliseconds: 500); _animationController.repeat(reverse: true); return FadeTransition( opacity: _animationController, child: Padding( padding: const EdgeInsets.all(5), child: Container( decoration: BoxDecoration( color: Colors.red, boxShadow: [ BoxShadow( color: Colors.grey[500], blurRadius: 2, spreadRadius: 1, ), ], borderRadius: BorderRadius.all(Radius.circular(5)), ), child: Center( child: Texts( "Deleting...", fontWeight: FontWeight.normal, fontSize: 15, color: Colors.white, )), ), ), ); }