import ' dart:async ' ;
import ' package:flutter/material.dart ' ;
import ' package:mc_common_app/classes/app_state.dart ' ;
import ' package:mc_common_app/extensions/int_extensions.dart ' ;
import ' package:mc_common_app/extensions/string_extensions.dart ' ;
import ' package:mc_common_app/models/general_models/m_response.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 ' ;
class UpgradePackageSheet extends StatefulWidget {
Subscription subscription ;
UpgradePackageSheet ( this . subscription , { 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 ) {
return SizedBox (
width: double . infinity ,
height: MediaQuery . of ( context ) . size . height / 2 ,
child: const Center ( child: CircularProgressIndicator ( ) ) ,
) ;
} else {
if ( isNowSelected ) {
price = model . newPrice . toString ( ) ;
}
return Column (
crossAxisAlignment: CrossAxisAlignment . start ,
mainAxisAlignment: MainAxisAlignment . start ,
children: [
" When do you want to upgrade " . toText (
fontSize: 18 ,
fontWeight: FontWeight . bold ,
) ,
12. height ,
packageItem (
" Now " ,
( " Your subscription will upgrade now. Affecting from ${ DateHelper . formatAsLongDate ( widget . subscription . dateEnd ? ? DateTime . now ( ) ) } " ) ,
model . newPrice . toString ( ) ,
isNowSelected ,
) . onPress ( ( ) {
setState ( ( ) {
price = model . newPrice . toString ( ) ;
isNowSelected = true ;
} ) ;
} ) ,
6. height ,
packageItem (
" Upgrade after current expiry " ,
( " Your subscription will upgrade after you current subscription will end. Affecting from 27 January, 2024 ${ DateHelper . formatAsLongDate ( widget . subscription . dateEnd ? ? DateTime . now ( ) ) } " ) ,
widget . subscription . price . toString ( ) ,
isNowSelected ? false : true ,
) . onPress ( ( ) {
price = widget . subscription . price . toString ( ) ;
setState ( ( ) {
isNowSelected = false ;
} ) ;
} ) ,
12. height ,
ShowFillButton (
title: " Upgrade " ,
maxWidth: double . infinity ,
onPressed: ( ) async {
navigateTo (
context ,
PaymentMethodsView (
paymentType: PaymentTypes . subscription ,
onTempContinue: ( ) async {
Utils . showLoading ( context ) ;
MResponse 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: " SAR " . 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 ,
) ;
}
}