red_packet_page.dart 7.7 KB

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