YSMoney.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import 'dart:async';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_alipay/flutter_alipay.dart';
  5. import 'package:fluwx/fluwx.dart';
  6. import 'package:ysairplane2/base/YSBase.dart';
  7. import 'package:ysairplane2/code/YSBill.dart';
  8. import 'package:ysairplane2/tools/YSNetWorking.dart';
  9. import 'package:ysairplane2/tools/YSTools.dart';
  10. import 'package:fluwx/fluwx.dart' as fluwx;
  11. class YSMoney extends StatefulWidget {
  12. @override
  13. _YSMoneyState createState() => _YSMoneyState();
  14. }
  15. class _YSMoneyState extends State<YSMoney> {
  16. List array = ['支付宝','微信'];
  17. int _selected = 0;
  18. bool _isSucceed = false;
  19. List _dataArray = [];
  20. String _psyStr = '支付宝';
  21. TextEditingController _amountField = TextEditingController();
  22. StreamSubscription _listener;
  23. @override
  24. void dispose() {
  25. _listener.cancel();
  26. super.dispose();
  27. }
  28. @override
  29. void initState() {
  30. _listener = fluwx.weChatResponseEventHandler.listen((res) {
  31. if (res is fluwx.WeChatPaymentResponse) {
  32. print('---------------->${res.errCode}');
  33. if(res.isSuccessful==true){
  34. }
  35. }
  36. });
  37. Future.delayed(Duration(seconds: 0)).then((value){
  38. _getBalancePackData();
  39. });
  40. super.initState();
  41. }
  42. String _balanceStr = '0.00';
  43. @override
  44. Widget build(BuildContext context) {
  45. return YSBase(
  46. ystitle: '钱包',
  47. ysright: GestureDetector(
  48. child: Text('账单',style: TextStyle(fontSize: zsp(34),color: Color(0xFF131318)),),
  49. onTap: (){
  50. Navigator.of(context).push(
  51. CupertinoPageRoute(
  52. builder: (context){
  53. return YSBill();
  54. }
  55. )
  56. );
  57. },
  58. ),
  59. yschild: Container(
  60. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44,
  61. width: MediaQuery.of(context).size.width,
  62. color: Color(0xFFF5F6F8),
  63. child: SingleChildScrollView(
  64. child: _isSucceed==true?Column(
  65. children: [
  66. Container(
  67. color: Colors.white,
  68. width: MediaQuery.of(context).size.width,
  69. padding: EdgeInsets.only(left: wsp(30),right: wsp(30),top: hsp(60),bottom: hsp(60)),
  70. child: Column(
  71. children: [
  72. Image(
  73. image: AssetImage('lib/images/succeed.png'),
  74. height: hsp(130),
  75. width: wsp(130)
  76. ),
  77. Container(
  78. child: Text('充值成功',style: TextStyle(fontSize: zsp(40),color: Color(0xFF333333)),),
  79. margin: EdgeInsets.only(top: hsp(40),bottom: hsp(33)),
  80. ),
  81. Text('本次充值可用金额1100元',style: TextStyle(fontSize: zsp(28),color: Color(0xFF878787)),),
  82. ],
  83. ),
  84. ),
  85. GestureDetector(
  86. onTap: (){
  87. setState(() {
  88. _isSucceed = false;
  89. });
  90. },
  91. child: Container(
  92. height: hsp(94),
  93. width: MediaQuery.of(context).size.width-wsp(24)*2,
  94. alignment: Alignment.center,
  95. margin: EdgeInsets.only(top: hsp(58)),
  96. decoration: BoxDecoration(
  97. color: Color(0xFF0079FF),
  98. borderRadius: BorderRadius.all(Radius.circular(5))
  99. ),
  100. child: Text('完成',style: TextStyle(color: Colors.white,fontSize: zsp(34)),),
  101. ),
  102. )
  103. ],
  104. ):Column(
  105. children: [
  106. Container(
  107. margin: EdgeInsets.only(left: wsp(24),right: wsp(24),bottom: hsp(24),top: hsp(18)),
  108. height: hsp(240),
  109. decoration: BoxDecoration(
  110. image: DecorationImage(
  111. image: AssetImage('lib/images/top.png'),
  112. fit: BoxFit.fill
  113. ),
  114. ),
  115. child: Stack(
  116. children: [
  117. Container(
  118. margin: EdgeInsets.only(left: wsp(46),top: hsp(65)),
  119. child: Row(
  120. children: [
  121. Text('可用余额(RMB) ',style: TextStyle(fontSize: zsp(24),color: Colors.white),),
  122. Image(image: AssetImage('lib/images/eyes2.png'),color: Colors.white,height: hsp(21),width: wsp(28),)
  123. ],
  124. ),
  125. ),
  126. Container(
  127. margin: EdgeInsets.only(left: wsp(48),top: hsp(120)),
  128. child: Text('$_balanceStr',style: TextStyle(fontSize: zsp(68),color: Colors.white,fontWeight: FontWeight.bold),),
  129. )
  130. ],
  131. ),
  132. ),
  133. Container(
  134. margin: EdgeInsets.only(left: wsp(24),right: wsp(24),bottom: hsp(14)),
  135. width: MediaQuery.of(context).size.width-wsp(48),
  136. decoration: BoxDecoration(
  137. color: Colors.white,
  138. borderRadius: BorderRadius.all(Radius.circular(5))
  139. ),
  140. child: Column(
  141. crossAxisAlignment: CrossAxisAlignment.start,
  142. children: [
  143. Container(
  144. padding: EdgeInsets.only(left: wsp(40),right: wsp(40),top: hsp(26),bottom: hsp(26)),
  145. decoration: BoxDecoration(
  146. border: Border(bottom: BorderSide(color: Color(0xFFF5F5F7),width: hsp (1)))
  147. ),
  148. child: Text('优惠活动',style: TextStyle(fontSize: zsp(30)),),
  149. ),
  150. Container(
  151. height: hsp(370),
  152. margin: EdgeInsets.only(left: wsp(34),right: wsp(34),top: hsp(40),bottom: hsp(40)),
  153. child: GridView.builder(
  154. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  155. crossAxisCount: 3,
  156. crossAxisSpacing: wsp(20),
  157. mainAxisSpacing: hsp(20),
  158. childAspectRatio: wsp(198)/hsp(110)
  159. ),
  160. itemBuilder: (context,index){
  161. return GestureDetector(
  162. onTap: (){
  163. setState(() {
  164. _selected = index;
  165. });
  166. },
  167. child: index==_dataArray.length?Container(
  168. height: hsp(110),
  169. decoration: BoxDecoration(
  170. color: Color(0xFFF5F7F7),
  171. border: Border.all(color: index==_selected?Color(0xFF0079FF):Colors.transparent,width: 0.5),
  172. borderRadius: BorderRadius.all(Radius.circular(5))
  173. ),
  174. child: Center(child: Text('其他金额',style: TextStyle(fontSize: zsp(26),color: Color(0xFF999999)),)),
  175. ):Container(
  176. padding: EdgeInsets.only(top: hsp(15),bottom: hsp(15)),
  177. height: hsp(110),
  178. decoration: BoxDecoration(
  179. color: Color(0xFFF5F7F7),
  180. border: Border.all(color: index==_selected?Color(0xFF0079FF):Colors.transparent,width: 0.5),
  181. borderRadius: BorderRadius.all(Radius.circular(5))
  182. ),
  183. child: Column(
  184. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  185. children: [
  186. Row(
  187. children: [
  188. Image(height: hsp(26),width: wsp(26),image: AssetImage('lib/images/money2.png')),
  189. Text(' ${_dataArray[index]['name']}',style: TextStyle(fontSize: zsp(30),fontWeight: FontWeight.bold,color: Color(0xFF181818)),)
  190. ],
  191. mainAxisSize: MainAxisSize.min,
  192. ),
  193. Text('${_dataArray[index]['des']}',style: TextStyle(fontSize: zsp(20),color: Color(0xFF999999)),)
  194. ],
  195. ),
  196. ),
  197. );
  198. },
  199. itemCount: _dataArray.length+1,
  200. physics: NeverScrollableScrollPhysics(),
  201. ),
  202. ),
  203. if(_selected==_dataArray.length)Container(
  204. width: MediaQuery.of(context).size.width,
  205. margin: EdgeInsets.only(left: wsp(34),right: wsp(34),bottom: hsp(22)),
  206. child: Column(
  207. crossAxisAlignment: CrossAxisAlignment.start,
  208. children: [
  209. Text('转入金额',style: TextStyle(fontSize: zsp(28),color: Color(0xFF333333)),),
  210. Container(
  211. height: hsp(100),
  212. decoration: BoxDecoration(
  213. border: Border(bottom: BorderSide(color: Color(0xFFEEEEEE),width: 0.5))
  214. ),
  215. child: CupertinoTextField(
  216. decoration: BoxDecoration(),
  217. prefix: Text('\n¥',style: TextStyle(fontSize: zsp(36),color: Color(0xFF333333)),),
  218. style: TextStyle(fontSize: zsp(60),color: Color(0xFF333333),fontWeight: FontWeight.bold),
  219. keyboardType: TextInputType.number,
  220. suffix: Image(image: AssetImage('lib/images/off2.png'),height: 20,width: 20,),
  221. controller: _amountField,
  222. ),
  223. )
  224. ],
  225. ),
  226. ),
  227. // Container(
  228. // margin: EdgeInsets.only(left: wsp(34),right: wsp(34),bottom: hsp(49)),
  229. // child: Row(
  230. // children: [
  231. // Text('充值即代表同意',style: TextStyle(fontSize: zsp(22),color: Color(0xFF999999)),),
  232. // Text('《某某充值协议》',style: TextStyle(fontSize: zsp(22),color: Color(0xFF0079FF),fontWeight: FontWeight.bold),)
  233. // ],
  234. // ),
  235. // )
  236. ],
  237. ),
  238. ),
  239. Container(
  240. height: hsp(92),
  241. width: MediaQuery.of(context).size.width-wsp(48),
  242. decoration: BoxDecoration(
  243. color: Colors.white,
  244. borderRadius: BorderRadius.all(Radius.circular(5))
  245. ),
  246. padding: EdgeInsets.only(left: wsp(30),right: wsp(30)),
  247. child: Row(
  248. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  249. children: [
  250. Text('支付方式',style: TextStyle(fontSize: zsp(32),color: Color(0xFF000000)),),
  251. DropdownButton(
  252. value: _psyStr,
  253. items: [
  254. for(int i=0;i<array.length;i++)DropdownMenuItem(child: Text('${array[i]}'),value: '${array[i]}',),
  255. ],
  256. onChanged: (value){
  257. setState(() {
  258. _psyStr = value;
  259. });
  260. },
  261. underline: Container(),
  262. icon: Icon(Icons.keyboard_arrow_down,size: zsp(30),color: Color(0xFF999999),),
  263. style: TextStyle(fontSize: zsp(30),color: Color(0xFF999999)),
  264. )
  265. ],
  266. ),
  267. ),
  268. GestureDetector(
  269. onTap: (){
  270. _postBalanceOrderData();
  271. },
  272. child: Container(
  273. height: hsp(90),
  274. width: MediaQuery.of(context).size.width-wsp(24)*2,
  275. alignment: Alignment.center,
  276. margin: EdgeInsets.only(top: hsp(65)),
  277. decoration: BoxDecoration(
  278. color: Color(0xFF0079FF),
  279. borderRadius: BorderRadius.all(Radius.circular(5))
  280. ),
  281. child: Text('充值',style: TextStyle(color: Colors.white,fontSize: zsp(34)),),
  282. ),
  283. )
  284. ],
  285. ),
  286. ),
  287. ),
  288. );
  289. }
  290. _getBalancePackData() async{
  291. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/balance/getBalancePack',parameter: {},isLoading: true,
  292. isToken: true,refresh: (){_getBalancePackData();});
  293. if(dict!=null){
  294. setState(() {
  295. _balanceStr = '${dict['data']['blance']}';
  296. _dataArray = dict['data']['balancePack'];
  297. });
  298. }
  299. }
  300. _postBalanceOrderData() async{
  301. Map request = {};
  302. request['way'] = _psyStr=='支付宝'?3:2;
  303. request['type'] = 1;
  304. if(_selected==_dataArray.length){
  305. if(_amountField.text.isEmpty){
  306. ysFlutterToast(context, '请输入充值金额 ');
  307. return;
  308. }
  309. request['price'] = _amountField.text;
  310. }else{
  311. request['id'] = _dataArray[_selected]['id'];
  312. }
  313. Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/applets/createPayOrder',
  314. parameter: request,isLoading: true,
  315. isToken: true,refresh: (){_getBalancePackData();});
  316. if(dict!=null){
  317. if(_psyStr=='支付宝'){
  318. var payResult = await FlutterAlipay.pay('${dict['data']}');
  319. String payResultStatus = payResult.resultStatus;
  320. if (payResultStatus == "9000") {
  321. print('=================================订单支付成功');
  322. } else if (payResultStatus == "6001") {
  323. print('=================================支付取消');
  324. } else if (payResultStatus == "4000") {
  325. print('=================================支付失败');
  326. } else if (payResultStatus == "8000") {
  327. print('=================================等待支付');
  328. } else if (payResultStatus == "6002") {
  329. print('=================================无网络');
  330. } else if (payResultStatus == "5000") {
  331. print('=================================重复支付');
  332. } else {
  333. print('=================================支付失败,未知错误');
  334. }
  335. }else{
  336. Map result = dict['data'];
  337. fluwx.payWithWeChat(
  338. appId: result['appId'].toString(),
  339. partnerId: result['partnerId'].toString(),
  340. prepayId: result['prepayId'].toString(),
  341. packageValue: result['package'].toString(),
  342. nonceStr: result['nonceStr'].toString(),
  343. timeStamp: int.parse('${result['timeStamp']}'),
  344. sign: result['paySign'].toString(),
  345. // appId: "wx7e62294a5bf6e206",
  346. // partnerId: "1609486954",
  347. // prepayId: "wx08190939522528a0fac5616807982f0000",
  348. // packageValue: "Sign=WXPay",
  349. // nonceStr: "01NYog3p0Ptz8JMW",
  350. // timeStamp: int.parse('1623150580'),
  351. // sign: "565D7E49DBC2BA60F63715D16F2FEA49",
  352. ).then((data) {});
  353. }
  354. }
  355. }
  356. }