import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:fluwx/fluwx.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../generated/l10n.dart'; import '../../retrofit/data/base_data.dart'; import '../../retrofit/data/vip_card_home.dart'; import '../../retrofit/data/wx_pay.dart'; import '../../retrofit/retrofit_api.dart'; import '../../utils/flutter_utils.dart'; import '../../utils/min.dart'; import 'package:tobias/tobias.dart' as tobias; class VipTop extends StatefulWidget { final VipCardHome vipCardHome; final Function queryVipHome; VipTop(this.vipCardHome, this.queryVipHome); @override State createState() { return _VipTop(); } } class _VipTop extends State { int selectIndex = 0; ApiService apiService; var payType = 1; dynamic payListen; @override void initState() { super.initState(); payListen = weChatResponseEventHandler.listen((event) async { print("payCallback: ${event.errCode}"); if (event.errCode == 0) { widget.queryVipHome(); SmartDialog.showToast("支付成功", alignment: Alignment.center); } else { SmartDialog.showToast("支付失败", alignment: Alignment.center); return; } }); } recharge() async { if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService(Dio(), context: context, token: value.getString("token"), showLoading: true); } BaseData baseData = await apiService.orderVip({ "cardId": widget?.vipCardHome?.cards[selectIndex]?.id, "type": payType }).catchError((error) {}); if (baseData != null && baseData.isSuccess) { if (payType == 1) { if (Platform.isAndroid) { if (!(await Min.isInitialize())) { // 小程序的微信支付和app的充值支付使用同一个WXPayEntryActivity回调, // 然而充值时小程序未初始化会导致回调内部代码调用getPackage空指针, // 故而在此初始化一下 await Min.initialize(); } } WxPay wxPay = WxPay.fromJson(baseData.data); await registerWxApi( appId: wxPay.appId, doOnAndroid: true, universalLink: "https://hx.lotus-wallet.com/app/", ); payWithWeChat( appId: wxPay.appId, partnerId: wxPay.partnerId, prepayId: wxPay.prepayId, packageValue: wxPay.packageValue, nonceStr: wxPay.nonceStr, timeStamp: int.tryParse(wxPay.timeStamp), sign: wxPay.sign, ); } else { tobias.isAliPayInstalled().then((value) => { // 判断是否安装了支付宝 if (!value) {SmartDialog.showToast("请安装支付宝", alignment: Alignment.center)} else { tobias.aliPay(baseData.data["body"]).then((payRes) { if (payRes['resultStatus'] == 9000 || payRes['resultStatus'] == '9000') { widget.queryVipHome(); SmartDialog.showToast("支付成功", alignment: Alignment.center); } else { SmartDialog.showToast(payRes['memo'], alignment: Alignment.center); } }) } }); } } else { SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } @override Widget build(BuildContext context) { return Container( child: Column( children: [ vipCard(), SizedBox( height: 15.h, ), (!(widget?.vipCardHome?.member?.isVip ?? false)) ? vipCardList() : Padding( padding: EdgeInsets.symmetric(horizontal: 16.w), child: Row( children: [ Expanded( child: Container( alignment: Alignment.center, padding: EdgeInsets.symmetric( horizontal: 10.w, vertical: 15.h), decoration: BoxDecoration( borderRadius: BorderRadius.circular(4.w), color: Color(0xffffffff), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Text.rich( TextSpan( children: [ TextSpan( text: S.of(context).yixianghuiyuanquanyi, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 15.sp, color: Color(0xff32A060), ), ), TextSpan( text: widget ?.vipCardHome?.member?.vipDuration .toString() ?? "0", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 18.sp, color: Color(0xffF96519), ), ), TextSpan( text: " 天", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 15.sp, color: Color(0xff32A060), ), ), ], ), textDirection: TextDirection.ltr, ))), SizedBox( width: 10.w, ), Expanded( child: GestureDetector( onTap: () { setState(() { vipRenewShowBottomSheet(); }); }, child: Container( alignment: Alignment.center, padding: EdgeInsets.symmetric( horizontal: 10.w, vertical: 15.h), decoration: BoxDecoration( borderRadius: BorderRadius.circular(4.w), color: Color(0xffffffff), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( "${(widget?.vipCardHome?.member?.isVipSubscribe ?? false) ? "已开通自动续费" : "续费回乡VIP"}", style: TextStyle( color: Color((widget?.vipCardHome ?.member?.isVipSubscribe ?? false) ? 0xff3A3936 : 0xff32A060), fontSize: 15.sp, fontWeight: MyFontWeight.semi_bold, ), ), SizedBox( width: 6.w, ), Image.asset( "${(widget?.vipCardHome?.member?.isVipSubscribe ?? false) ? "assets/image/vip_xf.webp" : "assets/image/icon_right.webp"}", width: 16, height: 16, fit: BoxFit.fill, color: Color(0xff32A060), ), ], ))), ) ], ), ), ], ), ); } ///vip卡 Widget vipCard() { return Container( margin: EdgeInsets.fromLTRB( 14.w, MediaQuery.of(context).padding.top + 25.h, 14.w, 8.h), height: 167.h, decoration: BoxDecoration( borderRadius: BorderRadius.circular(8.w), color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Stack( // alignment: Alignment.center, children: [ Image.asset( "assets/image/vip_card.webp", fit: BoxFit.fill, //填充剩余空间 height: 167.h, ), Container( padding: EdgeInsets.all(16.w), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Text( S.of(context).huixiangVIPka, style: TextStyle( color: Color(0xff32A060), fontSize: 24.sp, fontWeight: MyFontWeight.semi_bold, ), )), Text( widget?.vipCardHome?.member?.nickname ?? "", style: TextStyle( color: Colors.white, fontSize: 15.sp, fontWeight: MyFontWeight.regular, ), ), SizedBox( height: 15.h, ), if (widget?.vipCardHome?.member?.isVip ?? false) Text( "有效期至 ${(widget?.vipCardHome?.member?.vipExpire ?? "").substring(0, (widget?.vipCardHome?.member?.vipExpire ?? "").indexOf(" "))}", style: TextStyle( color: Color(0xffFFFFFF), fontSize: 13.sp, fontWeight: MyFontWeight.regular, ), ), ], ), ), ], ), ); } ///VIP购买套餐 Widget vipCardList() { return Container( height: 136.h, margin: EdgeInsets.symmetric(horizontal: 14.w), child: ListView.builder( scrollDirection: Axis.horizontal, physics: BouncingScrollPhysics(), itemCount: widget?.vipCardHome?.cards?.length ?? 0, itemBuilder: (context, position) { return GestureDetector( onTap: () { setState(() { selectIndex = position; if (widget?.vipCardHome?.member?.isVip ?? false) { vipShowBottomSheet(); } else { vipTreatyShowBottomSheet(); } }); }, child: vipCardItem(widget?.vipCardHome?.cards[position], position), ); }, ), ); } Widget vipCardItem(Cards cards, index) { return Stack( alignment: Alignment.topRight, children: [ Container( height: double.infinity, width: 166.w, alignment: Alignment.center, padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 25.h), margin: EdgeInsets.only(right: 15.w), decoration: BoxDecoration( borderRadius: BorderRadius.circular(6.w), color: Color(selectIndex == index ? 0xffF0FAF4 : 0xffffffff), border: Border.all( color: Color(selectIndex == index ? 0xff32A060 : 0xFFEAEAEA), width: 2, ), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Column( children: [ Row( children: [ Expanded( child: Text( "${cards?.duration.toString() ?? ""}天", textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontSize: 15.sp, fontWeight: MyFontWeight.medium, ), ), ), Expanded( child: Text( cards?.name ?? "", textAlign: TextAlign.center, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( color: Colors.black, fontSize: 15.sp, fontWeight: MyFontWeight.medium, ), ), ), ], ), SizedBox( height: 10.h, ), Text( "¥${AppUtils.calculateDouble(double.tryParse(cards?.price ?? ""))}", textAlign: TextAlign.center, style: TextStyle( color: Color(0xff32A060), fontSize: 36.sp, fontWeight: MyFontWeight.medium, ), ), ], ), ), if (cards.autoSubscribe) Container( padding: EdgeInsets.symmetric(vertical: 4.h, horizontal: 6.w), margin: EdgeInsets.only(right: 15.w), decoration: BoxDecoration( borderRadius: BorderRadius.only( topRight: Radius.circular(6.r), bottomLeft: Radius.circular(6.r), ), color: Color(0xff32A060), ), child: Text( S.of(context).gengyouhui, style: TextStyle( color: Colors.white, fontSize: 12.sp, fontWeight: MyFontWeight.regular, ), ), ) ], ); } ///会员卡续费弹窗 vipRenewShowBottomSheet() { showModalBottomSheet( builder: (BuildContext context) { return StatefulBuilder(builder: ( context, state, ) { return renewBuildBottomSheetWidget(context, state); }); }, backgroundColor: Colors.transparent, context: context); } Widget renewBuildBottomSheetWidget(BuildContext context, state) { return Container( padding: EdgeInsets.only( top: 10.h, ), decoration: new BoxDecoration( color: Colors.white, borderRadius: new BorderRadius.only( topLeft: const Radius.circular(8.0), topRight: const Radius.circular(8.0))), child: Container( height: 265.h, child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Padding( padding: EdgeInsets.only(right: 14.w), child: Row( children: [ Expanded( child: Text( S.of(context).xuefeihuiyuan, textAlign: TextAlign.center, style: TextStyle( fontWeight: MyFontWeight.medium, fontSize: 15.sp, color: Color(0xff0D0D0D), ), )), GestureDetector( onTap: () { Navigator.of(context).pop(); }, child: Icon( Icons.clear, color: Colors.black, ), ), ], )), SizedBox( height: 11.h, ), Container( height: 136.h, margin: EdgeInsets.symmetric(horizontal: 14.w), child: ListView.builder( scrollDirection: Axis.horizontal, physics: BouncingScrollPhysics(), itemCount: widget?.vipCardHome?.cards?.length ?? 0, itemBuilder: (context, position) { return GestureDetector( onTap: () { state(() { selectIndex = position; }); }, child: vipCardItem( widget?.vipCardHome?.cards[position], position), ); }, ), ), Container( width: double.infinity, height: 40.h, margin: EdgeInsets.only( top: 14.h, left: 14.w, right: 14.w, bottom: 5.h), decoration: new BoxDecoration( color: Color(0xff383A38), borderRadius: BorderRadius.circular(24)), child: GestureDetector( onTap: () { Navigator.of(context).pop(); vipShowBottomSheet(); }, child: Container( height: double.infinity, alignment: Alignment.center, padding: EdgeInsets.symmetric(horizontal: 16.w), decoration: new BoxDecoration( color: Color(0xff32A060), borderRadius: BorderRadius.circular(24)), child: Text( S.of(context).likexufei, style: TextStyle( fontSize: 16.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFFFFFFFF), ), ), ), ), ), GestureDetector( onTap: () { Navigator.of(context) .pushNamed('/router/user_vip_service_page'); }, child: Padding( padding: EdgeInsets.only(left: 14.w, bottom: 15.h), child: Text.rich( TextSpan( children: [ TextSpan( text: "开通即默认同意", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Color(0xff4D4D4D), ), ), TextSpan( text: "《回乡VIP会员卡规则协议》", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Color(0xff32A060), ), ), ], ), textDirection: TextDirection.ltr, ), ), ), ], ), )); } ///会员卡包月协议弹窗 vipTreatyShowBottomSheet() { showModalBottomSheet( builder: (BuildContext context) { return treatyBuildBottomSheetWidget(context); }, backgroundColor: Colors.transparent, context: context); } Widget treatyBuildBottomSheetWidget(BuildContext context) { return Container( padding: EdgeInsets.symmetric(vertical: 20.h, horizontal: 14.w), height: 127.h, decoration: new BoxDecoration( color: Colors.white, borderRadius: new BorderRadius.only( topLeft: const Radius.circular(6.0), topRight: const Radius.circular(6.0))), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ GestureDetector( onTap: () { Navigator.of(context) .pushNamed('/router/user_vip_service_page'); }, child: Padding( padding: EdgeInsets.only(left: 4.w), child: Text.rich( TextSpan( children: [ TextSpan( text: "开通即默认同意", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Color(0xff4D4D4D), ), ), TextSpan( text: "《回乡VIP会员卡规则协议》", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Color(0xff32A060), ), ), ], ), textDirection: TextDirection.ltr, ), ), ), GestureDetector( onTap: () { Navigator.of(context).pop(); vipShowBottomSheet(); }, child: Container( height: 40.h, alignment: Alignment.center, margin: EdgeInsets.only(top: 26.h), padding: EdgeInsets.symmetric(horizontal: 16.w), decoration: new BoxDecoration( color: Color(0xff32A060), borderRadius: BorderRadius.circular(24)), child: Text( S.of(context).likekaitong, style: TextStyle( fontSize: 16.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFFFFFFFF), ), ), ), ) ], )); } ///会员卡包月购买弹窗 vipShowBottomSheet() { showModalBottomSheet( builder: (BuildContext context) { return StatefulBuilder(builder: ( context, state, ) { return buildBottomSheetWidget(context, state); }); }, backgroundColor: Colors.transparent, context: context); } Widget buildBottomSheetWidget(BuildContext context, state) { return Container( padding: EdgeInsets.all(16), decoration: new BoxDecoration( color: Colors.white, borderRadius: new BorderRadius.only( topLeft: const Radius.circular(8.0), topRight: const Radius.circular(8.0))), child: Container( height: 230.h, child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Row( children: [ Expanded( child: Text( S.of(context).zhifufangshi, textAlign: TextAlign.center, style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 15.sp, color: Color(0xff0D0D0D), ), )), GestureDetector( onTap: () { Navigator.of(context).pop(); }, child: Icon( Icons.clear, color: Colors.black, ), ), ], ), if (!(widget?.vipCardHome?.cards[selectIndex]?.autoSubscribe ?? false)) GestureDetector( onTap: () { state(() { payType = 1; }); }, child: Container( padding: EdgeInsets.only( left: 14.w, right: 32.w, top: 11.h, bottom: 11.h), margin: EdgeInsets.only(top: 17.h), decoration: BoxDecoration( borderRadius: BorderRadius.circular(4.w), color: Color(payType == 1 ? 0xFFF0FAF4 : 0xffF9F9F9), border: Border.all( color: Color(payType == 1 ? 0xFF32A060 : 0xFFFAFAFA), width: payType == 1 ? 2 : 0, ), boxShadow: [ BoxShadow( color: Color(0xffF9F9F9).withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Row( children: [ Image.asset( "assets/image/icon_we_chat.webp", width: 20, fit: BoxFit.fill, height: 20, ), SizedBox( width: 8.w, ), Text( S.of(context).weixinzhifu, style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 15.sp, color: Color(0xff0D0D0D), ), ), SizedBox( width: 20.w, ), ], ), ), ), GestureDetector( onTap: () { state(() { payType = 2; }); }, child: Container( padding: EdgeInsets.only( left: 14.w, right: 32.w, top: 11.h, bottom: 11.h), margin: EdgeInsets.only(top: 17.h), decoration: BoxDecoration( borderRadius: BorderRadius.circular(4.w), color: Color(payType == 2 ? 0xFFF0FAF4 : 0xffF9F9F9), border: Border.all( color: Color(payType == 2 ? 0xFF32A060 : 0xFFFAFAFA), width: payType == 2 ? 2 : 0, ), boxShadow: [ BoxShadow( color: Color(0xffF9F9F9).withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Row( children: [ Image.asset( "assets/image/icon_alipay.webp", width: 20, fit: BoxFit.fill, height: 20, ), SizedBox( width: 8.w, ), Text( S.of(context).zhifubaozhifu, style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 15.sp, color: Color(0xff0D0D0D), ), ), SizedBox( width: 20.w, ), ], ), ), ), Spacer(), Container( width: double.infinity, height: 40.h, decoration: new BoxDecoration( color: Color(0xff383A38), borderRadius: BorderRadius.circular(24)), child: Row( children: [ SizedBox( width: 16.w, ), Expanded( child: Text.rich( TextSpan( children: [ TextSpan( text: "¥", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Colors.white, ), ), TextSpan( text: AppUtils.calculateDouble(double.tryParse( widget?.vipCardHome?.cards[selectIndex] ?.price ?? "")), style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 20.sp, color: Colors.white, ), ), ], ), textDirection: TextDirection.ltr, )), GestureDetector( onTap: () { Navigator.of(context).pop(); recharge(); }, child: Container( height: double.infinity, alignment: Alignment.center, padding: EdgeInsets.symmetric(horizontal: 16.w), decoration: new BoxDecoration( color: Color(0xff32A060), borderRadius: BorderRadius.circular(24)), child: Text( S.of(context).likezhifu, style: TextStyle( fontSize: 16.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFFFFFFFF), ), ), ), ) ], ), ) ], ), )); } }