You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
8.4 KiB
Dart
210 lines
8.4 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:mc_common_app/classes/app_state.dart';
|
|
import 'package:mc_common_app/config/routes.dart';
|
|
import 'package:mc_common_app/extensions/int_extensions.dart';
|
|
import 'package:mc_common_app/extensions/string_extensions.dart';
|
|
import 'package:mc_common_app/generated/locale_keys.g.dart';
|
|
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
|
|
import 'package:mc_common_app/models/subscriptions_models/subscription_model.dart';
|
|
import 'package:mc_common_app/theme/colors.dart';
|
|
import 'package:mc_common_app/utils/date_helper.dart';
|
|
import 'package:mc_common_app/utils/enums.dart';
|
|
import 'package:mc_common_app/utils/navigator.dart';
|
|
import 'package:mc_common_app/utils/utils.dart';
|
|
import 'package:mc_common_app/view_models/payment_view_model.dart';
|
|
import 'package:mc_common_app/view_models/subscriptions_view_model.dart';
|
|
import 'package:mc_common_app/views/payments/payment_methods_view.dart';
|
|
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
|
|
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
class UpgradePackageSheet extends StatefulWidget {
|
|
final Subscription subscription;
|
|
final bool isRenew;
|
|
|
|
const UpgradePackageSheet({required this.subscription, required this.isRenew, super.key});
|
|
|
|
@override
|
|
State<UpgradePackageSheet> createState() => _UpgradePackageSheetState();
|
|
}
|
|
|
|
class _UpgradePackageSheetState extends State<UpgradePackageSheet> {
|
|
bool isNowSelected = true;
|
|
String price = "0";
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
scheduleMicrotask(() {
|
|
calculateUpgradePrice();
|
|
});
|
|
}
|
|
|
|
calculateUpgradePrice() {
|
|
context.read<SubscriptionsVM>().calculationUpgradePrice(
|
|
AppState().getUser.data?.userInfo?.providerId.toString() ?? "",
|
|
widget.subscription.id.toString(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.only(left: 21, right: 21, bottom: 21, top: 4),
|
|
child: Consumer<SubscriptionsVM>(builder: (context, model, _) {
|
|
if (model.state == ViewState.busy) {
|
|
log(model.newPrice);
|
|
return SizedBox(
|
|
width: double.infinity,
|
|
height: MediaQuery.of(context).size.height / 2,
|
|
child: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
} else {
|
|
if (isNowSelected) {
|
|
price = double.parse(model.newPrice).toStringAsFixed(2);
|
|
}
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: [
|
|
(widget.isRenew ? LocaleKeys.whendoyouWanttoRenew.tr() : LocaleKeys.whendoyouWanttoUpgrade.tr()).toText(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
12.height,
|
|
packageItem(
|
|
LocaleKeys.nowVar.tr(),
|
|
("${LocaleKeys.yourSubscriptionUpgradeAffectingFrom.tr()} ${widget.subscription.dateEnd == null ? DateHelper.formatAsDayMonthYear(DateTime.now()) : DateHelper.formatAsDayMonthYear(DateHelper.parseStringToDate(DateHelper.formatDateT(widget.subscription.dateEnd!)))}"),
|
|
double.parse(model.newPrice).toStringAsFixed(2),
|
|
isNowSelected,
|
|
).onPress(() {
|
|
setState(() {
|
|
price = model.newPrice.toString();
|
|
isNowSelected = true;
|
|
});
|
|
}),
|
|
6.height,
|
|
if (AppState().getproviderSubscription.isNotEmpty && !AppState().getproviderSubscription.first.isExpired! && !widget.isRenew) ...[
|
|
packageItem(
|
|
LocaleKeys.upgradeAfterCurrentExpiry.tr(),
|
|
("${LocaleKeys.yourSubscriptionUpgradeCurrentSubscriptionAffectingFrom.tr()} ${AppState().getproviderSubscription.first.dateEnd == null ? "" : AppState().getproviderSubscription.first.dateEnd == null ? DateHelper.formatAsDayMonthYear(DateTime.now()) : DateHelper.formatAsDayMonthYear(DateHelper.parseStringToDate(DateHelper.formatDateT(AppState().getproviderSubscription.first.dateEnd!)))}"),
|
|
widget.subscription.price.toString(),
|
|
isNowSelected ? false : true,
|
|
).onPress(() {
|
|
price = widget.subscription.price.toString();
|
|
setState(() {
|
|
isNowSelected = false;
|
|
});
|
|
}),
|
|
],
|
|
12.height,
|
|
ShowFillButton(
|
|
title: widget.isRenew ? LocaleKeys.renewVar.tr() : LocaleKeys.upgradeVar.tr(),
|
|
maxWidth: double.infinity,
|
|
onPressed: () async {
|
|
if (widget.isRenew) {
|
|
Utils.showLoading(context);
|
|
GenericRespModel response = await model.createSubscriptionOrder(widget.subscription.id ?? 0, true, true);
|
|
Utils.hideLoading(context);
|
|
if (response.data != null) {
|
|
context.read<PaymentVM>().updateOrderProviderSubscriptionId(id: response.data ?? 0);
|
|
navigateWithName(context, AppRoutes.paymentMethodsView, arguments: PaymentTypes.subscription);
|
|
}
|
|
} else {
|
|
navigateTo(
|
|
context,
|
|
PaymentMethodsView(
|
|
paymentType: PaymentTypes.subscription,
|
|
onTempContinue: () async {
|
|
Utils.showLoading(context);
|
|
GenericRespModel mResponse = await model.payForSubscription(widget.subscription.id!, isNowSelected, false, price);
|
|
Utils.hideLoading(context);
|
|
if (mResponse.messageStatus == 1) {
|
|
context.read<PaymentVM>().updateOrderProviderSubscriptionId(id: mResponse.data);
|
|
|
|
// Utils.showToast(
|
|
// "Subscriptions is successfully updated");
|
|
return true;
|
|
} else {
|
|
Utils.showToast(mResponse.message.toString());
|
|
return false;
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
|
|
Widget packageItem(String title, String description, String price, bool isSelected) {
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: [
|
|
const Icon(
|
|
Icons.done,
|
|
size: 14,
|
|
color: Colors.white,
|
|
).toContainer(
|
|
isEnabledBorder: true,
|
|
backgroundColor: isSelected ? MyColors.primaryColor : Colors.transparent,
|
|
borderColor: MyColors.primaryColor,
|
|
borderWidget: 1,
|
|
borderRadius: 0,
|
|
paddingAll: 2,
|
|
),
|
|
12.width,
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: [
|
|
title.toText(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
4.height,
|
|
// if (widget.subscription.dateEnd != null)
|
|
description.toText(
|
|
fontSize: 12,
|
|
color: MyColors.lightTextColor,
|
|
),
|
|
10.height,
|
|
Row(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
price.toText(fontSize: 24, isBold: true),
|
|
2.width,
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 2),
|
|
child: LocaleKeys.sar.tr().toText(fontSize: 16, isBold: true, color: MyColors.lightTextColor),
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
],
|
|
).toContainer(
|
|
isShadowEnabled: true,
|
|
paddingAll: 16,
|
|
borderRadius: 0,
|
|
borderColor: isSelected ? MyColors.primaryColor : Colors.transparent,
|
|
isEnabledBorder: true,
|
|
borderWidget: 2,
|
|
);
|
|
}
|
|
}
|