| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- 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<Recharge> createState() => _RechargeState();
- }
- class _RechargeState extends State<Recharge> {
- List<RechargeBean> 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<String, dynamic> data = await HttpUtils.get(Api.recharge);
- List<RechargeBean> array = [];
- if (data != null) {
- data['data'].forEach((obj) => {array.add(RechargeBean.fromJson(obj))});
- }
- setState(() {
- list = array;
- });
- }
- Future getBalance() async {
- Map<String, dynamic> 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<String, dynamic> data =
- await HttpUtils.post(Api.recharge, data: {'channel': 'wx', 'id': id});
- if (data != null) {
- Map<String, dynamic> 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<Widget>.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();
- }),
- ]));
- }
- }
|