import 'dart:async'; import 'package:fast/model/model.dart'; import 'package:fast/utils/api.dart'; import 'package:fast/utils/global.dart'; import 'package:fast/utils/http_utils.dart'; import 'package:fast/utils/size_fit.dart'; import 'package:fast/view/balance_history.dart'; import 'package:fast/view/component/fast_btn.dart'; import 'package:fast/view/pay_result.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:fluwx/fluwx.dart'; import '../constants.dart'; import 'component/navi_bar.dart'; class Recharge extends StatefulWidget { const Recharge({Key? key}) : super(key: key); @override State createState() => _RechargeState(); } class _RechargeState extends State { List list = []; StreamSubscription? subscription; bool isCharging = false; int balance = 0; @override void initState() { // TODO: implement initState setState(() { balance = Global().balance; }); getSkus(); initWxApi(); super.initState(); } Future getSkus() async { Map data = await HttpUtils.get(Api.recharge); List array = []; if (data != null) { data['data'].forEach((obj) => {array.add(RechargeBean.fromJson(obj))}); } setState(() { list = array; }); } Future getBalance() async { Map data = await HttpUtils.get(Api.balance); Global().balance = data['rjv_balance']; if (mounted) { setState(() { balance = Global().balance; }); } return data; } Future initWxApi() async { bool isSuccess = await registerWxApi( appId: 'wxa8557217acf4f532', universalLink: 'https://api.fast.liveplus.fun/'); if (isSuccess) { subscription = weChatResponseEventHandler.listen((event) { if (event is WeChatPaymentResponse) { isCharging = false; if (event.isSuccessful) { print('支付成功'); Get.to(() => PayResult( success: true, )); getBalance(); } else { print('支付失败'); Get.to(() => PayResult( success: false, )); } print(event.toString()); } else if (event is WeChatAuthResponse) { print('9527'); } else if (event is WeChatShareResponse) {} }); } } Future recharge() async { if (isCharging) { return; } isCharging = true; String id = ''; list.forEach((e) => { if (e.def == true) {id = e.id} }); Map data = await HttpUtils.post(Api.recharge, data: {'channel': 'wx', 'id': id}); if (data != null) { Map info = data['credential']['wx']; var result = payWithWeChat( appId: info['sign'], partnerId: info['partnerId'], prepayId: info['prepayId'], packageValue: info['packageValue'], nonceStr: info['nonceStr'], timeStamp: int.parse(info['timeStamp']), sign: info['sign']); if (result != null) { print('success'); } } } Widget item(RechargeBean bean) { return Stack( children: [ GestureDetector( onTap: () { list.forEach((element) { element.def = false; }); bean.def = true; setState(() { list = [...list]; }); }, child: Container( width: 310.px, height: 56.px, margin: EdgeInsets.only( bottom: 2.px, top: 8.px, left: 5.px, right: 5.px), decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(28.px)), color: const Color(0xFF142133), border: Border.all( color: bean.def ? kThemeColor : const Color(0xFF142133), width: 2.px)), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ SizedBox( width: 26.px, ), Image.asset( 'assets/images/stone.png', width: 24.px, height: 24.px, ), SizedBox( width: 8.px, ), Text( '×', style: TextStyle( color: const Color(0x66C4CCDA), fontSize: 28.px, fontFamily: 'Exo2', fontWeight: FontWeight.bold), ), SizedBox( width: 8.px, ), Expanded( child: Text( bean.rjv.toString(), style: TextStyle( color: kThemeColor, fontFamily: 'Exo2', fontWeight: FontWeight.bold, fontSize: 28.px), ), ), Text( '¥${bean.amount}', style: TextStyle( color: bean.def ? Colors.white : const Color(0xCCC4CCDA), fontSize: bean.def ? 18.px : 14.px, fontFamily: 'Exo2'), ), SizedBox( width: 10.px, ), Image.asset( bean.def ? 'assets/images/checked.png' : 'assets/images/check.png', width: 24.px, height: 24.px, ), SizedBox( width: 16.px, ) ], ), ), ), if (bean.tags.isNotEmpty) Positioned( left: 0, top: 0, child: Container( padding: EdgeInsets.only(left: 8.px, right: 8.px), height: 18.px, alignment: Alignment.center, decoration: BoxDecoration( // borderRadius: BorderRadius.all(Radius.circular(9.px)), borderRadius: BorderRadius.only( topLeft: Radius.circular(9.px), topRight: Radius.circular(9.px), bottomLeft: Radius.circular(9.px)), gradient: LinearGradient( begin: Alignment.centerLeft, end: Alignment.centerRight, colors: bean.tags == '推荐' ? [const Color(0xFFFFD500), const Color(0xFFFF8000)] : [ const Color(0xFFAA00FF), const Color(0xFF0080FF) ])), child: Text( bean.tags, style: TextStyle( color: bean.tags == '推荐' ? Colors.black : Colors.white, fontWeight: FontWeight.bold, fontSize: 10.px), ), )) ], ); } @override Widget build(BuildContext context) { SizeFit.initialize(context); EdgeInsets safePadding = MediaQuery.of(context).padding; return Material( color: kBgColor, child: Stack(children: [ SizedBox( height: MediaQuery.of(context).size.height, child: SingleChildScrollView( child: Column(children: [ Container( margin: EdgeInsets.only(top: safePadding.top + 40.px), ), SizedBox( height: 14.px, ), Text( balance.toString(), style: TextStyle( color: kThemeColor, fontSize: 72.px, fontFamily: 'Exo2', fontWeight: FontWeight.bold), ), SizedBox( height: 10.px, ), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Image.asset( 'assets/images/stone.png', width: 16.px, height: 16.px, ), SizedBox( width: 4.px, ), Text( '逆龄石余额', style: TextStyle( color: Colors.white, fontSize: 16.px, fontWeight: FontWeight.bold), ) ], ), SizedBox( height: 20.px, ), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ GestureDetector( onTap: () { Get.to(() => BalanceHistory( type: 0, )); }, child: Container( alignment: Alignment.center, width: 80.px, height: 24.px, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(12.px)), border: Border.all( color: const Color(0x66FFFFFF), width: 1.px)), child: Text( '全部记录', style: TextStyle( color: const Color(0x66FFFFFF), fontSize: 12.px), ), ), ), SizedBox( width: 16.px, ), GestureDetector( onTap: () { Get.to(() => BalanceHistory( type: 1, )); }, child: Container( alignment: Alignment.center, width: 80.px, height: 24.px, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(12.px)), border: Border.all( color: const Color(0x66FFFFFF), width: 1.px)), child: Text( '充值记录', style: TextStyle( color: const Color(0x66FFFFFF), fontSize: 12.px), ), ), ) ], ), SizedBox( height: 32.px, ), ...List.generate(list.length, (i) { return item(list[i]); }), SizedBox( height: 32.px, ), FastBtn( title: '充值', disable: false, width: 310.px, height: 48.px, callback: () { recharge(); }) ]))), NaviBar( title: '充值', closeCallback: () { Get.back(); }), ])); } }