red_packet_page.dart 7.6 KB

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