red_packet_page.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import 'dart:convert';
  2. import 'package:bbyyy/beans/my_shop_bean_entity.dart';
  3. import 'package:bbyyy/beans/send_message_bean_entity.dart';
  4. import 'package:bbyyy/beans/user_balance_entity.dart';
  5. import 'package:bbyyy/beans/user_bean_entity.dart';
  6. import 'package:bbyyy/https/MyDio.dart';
  7. import 'package:bbyyy/https/my_request.dart';
  8. import 'package:bbyyy/https/url.dart';
  9. import 'package:bbyyy/msgDB/my_msg_db.dart';
  10. import 'package:bbyyy/my_tools/const.dart';
  11. import 'package:bbyyy/my_tools/dims.dart';
  12. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  13. import 'package:bbyyy/my_tools/event_bus.dart';
  14. import 'package:bbyyy/my_tools/my_apis.dart';
  15. import 'package:bbyyy/my_tools/my_colors.dart';
  16. import 'package:bbyyy/my_tools/my_cookie.dart';
  17. import 'package:bbyyy/my_tools/my_tools.dart';
  18. import 'package:bbyyy/my_tools/my_views.dart';
  19. import 'package:bbyyy/pay/pay_tools.dart';
  20. import 'package:flutter/material.dart';
  21. import 'package:flutter/services.dart';
  22. class RedPacketPage extends StatefulWidget {
  23. MyShopBeanDataData chatWith;
  24. RedPacketPage(this.chatWith);
  25. @override
  26. _RedPacketPageState createState() => _RedPacketPageState();
  27. }
  28. class _RedPacketPageState extends State<RedPacketPage> {
  29. TextEditingController _controller = TextEditingController();
  30. @override
  31. void initState() {
  32. // TODO: implement initState
  33. super.initState();
  34. Future.delayed(Duration.zero,(){
  35. queryPersonalInformation();
  36. });
  37. Future.delayed(Duration.zero,(){
  38. EasyLoading.show();
  39. getUserWalletBalance((re, hE) {
  40. if (!hE) {
  41. UserBalanceEntity balance =
  42. UserBalanceEntity().fromJson(json.decode(re.data.toString()));
  43. MyCookie().balance = balance.data;
  44. setState(() {
  45. EasyLoading.dismiss();
  46. });
  47. }
  48. }, (e) {
  49. EasyLoading.dismiss();
  50. }, context);
  51. });
  52. }
  53. void queryPersonalInformation() {
  54. MyDio().query({
  55. "key": "user",
  56. "filters": {
  57. "conditions": ["uid == ${MyCookie().getUID()}"]
  58. },
  59. "dims": userDims,
  60. "paging": [1, 20]
  61. }, (response, hasError) {
  62. if (!hasError) {
  63. UserBeanEntity entity =
  64. UserBeanEntity().fromJson(json.decode(response.data.toString()));
  65. MyCookie().userBean = entity.data.data[0];
  66. EventBus().emit('userChange');
  67. setState(() {});
  68. }
  69. }, (error) {});
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return Scaffold(
  74. body: Column(
  75. children: [
  76. MyViews().myAppBar('发送红包', context, []),
  77. Expanded(
  78. child: Column(
  79. children: [
  80. Container(
  81. margin: EdgeInsets.fromLTRB(17, 24, 17, 0),
  82. child: Row(
  83. children: [
  84. ClipRRect(
  85. child: MyViews()
  86. .netImg(imgURL(widget.chatWith.userPic), 24, 24),
  87. borderRadius: BorderRadius.circular(12),
  88. ),
  89. Container(
  90. child: MyViews().myText(
  91. widget.chatWith.userName, MyColors.c666666, 15),
  92. margin: EdgeInsets.only(left: 10),
  93. ),
  94. ],
  95. ),
  96. ),
  97. Container(
  98. decoration: BoxDecoration(
  99. color: Colors.white,
  100. borderRadius: BorderRadius.circular(4)),
  101. margin: EdgeInsets.symmetric(horizontal: 17, vertical: 14),
  102. padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
  103. child: Column(
  104. children: [
  105. MyViews().myText('红包金额', MyColors.c333333, 16),
  106. Container(
  107. margin: EdgeInsets.only(top: 15),
  108. child: Row(
  109. children: [
  110. Text(
  111. '¥',
  112. style: TextStyle(
  113. color: MyColors.c333333, fontSize: 30),
  114. ),
  115. Expanded(
  116. child: TextField(
  117. inputFormatters: [
  118. FilteringTextInputFormatter.allow(
  119. RegExp("[0-9.]")),
  120. ],
  121. controller: _controller,
  122. cursorColor: MyColors.cFF4233,
  123. cursorWidth: 1.0,
  124. decoration: InputDecoration(
  125. border: InputBorder.none,
  126. disabledBorder: InputBorder.none,
  127. enabledBorder: InputBorder.none,
  128. focusedBorder: InputBorder.none,
  129. hintText: '请输入金额',
  130. hintStyle: TextStyle(
  131. color: MyColors.c999999, fontSize: 16),
  132. isDense: true,
  133. contentPadding: const EdgeInsets.fromLTRB(
  134. 14, 4.5, 8, 4.5)),
  135. maxLines: 1,
  136. style: TextStyle(
  137. color: MyColors.c333333,
  138. fontSize: 32,
  139. fontWeight: FontWeight.bold,
  140. height: 1.3,
  141. letterSpacing: 0.2),
  142. keyboardType: TextInputType.number,
  143. onChanged: (t) {
  144. setState(() {});
  145. },
  146. ),
  147. )
  148. ],
  149. ),
  150. ),
  151. ],
  152. crossAxisAlignment: CrossAxisAlignment.start,
  153. ),
  154. ),
  155. GestureDetector(
  156. onTap: () {
  157. sendRedEnvelope();
  158. },
  159. behavior: HitTestBehavior.translucent,
  160. child: Container(
  161. decoration: BoxDecoration(
  162. color: MyColors.cFF4233,
  163. borderRadius: BorderRadius.circular(20)),
  164. child: MyViews().myText('发送', Colors.white, 14),
  165. alignment: Alignment.center,
  166. height: 40,
  167. margin: EdgeInsets.only(
  168. top: 20, bottom: 15, left: 17, right: 17),
  169. ),
  170. ),
  171. ],
  172. ),
  173. ),
  174. ],
  175. ),
  176. );
  177. }
  178. double amount;
  179. void sendRedEnvelope() {
  180. try {
  181. amount = double.parse(_controller.text.toString());
  182. _controller.clear();
  183. if (amount > MyCookie().balance) {
  184. showToast('超过你拥有的积分');
  185. return;
  186. }
  187. if (amount <= 0) {
  188. showToast('请输入正确的金额');
  189. return;
  190. }
  191. MyDio().post(MyApis.getApi('redPacket'), {
  192. 'payer_uid': MyCookie().getUID(),
  193. 'payee_uid': widget.chatWith.userUid,
  194. 'amount': amount,
  195. }, (response, hasError) {
  196. if (!hasError) {
  197. showToast('发送成功');
  198. sendRedP();
  199. }
  200. }, (error) {});
  201. } catch (e) {
  202. showToast('请输入正确的金额');
  203. }
  204. }
  205. void sendRedP() {
  206. sendMsg(chatMsgTypeRedPackage, widget.chatWith.userUid, '$amount!@#${widget.chatWith.userName}',
  207. (re, hE) async {
  208. if (!hE) {
  209. SendMessageBeanEntity entity =
  210. SendMessageBeanEntity().fromJson(json.decode(re.data.toString()));
  211. MsgDB msgDB =
  212. MsgDB('table${MyCookie().getUID()}_${entity.data.receiverUid}');
  213. if (!msgDB.isTableExits) {
  214. await msgDB.open();
  215. }
  216. await msgDB.addTableData([entity.data]);
  217. EventBus().emit('hasNewMsg', entity.data);
  218. EventBus().emit('hasNewMsgInMsgPage', entity.data);
  219. Navigator.pop(context);
  220. }
  221. }, (e) {});
  222. }
  223. }