Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

278 rivejä
11 KiB

  1. import 'package:chat/chat/gift_page.dart';
  2. import 'package:chat/data/UserData.dart';
  3. import 'package:chat/data/constants.dart';
  4. import 'package:chat/data/gift_data_mgr.dart';
  5. import 'package:chat/generated/i18n.dart';
  6. import 'package:chat/models/gift_item_model.dart';
  7. import 'package:chat/models/gift_select_provider.dart';
  8. import 'package:chat/models/money_change.dart';
  9. import 'package:chat/proto/chat.pb.dart';
  10. import 'package:chat/r.dart';
  11. import 'package:chat/utils/ChargeMoney.dart';
  12. import 'package:chat/utils/HttpUtil.dart';
  13. import 'package:chat/utils/TokenMgr.dart';
  14. import 'package:chat/utils/anim_effect_overlay.dart';
  15. import 'package:chat/utils/msgHandler.dart';
  16. import 'package:chat/utils/screen.dart';
  17. import 'package:dio/dio.dart';
  18. import 'package:flutter/material.dart';
  19. import 'package:oktoast/oktoast.dart';
  20. import 'package:provider/provider.dart';
  21. class GiftSelectWidget extends StatefulWidget {
  22. final int friendId;
  23. final Function sendMsg;
  24. GiftSelectWidget(this.friendId, this.sendMsg);
  25. @override
  26. _GiftSelectWidgetState createState() => _GiftSelectWidgetState();
  27. }
  28. class _GiftSelectWidgetState extends State<GiftSelectWidget> {
  29. //打赏礼物信息
  30. List<GiftItemModel> giftList;
  31. //礼物数量
  32. int curGiftValue = 1;
  33. @override
  34. void initState() {
  35. super.initState();
  36. getGiftList();
  37. }
  38. getGiftList() async {
  39. giftList = await GiftMgr().getGiftList();
  40. if (mounted) {
  41. setState(() {});
  42. }
  43. }
  44. _sendGift(BuildContext context) async {
  45. int curSelectIndex =
  46. Provider.of<GiftSelectProvider>(context).curSelectIndex;
  47. var curGift = giftList[curSelectIndex];
  48. int price = curGift.price;
  49. int giftId = curGift.id;
  50. int total = curGiftValue * price;
  51. if (total > Provider.of<MoneyChangeProvider>(context).money) {
  52. showToast(I18n.of(context).not_enough);
  53. return;
  54. }
  55. int buyCount = 0;
  56. if (curGiftValue > curGift.amount) {
  57. buyCount = curGiftValue - curGift.amount;
  58. }
  59. Map data = {
  60. "userId": UserData().basicInfo.userId,
  61. "rUserId": widget.friendId,
  62. "id": giftId,
  63. "price": price,
  64. "amount": curGiftValue,
  65. "totalPrice": total,
  66. "payNum": buyCount
  67. };
  68. data['sign'] = TokenMgr().getSign(data);
  69. Response res = await HttpUtil().post('reward/gift', data: data);
  70. if (res == null) {
  71. return;
  72. }
  73. Map resData = res.data;
  74. //showToast(resData['msg']);
  75. if (resData['code'] == 0) {
  76. //赠送成功
  77. print('赠送金额:${resData['data']}');
  78. if (curGift.amount >= curGiftValue) {
  79. curGift.amount -= curGiftValue;
  80. }
  81. int receiveAmount = resData['data'];
  82. Provider.of<MoneyChangeProvider>(context, listen: false).subMoney(total);
  83. AnimEffect.showEffect(context, giftId);
  84. AnimEffect.showDashangEffect(context, true, curGiftValue, curGift.name);
  85. var msgContetnt = GiftChat.create();
  86. msgContetnt.giftAmount = curGiftValue;
  87. msgContetnt.suId = UserData().basicInfo.userId;
  88. msgContetnt.tuId = widget.friendId;
  89. msgContetnt.giftId = giftId;
  90. msgContetnt.giftName = curGift.name;
  91. msgContetnt.money = receiveAmount;
  92. var msg = MsgHandler.createSendMsg(
  93. ChatType.GiftChatType, msgContetnt.writeToBuffer(),
  94. friendId: widget.friendId, channelType: ChatChannelType.Session);
  95. widget.sendMsg(msg);
  96. setState(() {});
  97. } else {
  98. print(resData['msg']);
  99. }
  100. }
  101. @override
  102. Widget build(BuildContext context) {
  103. return Container(
  104. height: 320,
  105. width: Screen.width,
  106. decoration: BoxDecoration(
  107. color: Colors.white,
  108. ),
  109. child: giftList == null
  110. ? Center(child: Text(I18n.of(context).no_gift))
  111. : Column(
  112. children: <Widget>[
  113. Container(
  114. padding: EdgeInsets.symmetric(vertical: 5, horizontal: 20),
  115. child: Row(
  116. children: <Widget>[
  117. Text(I18n.of(context).sent_gift,
  118. textScaleFactor: 1.0,
  119. style: TextStyle(
  120. fontSize: 16,
  121. fontWeight: FontWeight.bold,
  122. color: Constants.BlackTextColor)),
  123. Expanded(
  124. child: SizedBox(),
  125. ),
  126. Image.asset(
  127. R.assetsImagesCoin,
  128. scale: 2,
  129. ),
  130. SizedBox(
  131. width: 5,
  132. ),
  133. fixedText(
  134. I18n.of(context)
  135. .available_balance
  136. .replaceFirst('/s1', ''),
  137. color: Constants.GreyTextColor),
  138. Consumer<MoneyChangeProvider>(
  139. builder:
  140. (context, MoneyChangeProvider counter, child) =>
  141. fixedText(counter.money.toString(),
  142. color: Color(0xFFDB305D)),
  143. ),
  144. fixedText(I18n.of(context).mask_coin,
  145. color: Color(0xFFDB305D)),
  146. ],
  147. ),
  148. ),
  149. Container(
  150. alignment: Alignment.topCenter,
  151. padding: EdgeInsets.all(10),
  152. height: 150,
  153. child: GiftPage(giftList, 0),
  154. ),
  155. Expanded(child: SizedBox()),
  156. Padding(
  157. padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
  158. child: Row(
  159. children: <Widget>[
  160. Expanded(
  161. child: SizedBox(
  162. height: 36,
  163. child: RaisedButton(
  164. padding: EdgeInsets.symmetric(horizontal: 5),
  165. child: Text(
  166. I18n.of(context).recharge,
  167. textScaleFactor: 1.0,
  168. maxLines: 1,
  169. style: TextStyle(
  170. fontSize: 16, fontWeight: FontWeight.bold),
  171. ),
  172. elevation: 2.0,
  173. color: Colors.orangeAccent,
  174. textColor: Colors.white,
  175. onPressed: () {
  176. ChargeMoney.showChargeSheet(context, () {});
  177. },
  178. shape: RoundedRectangleBorder(
  179. borderRadius:
  180. BorderRadius.all(Radius.circular(20)),
  181. ),
  182. ),
  183. )),
  184. SizedBox(width: 20),
  185. Expanded(
  186. flex: 2,
  187. child: Container(
  188. height: 36,
  189. decoration: BoxDecoration(
  190. boxShadow: [
  191. BoxShadow(
  192. blurRadius: 2.0,
  193. color: Colors.white24)
  194. ],
  195. borderRadius:
  196. BorderRadius.all(Radius.circular(20)),
  197. border: Border.all(color: Colors.red)),
  198. child: Row(
  199. children: <Widget>[
  200. Expanded(
  201. child: DropdownButton(
  202. isExpanded: true,
  203. underline: Container(),
  204. value: curGiftValue,
  205. style: TextStyle(fontSize: 16),
  206. iconEnabledColor: Colors.redAccent,
  207. onChanged: (newValue) {
  208. setState(() {
  209. curGiftValue = newValue;
  210. });
  211. },
  212. items: <int>[1, 6, 8, 66]
  213. .map<DropdownMenuItem>((v) =>
  214. DropdownMenuItem(
  215. value: v,
  216. child: Container(
  217. padding: EdgeInsets.only(
  218. left: 20),
  219. child: fixedText(
  220. v.toString(),
  221. color: v ==
  222. curGiftValue
  223. ? Colors.redAccent
  224. : Colors.black))))
  225. .toList(),
  226. )),
  227. Expanded(
  228. child: InkWell(
  229. child: Container(
  230. decoration: BoxDecoration(
  231. color: Color(0xFFFD6E8F),
  232. borderRadius: BorderRadius.only(
  233. topRight: Radius.circular(20),
  234. bottomRight: Radius.circular(20)),
  235. ),
  236. height: 36,
  237. alignment: Alignment.center,
  238. child: Text(
  239. I18n.of(context).give,
  240. textScaleFactor: 1.0,
  241. style: TextStyle(
  242. color: Colors.white,
  243. fontSize: 16,
  244. fontWeight: FontWeight.bold),
  245. ),
  246. ),
  247. onTap: () {
  248. _sendGift(context);
  249. },
  250. ))
  251. ],
  252. )))
  253. ],
  254. ),
  255. )
  256. ],
  257. ));
  258. }
  259. }