import 'dart:convert'; import 'package:bbyyy/beans/pay_way_bean_entity.dart'; import 'package:bbyyy/beans/user_balance_entity.dart'; import 'package:bbyyy/beans/user_bean_entity.dart'; import 'package:bbyyy/beans/withdraw_pricing_bean_entity.dart'; import 'package:bbyyy/https/MyDio.dart'; import 'package:bbyyy/my_tools/dims.dart'; import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart'; import 'package:bbyyy/my_tools/event_bus.dart'; import 'package:bbyyy/my_tools/my_apis.dart'; import 'package:bbyyy/my_tools/my_colors.dart'; import 'package:bbyyy/my_tools/my_cookie.dart'; import 'package:bbyyy/my_tools/my_tools.dart'; import 'package:bbyyy/my_tools/my_views.dart'; import 'package:bbyyy/paegs/mine_page/bind_alipay_page/bind_alipay_page.dart'; import 'package:bbyyy/pay/pay_tools.dart'; import 'package:flustars/flustars.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:pinput/pin_put/pin_put.dart'; class WithdrawPage extends StatefulWidget { @override _WithdrawPageState createState() => _WithdrawPageState(); } class _WithdrawPageState extends State { TextEditingController _controller = TextEditingController(); int withdrawalStatus = 1; //0--未提现 1--提现中 2--提现完成 double percentFee = 0.0; double extraFee = 0.0; double threshold = 0.0; double maxPerHand = 0.0; double minPerHand = 0.0; int withdrawalPath = -1; // 1---支付宝 2---微信 List payWays; bool showAlipay = false; bool showWeChat = false; bool showHandlingFee = false; double handlingFee = 0.0; double balance = 0.0; String withdrawPassword = ''; @override void initState() { // TODO: implement initState super.initState(); queryPersonalInformation(); queryWithdrawalInstructions(); Future.delayed(Duration.zero, () { checkPaymentMethod((response, hasError) { if (!hasError) { PayWayBeanEntity entity = PayWayBeanEntity.fromJson(json.decode(response.toString())); payWays = entity.data; showWeChat = payWays[payWays.indexWhere((element) => element.way == 2)] .withdraw; showAlipay = payWays[payWays.indexWhere((element) => element.way == 1)] .withdraw; if (MyCookie().userBean.aliPayAccount.isNotEmpty && payWays[payWays.indexWhere((element) => element.way == 1)] .withdraw) { withdrawalPath = 1; } else if (MyCookie().userBean.wxOpenid.isNotEmpty && payWays[payWays.indexWhere((element) => element.way == 2)] .withdraw) { withdrawalPath = 2; } setState(() {}); } }, (e) {}); EasyLoading.show(); getUserWalletBalance((re, hE) { if (!hE) { UserBalanceEntity balance = UserBalanceEntity.fromJson(json.decode(re.data.toString())); MyCookie().balance = balance.data; setState(() { EasyLoading.dismiss(); }); } }, (e) { EasyLoading.dismiss(); }, context); }); } @override Widget build(BuildContext context) { return GestureDetector( onTap: () { MyTools().hideKeyboard(context); }, behavior: HitTestBehavior.translucent, child: Scaffold( backgroundColor: MyColors.cF7F7F7, body: Column( children: [ MyViews().myAppBar('申请提现', context, [ GestureDetector( onTap: () { MyTools().toPage(context, BindAlipayPage(), (then) { setState(() {}); }); }, behavior: HitTestBehavior.translucent, child: Container( padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: Text( '修改绑定', style: TextStyle(color: MyColors.c333333, fontSize: 16), ), ), ), ]), Expanded( child: SingleChildScrollView( child: Column( children: [ if (showAlipay) GestureDetector( onTap: () { if (!payWays[payWays .indexWhere((element) => element.way == 1)] .withdraw) { showToast('支付宝提现暂时维护中'); return; } if (MyCookie().userBean.aliPayAccount.isEmpty) { showToast('未绑定支付宝'); } else { setState(() { withdrawalPath = 1; }); } }, behavior: HitTestBehavior.translucent, child: Container( margin: EdgeInsets.fromLTRB(17, 24, 17, 0), child: Row( children: [ Container( decoration: BoxDecoration( color: withdrawalPath == 1 ? MyColors.cFF4233 : MyColors.cD0D0D0, borderRadius: BorderRadius.circular(9)), height: 18, width: 18, child: Icon( Icons.check, color: Colors.white, size: 14, ), alignment: Alignment.center, margin: EdgeInsets.only(right: 10), ), MyViews().myText( '提现到支付宝:${MyCookie().userBean.aliPayAccount.isEmpty ? '未绑定支付宝' : MyCookie().userBean.aliPayAccount}', MyColors.c666666, 15), ], ), ), ), if (showWeChat) GestureDetector( onTap: () { if (!payWays[payWays .indexWhere((element) => element.way == 2)] .withdraw) { showToast('微信提现暂时维护中'); return; } if (MyCookie().userBean.wxOpenid.isEmpty) { showToast('未绑定微信'); } else { setState(() { withdrawalPath = 2; }); } }, behavior: HitTestBehavior.translucent, child: Container( margin: EdgeInsets.fromLTRB(17, 24, 17, 0), child: Row( children: [ Container( decoration: BoxDecoration( color: withdrawalPath == 2 ? MyColors.cFF4233 : MyColors.cD0D0D0, borderRadius: BorderRadius.circular(9)), height: 18, width: 18, child: Icon( Icons.check, color: Colors.white, size: 14, ), alignment: Alignment.center, margin: EdgeInsets.only(right: 10), ), MyViews().myText('提现到微信:', MyColors.c666666, 15), MyCookie().userBean.wxOpenid.isEmpty ? MyViews() .myText('未绑定微信', MyColors.c666666, 15) : Row( children: [ Container( margin: EdgeInsets.only(right: 10), child: ClipRRect( borderRadius: BorderRadius.circular(12), child: MyViews().netImg( MyCookie().userBean.wxPicture, 24, 24), ), ), MyViews().myText( MyCookie().userBean.wxName, MyColors.c666666, 15) ], ) ], ), ), ), Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(4), ), margin: EdgeInsets.symmetric(horizontal: 17, vertical: 14), padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15), child: Column( children: [ MyViews().myText('提现金额', MyColors.c333333, 16), Container( margin: EdgeInsets.only(top: 15), child: Row( children: [ Text( '¥', style: TextStyle( color: MyColors.c333333, fontSize: 30), ), Expanded( child: TextField( inputFormatters: [ FilteringTextInputFormatter.allow( RegExp("[0-9.]")), MoneyTextInputFormatter() ], controller: _controller, cursorColor: MyColors.cFF4233, cursorWidth: 1.0, decoration: InputDecoration( border: InputBorder.none, disabledBorder: InputBorder.none, enabledBorder: InputBorder.none, focusedBorder: InputBorder.none, hintText: '请输入金额', hintStyle: TextStyle( color: MyColors.c999999, fontSize: 16), isDense: true, contentPadding: const EdgeInsets.fromLTRB( 14, 4.5, 8, 4.5)), maxLines: 1, style: TextStyle( color: MyColors.c333333, fontSize: 30, fontWeight: FontWeight.bold, height: 1.3, letterSpacing: 0.2), keyboardType: TextInputType.number, onChanged: (t) { try { showH(); double amount = double.parse(t); if (amount > 0) { withdrawalStatus = 0; setState(() {}); } } catch (e) { withdrawalStatus = 1; } setState(() {}); }, ), ) ], ), ), Container( margin: EdgeInsets.only(top: 4, bottom: 15), color: MyColors.cE7E7E7, height: 0.5, ), RichText( text: TextSpan( text: '当前零钱余额', style: TextStyle( color: MyColors.c999999, fontSize: 15), children: [ TextSpan( text: '${balanceDisplay(balance).toStringAsFixed(2)}', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), ), TextSpan( text: '元,', style: TextStyle( color: MyColors.c999999, fontSize: 15), ), TextSpan( text: '全部提现', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), recognizer: TapGestureRecognizer() ..onTap = () { _controller.text = balance.toStringAsFixed(2); showH(); setState(() { withdrawalStatus = 0; }); }), ]), ), if (showHandlingFee) RichText( text: TextSpan( text: '手续费', style: TextStyle( color: MyColors.c999999, fontSize: 15), children: [ TextSpan( text: '${handlingFee.toStringAsFixed(2)}', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), ), ]), ), if (showHandlingFee) RichText( text: TextSpan( text: '实际到账', style: TextStyle( color: MyColors.c999999, fontSize: 15), children: [ TextSpan( text: '${(double.parse(_controller.text.toString()) - handlingFee).toStringAsFixed(2)}', style: TextStyle( color: MyColors.cFF4233, fontSize: 15), ), ]), ), GestureDetector( onTap: () { MyTools().hideKeyboard(context); if (withdrawalPath == -1) { showToast('请选择提现方式'); return; } if (MyCookie().userBean.forbidWithdraw) { showSimpleDialog('你已经被冻结提现,请联系群主或客服。', context, () { Navigator.pop(context); }); return; } if (withdrawalStatus == 0) { enterTheWithdrawalPassword(); } else {} }, behavior: HitTestBehavior.translucent, child: Container( decoration: BoxDecoration( color: withdrawalStatus == 0 ? MyColors.cFF4233 : MyColors.cEFEFEF, borderRadius: BorderRadius.circular(20)), child: MyViews().myText('提现', Colors.white, 14), alignment: Alignment.center, height: 40, margin: EdgeInsets.only(top: 20, bottom: 15), ), ), Container( margin: EdgeInsets.only(bottom: 8), child: RichText( text: TextSpan( text: '您的提现手续费为:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '${NumUtil.multiply(percentFee, 100)}%${extraFee == 0 ? '' : '+$extraFee'}', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), // TextSpan( // text: '元', // style: TextStyle( // color: MyColors.c999999, // fontSize: 14), // ), ]), ), ), Visibility( visible: maxPerHand != 0, child: Container( margin: EdgeInsets.only(bottom: 8), child: RichText( text: TextSpan( text: '单笔最高限额:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '$maxPerHand', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), TextSpan( text: '元', style: TextStyle( color: MyColors.c999999, fontSize: 14), ), ]), ), ), ), Visibility( visible: minPerHand != 0, child: Container( margin: EdgeInsets.only(bottom: 8), child: RichText( text: TextSpan( text: '单笔最低限额:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '$minPerHand', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), TextSpan( text: '元', style: TextStyle( color: MyColors.c999999, fontSize: 14), ), ]), ), ), ), Visibility( visible: threshold != 0, child: RichText( text: TextSpan( text: '当日免手续费提现额度:', style: TextStyle( color: MyColors.c999999, fontSize: 14), children: [ TextSpan( text: '$threshold', style: TextStyle( color: MyColors.cE2A62D, fontSize: 14), ), TextSpan( text: '元', style: TextStyle( color: MyColors.c999999, fontSize: 14), ), ]), ), ), ], crossAxisAlignment: CrossAxisAlignment.start, ), ) ], ), ), ), ], ), ), ); } void withdraw() { EasyLoading.show(); try { double amount = double.parse(_controller.text.toString()); if (amount <= balance && amount >= minPerHand) { var j = { 'user_uid': MyCookie().getUID(), 'way': withdrawalPath, 'amount': amount, 'password': MyTools.base64Encode(withdrawPassword) }; if (withdrawalPath == 2) { j['wx_openid'] = MyCookie().userBean.wxOpenid; } print(j.toString()); MyDio().post(MyApis.getApi('withdraw'), j, (response, hasError) { if (!hasError) { showToast('提现成功'); withdrawalStatus = 2; queryPersonalInformation(); } else { setState(() { withdrawalStatus = 0; }); } }, (error) { setState(() { withdrawalStatus = 0; }); }); } else if (amount > balance) { setState(() { withdrawalStatus = 0; }); EasyLoading.dismiss(); showSimpleDialog('您的余额不足。', context, () { Navigator.pop(context); }); } else { setState(() { withdrawalStatus = 0; }); EasyLoading.dismiss(); showSimpleDialog('提现失败:提现金额不能低于$minPerHand元。', context, () { Navigator.pop(context); }); } } catch (e) { EasyLoading.dismiss(); setState(() { withdrawalStatus = 0; EasyLoading.dismiss(); }); EasyLoading.dismiss(); showSimpleDialog('输入内容有误,请重新输入', context, () { Navigator.pop(context); }); } } void queryPersonalInformation() { MyDio().query({ "key": "user", "filters": { "conditions": ["uid == ${MyCookie().getUID()}"] }, "dims": userDims, "paging": [1, 20] }, (response, hasError) { if (!hasError) { UserBeanEntity entity = UserBeanEntity.fromJson(json.decode(response.data.toString())); MyCookie().userBean = entity.data.data[0]; balance = balanceDisplay(MyCookie().balance); EventBus().emit('userChange'); setState(() {}); if (withdrawalStatus == 2) { Navigator.pop(context); } } }, (error) {}); } void queryWithdrawalInstructions() { MyDio().query({ "key": "withdraw_pricing", "filters": {}, "dims": withdrawPricingDims, "paging": [1, 20] }, (response, hasError) { if (!hasError) { WithdrawPricingBeanEntity entity = WithdrawPricingBeanEntity.fromJson( json.decode(response.data.toString())); if (entity.data.data.isNotEmpty) { percentFee = entity.data.data[0].percentFee; extraFee = entity.data.data[0].extraFee; threshold = entity.data.data[0].threshold; maxPerHand = entity.data.data[0].maxPerHand; minPerHand = entity.data.data[0].minPerHand; } } setState(() {}); }, (error) {}); } showH() { try { double amount = double.parse(_controller.text.toString()); if (amount >= minPerHand) { handlingFee = double.parse((double.parse(_controller.text.toString()) * percentFee * 100) .toStringAsFixed(0)) / 100 + extraFee; setState(() { showHandlingFee = true; }); } else { setState(() { showHandlingFee = false; }); } } catch (e) { setState(() { showHandlingFee = false; }); } } void enterTheWithdrawalPassword() { StateSetter ss; showDialog( context: context, builder: (BuildContext context) { return Material( color: Colors.black12, child: Center( child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(16), color: Colors.white, ), height: 300, margin: EdgeInsets.symmetric(horizontal: 18), padding: EdgeInsets.symmetric(horizontal: 8, vertical: 12), child: Column( children: [ Expanded( child: Column( children: [ Stack( children: [ Positioned( child: Container( child: MyViews() .myText('提现密码', MyColors.c333333, 14), margin: EdgeInsets.symmetric(horizontal: 20), width: double.infinity, alignment: Alignment.center, height: 30, ), ), Positioned( right: 0, child: GestureDetector( onTap: () { _pinPutController.clear(); MyTools().hideKeyboard(context); Navigator.pop(context); }, behavior: HitTestBehavior.translucent, child: Icon( Icons.cancel, color: MyColors.cB6B6B6, ), ), ) ], ), Container( height: 0.5, width: double.infinity, color: MyColors.cE7E7E7, margin: EdgeInsets.only( left: 8, right: 8, top: 8, bottom: 20), ), Text( '提现金额', style: TextStyle(color: MyColors.c333333, fontSize: 13), ), Text( '¥${(double.parse(_controller.text.toString()) - handlingFee).toStringAsFixed(2)}', style: TextStyle(color: MyColors.c333333, fontSize: 32), ), // Container( // margin: EdgeInsets.only(top: 10), // child: PasswordInputTextField( // decoration: BoxLooseDecoration( // strokeColor: MyColors.cEDEDED, // solidColor:MyColors.cEDEDED , // radius: Radius.circular(2), // obscureStyle: // ObscureStyle(isTextObscure: true,obscureText: '*'),), // passwordLength: 6, // keyboardType: TextInputType.number, // autoFocus: true, // inputFormatter: [ // FilteringTextInputFormatter.allow(RegExp(r'[0-9]' // r'')) // ], // textInputAction: TextInputAction.done, // onChanged: (password) { // withdrawPassword = password; // }, // ), // ), Container( margin: EdgeInsets.fromLTRB(20, 10, 20, 10), child: PinPut( fieldsCount: 6, eachFieldConstraints: BoxConstraints(minHeight: 34.0, minWidth: 34.0), focusNode: _pinPutFocusNode, autofocus: true, controller: _pinPutController, submittedFieldDecoration: _pinPutDecoration.copyWith( borderRadius: BorderRadius.circular(2.0), ), onChanged: (t) { withdrawPassword = t; if (t.length == 6) { MyTools().hideKeyboard(context); } }, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'[0-9]' r'')) ], obscureText: '*', selectedFieldDecoration: _pinPutDecoration, followingFieldDecoration: _pinPutDecoration.copyWith( borderRadius: BorderRadius.circular(2.0), border: Border.all( color: MyColors.cEDEDED, ), ), ), ), Container( child: Row( children: [ Text( '提现路径', style: TextStyle( color: MyColors.c666666, fontSize: 12), ), GestureDetector( onTap: () { showCupertinoModalPopup( context: context, builder: (BuildContext context) { return CupertinoActionSheet( title: MyViews().myText( '提现路径', MyColors.c333333, 20), actions: [ if (MyCookie() .userBean .aliPayAccount .isNotEmpty) CupertinoActionSheetAction( child: Row( children: [ SvgPicture.asset( 'images/svg/支付宝.svg', width: 24, height: 24, ), Container( child: MyViews().myText( '支付宝提现', MyColors.c333333, 16), margin: EdgeInsets.only( left: 6, right: 6), ), ], mainAxisAlignment: MainAxisAlignment.center, ), onPressed: (() { ss(() { withdrawalPath = 1; Navigator.pop(context); }); }), ), if (MyCookie() .userBean .wxOpenid .isNotEmpty) CupertinoActionSheetAction( child: Row( children: [ SvgPicture.asset( 'images/svg/微信.svg', width: 24, height: 24, ), Container( child: MyViews().myText( '微信提现', MyColors.c333333, 16), margin: EdgeInsets.only( left: 6, right: 6), ), ], mainAxisAlignment: MainAxisAlignment.center, ), onPressed: (() { ss(() { withdrawalPath = 2; Navigator.pop(context); }); }), ), ], cancelButton: CupertinoActionSheetAction( child: Text("取消"), isDefaultAction: true, onPressed: (() { Navigator.pop(context, 'Cancel'); }), ), ); }); }, behavior: HitTestBehavior.translucent, child: StatefulBuilder( builder: (BuildContext context, void Function(void Function()) setState) { ss = setState; return Row( children: [ SvgPicture.asset( withdrawalPath == 1 ? 'images/svg/支付宝.svg' : 'images/svg/微信.svg', height: 18, width: 18, ), Container( child: Text( withdrawalPath == 1 ? '支付宝' : '微信', style: TextStyle( color: MyColors.c333333, fontSize: 14), ), margin: EdgeInsets.symmetric( horizontal: 8), ), Icon( Icons.arrow_forward_ios, size: 11, color: MyColors.cB6B6B6, ) ], ); }, ), ) ], mainAxisAlignment: MainAxisAlignment.spaceBetween, ), margin: EdgeInsets.symmetric(horizontal: 20), ) ], ), ), Container( height: 0.5, color: MyColors.cE7E7E7, margin: EdgeInsets.only(bottom: 12), ), Container( margin: EdgeInsets.symmetric(horizontal: 10), child: GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { if (withdrawPassword.length != 6) { return; } _pinPutController.clear(); MyTools().hideKeyboard(context); Navigator.pop(context); withdrawalStatus = 1; withdraw(); }, child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), color: MyColors.cFF4233), height: 40, width: 150, child: MyViews().myText('提现', Colors.white, 14), alignment: Alignment.center, ), ), ) ], mainAxisAlignment: MainAxisAlignment.spaceBetween, ), ), ), ); }, ); } final TextEditingController _pinPutController = TextEditingController(); final FocusNode _pinPutFocusNode = FocusNode(); BoxDecoration get _pinPutDecoration { return BoxDecoration( border: Border.all(color: MyColors.cEDEDED), color: MyColors.cEDEDED, borderRadius: BorderRadius.circular(2.0), ); } } class MoneyTextInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { // TODO: implement formatEditUpdate String newvalueText = newValue.text; if (newvalueText == ".") { //第一个数为. newvalueText = "0."; } else if (newvalueText.contains(".")) { if (newvalueText.lastIndexOf(".") != newvalueText.indexOf(".")) { //输入了2个小数点 newvalueText = newvalueText.substring(0, newvalueText.lastIndexOf('.')); } else if (newvalueText.length - 1 - newvalueText.indexOf(".") > 2) { //输入了1个小数点 小数点后两位 newvalueText = newvalueText.substring(0, newvalueText.indexOf(".") + 3); } } return TextEditingValue( text: newvalueText, selection: new TextSelection.collapsed(offset: newvalueText.length), ); } }