scan_add_new_friend_dialog.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import 'dart:async';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:sport/bean/user_info.dart';
  6. import 'package:sport/services/api/inject_api.dart';
  7. import 'package:sport/utils/toast.dart';
  8. import '../button_cancel.dart';
  9. import '../button_primary.dart';
  10. import '../space.dart';
  11. /// 业务组件 只针对 扫码加好友情况下的弹窗...
  12. class ScanAddFriendCustomAlertDialog extends StatefulWidget {
  13. final Function ok;
  14. final bool addClose;
  15. final UserInfo userInfo;
  16. final String newFriendCode;
  17. final bool isWaitAdd;
  18. final Function goNext;
  19. ScanAddFriendCustomAlertDialog(
  20. {this.userInfo,
  21. this.ok,
  22. this.addClose = false,
  23. this.newFriendCode,
  24. this.isWaitAdd,
  25. this.goNext});
  26. @override
  27. State<StatefulWidget> createState() {
  28. // TODO: implement createState
  29. return _ScanAddFriendCustomAlertDialog();
  30. }
  31. }
  32. class _ScanAddFriendCustomAlertDialog
  33. extends State<ScanAddFriendCustomAlertDialog> with InjectApi {
  34. String okText = "申请中";
  35. int dialogIndex = 0;
  36. List<Function> dialogs;
  37. Timer timerText;
  38. Timer timerIsFriend;
  39. @override
  40. initState() {
  41. super.initState();
  42. }
  43. initOkText() {
  44. int cout = 0;
  45. List<String> okTexts = ["申请中", "申请中.", "申请中..", "申请中..."];
  46. timerText = Timer.periodic(const Duration(seconds: 1), (timer) {
  47. //到时回调
  48. setState(() {
  49. okText = okTexts[cout % 3];
  50. });
  51. cout += 1;
  52. if (cout >= 30) {
  53. timer.cancel();
  54. timer = null;
  55. }
  56. });
  57. }
  58. @override
  59. void dispose() {
  60. // TODO: implement dispose
  61. super.dispose();
  62. timerText?.cancel();
  63. // timer = null;
  64. timerIsFriend?.cancel();
  65. }
  66. void pollGetIsFriend(int uid) {
  67. timerIsFriend = Timer.periodic(const Duration(seconds: 2), (timer) async {
  68. //到时回调
  69. var data = (await api.postIsFriend(uid)).data;
  70. if (data) {
  71. ToastUtil.show("添加成功");
  72. timer.cancel();
  73. timer = null;
  74. Navigator.of(context).pop(false);
  75. }
  76. });
  77. }
  78. @override
  79. Widget build(BuildContext context) {
  80. var _width = MediaQuery.of(context).size.width * 0.86;
  81. Widget userInfoContent(bool isShowUserInfo) {
  82. return Column(
  83. children: <Widget>[
  84. CircleAvatar(
  85. backgroundImage: CachedNetworkImageProvider(
  86. widget.userInfo?.avatar,
  87. ),
  88. radius: 35),
  89. Space(
  90. height: 8.0,
  91. ),
  92. Center(
  93. child: Text(
  94. "${widget.userInfo.name}",
  95. style: Theme.of(context).textTheme.headline3,
  96. ),
  97. ),
  98. Space(
  99. height: 8.0,
  100. ),
  101. isShowUserInfo
  102. ? Row(
  103. mainAxisAlignment: MainAxisAlignment.center,
  104. children: <Widget>[
  105. Row(
  106. children: <Widget>[
  107. Image.asset(
  108. "lib/assets/img/mine_icon_${widget.userInfo.gender == 1 ? "manl" : "girl"}_gray.png"),
  109. Space(
  110. width: 4,
  111. ),
  112. Text(
  113. widget.userInfo.gender == 1 ? "男" : "女",
  114. style:
  115. TextStyle(color: Color(0xff999999), fontSize: 12),
  116. ),
  117. ],
  118. ),
  119. if ((widget.userInfo.age ?? 0) != 0)
  120. Padding(
  121. padding: const EdgeInsets.only(left: 10.0),
  122. child: Text(
  123. "${widget.userInfo.age}岁",
  124. style:
  125. TextStyle(color: Color(0xff999999), fontSize: 12),
  126. ),
  127. ),
  128. if (widget.userInfo.city != null)
  129. Padding(
  130. padding: const EdgeInsets.only(left: 10.0),
  131. child: Text(
  132. "${widget.userInfo.province}${widget.userInfo.province == widget.userInfo.city ? '' : widget.userInfo.city}",
  133. style:
  134. TextStyle(color: Color(0xff999999), fontSize: 12),
  135. ),
  136. ),
  137. ],
  138. )
  139. : Text(
  140. "请求添加您为好友",
  141. style: Theme.of(context).textTheme.subtitle1,
  142. ),
  143. ],
  144. );
  145. }
  146. Widget baseDialog(double width, Widget button, bool isShowUserInfo) {
  147. return Center(
  148. child: Container(
  149. width: width,
  150. decoration: BoxDecoration(
  151. borderRadius: BorderRadius.all(Radius.circular(10.0)),
  152. color: Colors.white,
  153. ),
  154. child: Column(
  155. mainAxisSize: MainAxisSize.min,
  156. children: <Widget>[
  157. Align(
  158. alignment: Alignment.topRight,
  159. child: IconButton(
  160. icon: Image.asset("lib/assets/img/btn_close_big.png"),
  161. onPressed: () async {
  162. await api.postRejectFriend(widget.userInfo.id);
  163. Navigator.pop(context, false);
  164. },
  165. )),
  166. ConstrainedBox(
  167. constraints: BoxConstraints(
  168. maxHeight: MediaQuery.of(context).size.height - 100),
  169. child: SingleChildScrollView(
  170. child: userInfoContent(isShowUserInfo))),
  171. Padding(
  172. padding: const EdgeInsets.all(16.0),
  173. child: Row(
  174. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  175. children: <Widget>[
  176. Expanded(
  177. child: FeelCancelButton(
  178. height: 35,
  179. callback: () async {
  180. // 这里cancel好像不需要?
  181. // 代表是第三个框 可以 取消...
  182. if (!isShowUserInfo) {
  183. await api.postRejectFriend(widget.userInfo.id);
  184. }
  185. Navigator.of(context).pop(false);
  186. },
  187. content: "取消"),
  188. ),
  189. SizedBox(
  190. width: 16,
  191. ),
  192. Expanded(child: button)
  193. ],
  194. ),
  195. )
  196. ],
  197. ),
  198. ),
  199. );
  200. }
  201. Widget scanDialog(double width) {
  202. return baseDialog(
  203. width,
  204. PrimaryButton(
  205. height: 35,
  206. callback: () async {
  207. await api.getNewFriend(widget.newFriendCode);
  208. setState(() {
  209. dialogIndex = 1;
  210. initOkText(); // 申请中的 setState
  211. pollGetIsFriend(widget.userInfo.id); // 是否是好友的
  212. });
  213. },
  214. content: "申请好友"),
  215. true,
  216. );
  217. }
  218. Widget confirmDialog(double width) {
  219. return baseDialog(width,
  220. FeelCancelButton(height: 35, content: okText, callback: () {}), true);
  221. }
  222. Widget confirmDialogOther(double width) {
  223. return baseDialog(
  224. width,
  225. PrimaryButton(
  226. height: 35,
  227. callback: () async {
  228. var data =
  229. (await api.postAcceptFriend(widget.userInfo.id)).data;
  230. // 这里我不想去 争论什么 这里就是返回null的。 从来都没有用result 去 判断...
  231. if (data == null) {
  232. ToastUtil.show("添加成功");
  233. }
  234. Navigator.of(context).pop(false);
  235. },
  236. content: "添加"),
  237. false);
  238. }
  239. dialogs = [scanDialog, confirmDialog, confirmDialogOther];
  240. return Material(
  241. type: MaterialType.transparency,
  242. child: widget.isWaitAdd != null
  243. ? dialogs[2](_width)
  244. : dialogs[dialogIndex](_width),
  245. );
  246. }
  247. }