import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:in_app_purchase/in_app_purchase.dart'; import 'package:aslan/app_localizations.dart'; import 'package:aslan/chatvibe_ui/components/at_debounce_widget.dart'; import 'package:aslan/chatvibe_ui/components/at_compontent.dart'; import 'package:aslan/chatvibe_ui/components/text/at_text.dart'; import 'package:provider/provider.dart'; import 'package:aslan/chatvibe_ui/components/appbar/chatvibe_appbar.dart'; import 'package:aslan/chatvibe_core/constants/at_screen.dart'; import 'package:aslan/chatvibe_core/routes/at_fluro_navigator.dart'; import 'package:aslan/chatvibe_managers/apple_payment_manager.dart'; import 'package:aslan/chatvibe_managers/google_payment_manager.dart'; import 'package:aslan/chatvibe_managers/user_profile_manager.dart'; import 'package:aslan/chatvibe_features/wallet/wallet_route.dart'; import 'package:aslan/chatvibe_core/constants/at_global_config.dart'; class RechargePage extends StatefulWidget { @override _RechargePageState createState() => _RechargePageState(); } class _RechargePageState extends State { @override void initState() { super.initState(); Provider.of(context, listen: false).balance(); if (Platform.isAndroid) { Provider.of(context, listen: false).init(context); } else if (Platform.isIOS) { Provider.of(context, listen: false).init(context); } } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return Stack( children: [ Image.asset( ATGlobalConfig.businessLogicStrategy.getRechargePageBackgroundImage(), width: ScreenUtil().screenWidth, fit: BoxFit.fill, ), Scaffold( backgroundColor: ATGlobalConfig.businessLogicStrategy .getRechargePageScaffoldBackgroundColor(), resizeToAvoidBottomInset: false, appBar: ChatVibeStandardAppBar( title: ATAppLocalizations.of(context)!.recharge, actions: [ GestureDetector( child: Container( margin: EdgeInsetsDirectional.only(end: 15.w), child: Image.asset( ATGlobalConfig.businessLogicStrategy .getRechargePageRecordIcon(), width: 24.w, height: 24.w, ), ), onTap: () { showGeneralDialog( context: context, barrierLabel: '', barrierDismissible: true, transitionDuration: Duration(milliseconds: 350), barrierColor: ATGlobalConfig.businessLogicStrategy .getRechargePageDialogBarrierColor(), pageBuilder: ( BuildContext context, Animation animation, Animation secondaryAnimation, ) { return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { Navigator.pop(context); setState(() {}); }, child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ SizedBox(height: height(68)), Row( children: [ Spacer(), GestureDetector( child: Container( decoration: BoxDecoration( color: ATGlobalConfig.businessLogicStrategy .getRechargePageDialogContainerBackgroundColor(), borderRadius: BorderRadius.circular(4), ), padding: EdgeInsets.symmetric( horizontal: 8.w, vertical: 3.w, ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(height: 5.w), GestureDetector( child: text( ATAppLocalizations.of( context, )!.goldListort, textColor: ATGlobalConfig .businessLogicStrategy .getRechargePageDialogTextColor(), fontSize: 14.sp, ), onTap: () { Navigator.of(context).pop(); ATNavigatorUtils.push( context, WalletRoute.goldRecord, replace: false, ); }, ), // SizedBox(height: 5.w), // Container( // color: Colors.black26, // width: 110.w, // height: 0.5.w, // ), // SizedBox(height: 5.w,), // text( // ATAppLocalizations.of( // context, // )!.rechargeList, // textColor: Colors.black38, // fontSize: 14.sp, // ), SizedBox(height: 5.w), ], ), ), onTap: () {}, ), SizedBox(width: 10.w), ], ), Expanded(child: SizedBox(width: width(1))), ], ), ); }, ); }, ), ], ), body: SafeArea( top: false, child: Column( children: [ _buildWalletSection(), SizedBox(height: 20.w), Expanded( child: Container( padding: EdgeInsets.symmetric( vertical: 20.w, horizontal: 12.w, ), decoration: BoxDecoration( borderRadius: BorderRadius.only( topLeft: Radius.circular(12.0), topRight: Radius.circular(12.0), ), color: ATGlobalConfig.businessLogicStrategy .getRechargePageMainContainerBackgroundColor(), ), child: Column( children: [ Expanded( child: Platform.isAndroid ? Consumer( builder: (context, ref, child) { return ref.products.isNotEmpty ? ListView.separated( itemCount: ref.products.length, // 列表项数量 padding: EdgeInsets.only( bottom: 15.w, ), itemBuilder: (context, index) { return _buildGoogleProductItem( ref.products[index], ref, index, ); }, separatorBuilder: ( BuildContext context, int index, ) { return SizedBox(height: 10.w); }, ) : mainEmpty(); }, ) : Consumer( builder: (context, ref, child) { return ref.products.isNotEmpty ? ListView.separated( itemCount: ref.products.length, // 列表项数量 padding: EdgeInsets.only( bottom: 15.w, ), itemBuilder: (context, index) { return _buildAppleProductItem( ref.products[index], ref, index, ); }, separatorBuilder: ( BuildContext context, int index, ) { return SizedBox(height: 10.w); }, ) : mainEmpty(); }, ), ), ///针对ios的恢复购买 Platform.isIOS ? ATDebounceWidget( child: Container( alignment: Alignment.center, margin: EdgeInsets.symmetric(horizontal: 60.w), height: 42.w, decoration: BoxDecoration( color: ATGlobalConfig.businessLogicStrategy .getRechargePageButtonBackgroundColor(), borderRadius: BorderRadius.all( Radius.circular(height(8)), ), ), child: text( ATAppLocalizations.of( context, )!.restorePurchases, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageButtonTextColor(), fontSize: 16.sp, ), ), onTap: () { Provider.of( context, listen: false, ).restorePurchases(); }, ) : Container(), SizedBox(height: 15.w), ATDebounceWidget( child: Container( alignment: Alignment.center, margin: EdgeInsets.symmetric(horizontal: 60.w), height: 42.w, decoration: BoxDecoration( color: ATGlobalConfig.businessLogicStrategy .getRechargePageButtonBackgroundColor(), borderRadius: BorderRadius.all( Radius.circular(height(8)), ), ), child: text( ATAppLocalizations.of(context)!.recharge, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageButtonTextColor(), fontSize: 16.sp, ), ), onTap: () { if (Platform.isAndroid) { Provider.of( context, listen: false, ).buyProduct(); } else if (Platform.isIOS) { Provider.of( context, listen: false, ).buyProduct(); } }, ), SizedBox(height: 45.w), ], ), ), ), ], ), ), ), ], ); } Widget _buildWalletSection() { return Container( padding: EdgeInsets.symmetric(vertical: 16.w, horizontal: 20.w), margin: EdgeInsets.symmetric(horizontal: 15.w), decoration: BoxDecoration( image: DecorationImage( image: AssetImage( ATGlobalConfig.businessLogicStrategy .getRechargePageWalletBackgroundImage(), ), ), ), child: Row( children: [ // 左侧装饰图像 Image.asset( ATGlobalConfig.businessLogicStrategy.getRechargePageWalletIcon(), width: 60.w, height: 60.w, ), SizedBox(width: 12.w), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ text( ATAppLocalizations.of(context)!.wallet, fontSize: 24.sp, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageWalletTextColor(), fontWeight: FontWeight.w900, fontStyle: FontStyle.italic, ), SizedBox(height: 4.w), Row( children: [ Image.asset( ATGlobalConfig.businessLogicStrategy .getRechargePageGoldIcon(), width: 24.w, height: 24.w, ), SizedBox(width: 3.w), Consumer( builder: (context, ref, child) { return text( ref.myBalance > 1000 ? "${(ref.myBalance / 1000).toStringAsFixed(2)}k" : "${ref.myBalance}", fontSize: 16.sp, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageWalletTextColor(), fontWeight: FontWeight.bold, ); }, ), ], ), ], ), ), ], ), ); } Widget _buildGoogleProductItem( SelecteProductConfig productConfig, GooglePaymentManager ref, int index, ) { return GestureDetector( child: Container( padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 10.w), decoration: BoxDecoration( borderRadius: BorderRadius.circular(99), border: Border.all( color: productConfig.isSelecte ? ATGlobalConfig.businessLogicStrategy .getRechargePageSelectedItemBorderColor() : ATGlobalConfig.businessLogicStrategy .getRechargePageUnselectedItemBorderColor(), width: 1.w, ), ), child: Row( children: [ Image.asset( ATGlobalConfig.businessLogicStrategy .getRechargePageGoldIconByIndex(index), width: 34.w, height: 34.w, ), SizedBox(width: 5.w), text( "${ref.productMap[productConfig.produc.id]?.obtainCandy ?? 0}", fontSize: 12.sp, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageItemTextColor(), ), Spacer(), text( productConfig.produc.price, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageItemPriceTextColor(), fontWeight: FontWeight.w600, fontSize: 12.sp, ), ], ), ), onTap: () { ref.seleceConfig(index); }, ); } Widget _buildAppleProductItem( SelecteProductConfig productConfig, ApplePaymentManager ref, int index, ) { return GestureDetector( child: Container( padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 10.w), decoration: BoxDecoration( borderRadius: BorderRadius.circular(99), border: Border.all( color: productConfig.isSelecte ? ATGlobalConfig.businessLogicStrategy .getRechargePageSelectedItemBorderColor() : ATGlobalConfig.businessLogicStrategy .getRechargePageUnselectedItemBorderColor(), width: 1.w, ), ), child: Row( children: [ Image.asset( ATGlobalConfig.businessLogicStrategy .getRechargePageGoldIconByIndex(index), width: 34.w, height: 34.w, ), SizedBox(width: 5.w), text( "${ref.productMap[productConfig.produc.id]?.obtainCandy ?? 0}", fontSize: 12.sp, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageItemTextColor(), ), Spacer(), text( productConfig.produc.price, textColor: ATGlobalConfig.businessLogicStrategy .getRechargePageItemPriceTextColor(), fontWeight: FontWeight.w600, fontSize: 12.sp, ), ], ), ), onTap: () { ref.seleceConfig(index); }, ); } } class SelecteProductConfig { ProductDetails produc; bool isSelecte = false; SelecteProductConfig(this.produc, this.isSelecte); }