scan_add_new_friend_dialog.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 'package:sport/widgets/image.dart';
  9. import '../button_cancel.dart';
  10. import '../button_primary.dart';
  11. import '../space.dart';
  12. /// 业务组件 只针对 扫码加好友情况下的弹窗...
  13. class ScanAddFriendCustomAlertDialog extends StatefulWidget {
  14. final Function? ok;
  15. final bool addClose;
  16. final UserInfo userInfo;
  17. final String? newFriendCode;
  18. final bool isWaitAdd;
  19. final Function? goNext;
  20. ScanAddFriendCustomAlertDialog({required this.userInfo, this.ok, this.addClose = false, this.newFriendCode, this.isWaitAdd = false, this.goNext});
  21. @override
  22. State<StatefulWidget> createState() {
  23. return _ScanAddFriendCustomAlertDialog();
  24. }
  25. }
  26. class _ScanAddFriendCustomAlertDialog extends State<ScanAddFriendCustomAlertDialog> with InjectApi {
  27. String okText = "申请中";
  28. int dialogIndex = 0;
  29. List<Function>? dialogs;
  30. Timer? timerText;
  31. Timer? timerIsFriend;
  32. ValueNotifier<int> _addState = ValueNotifier(0);
  33. @override
  34. initState() {
  35. super.initState();
  36. _addState.addListener(() {
  37. Timer(Duration(seconds: 3), () {
  38. Navigator.of(context).maybePop(false);
  39. });
  40. });
  41. }
  42. initOkText() {
  43. int timeout = 0;
  44. List<String> okTexts = ["申请中", "申请中.", "申请中..", "申请中..."];
  45. int max = 20;
  46. timerText?.cancel();
  47. timerText = Timer.periodic(const Duration(seconds: 1), (timer) {
  48. //到时回调
  49. setState(() {
  50. okText = "${okTexts[timeout % 3]} (${max - timeout})";
  51. });
  52. if (timeout > max) {
  53. timer.cancel();
  54. _addState.value = 99;
  55. }
  56. timeout += 1;
  57. });
  58. }
  59. @override
  60. void 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.userRelation(uid: uid)).data;
  70. if (data?.relation == "friends") {
  71. // ToastUtil.show("添加成功");
  72. timer.cancel();
  73. _addState.value = 1;
  74. // Navigator.of(context).pop(false);
  75. return;
  76. }
  77. if (data?.followDetail?.isIgnore == 0) {
  78. // ToastUtil.show("添加成功");
  79. timer.cancel();
  80. _addState.value = 2;
  81. // Navigator.of(context).pop(false);
  82. return;
  83. }
  84. });
  85. }
  86. @override
  87. Widget build(BuildContext context) {
  88. var _width = MediaQuery.of(context).size.width * 0.86;
  89. Widget userInfoContent(bool isShowUserInfo) {
  90. return Column(
  91. children: <Widget>[
  92. CircleAvatar(backgroundColor: Colors.black26,
  93. backgroundImage: userAvatarProvider(
  94. widget.userInfo.avatar,
  95. ),
  96. radius: 35),
  97. Space(
  98. height: 12.0,
  99. ),
  100. Center(
  101. child: isShowUserInfo
  102. ? ValueListenableBuilder(
  103. valueListenable: _addState,
  104. builder: (BuildContext context, int value, Widget? child) {
  105. if (value == 0) {
  106. return RichText(
  107. text: TextSpan(children: <InlineSpan>[
  108. TextSpan(text: "是否添加 ", style: Theme.of(context).textTheme.subtitle1!),
  109. TextSpan(
  110. text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
  111. TextSpan(text: " 为好友", style: Theme.of(context).textTheme.subtitle1!)
  112. ]),
  113. );
  114. }else if (value == 1) {
  115. return RichText(
  116. text: TextSpan(children: <InlineSpan>[
  117. TextSpan(text: "已添加 ", style: Theme.of(context).textTheme.subtitle1!),
  118. TextSpan(
  119. text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
  120. TextSpan(text: " 为好友", style: Theme.of(context).textTheme.subtitle1!)
  121. ]),
  122. );
  123. } else if (value == 2) {
  124. return Text("对方拒绝了您的好友申请", style: Theme.of(context).textTheme.subtitle1!);
  125. } else {
  126. return RichText(
  127. text: TextSpan(children: <InlineSpan>[
  128. TextSpan(text: "对方未回应,已", style: Theme.of(context).textTheme.subtitle1!),
  129. TextSpan(text: " 关注 ", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
  130. TextSpan(text: "对方", style: Theme.of(context).textTheme.subtitle1!)
  131. ]),
  132. );
  133. }
  134. },
  135. )
  136. : ValueListenableBuilder(
  137. valueListenable: _addState,
  138. builder: (BuildContext context, int value, Widget? child) {
  139. if (value == 1) {
  140. return RichText(
  141. text: TextSpan(children: <InlineSpan>[
  142. TextSpan(text: "已添加", style: Theme.of(context).textTheme.subtitle1!),
  143. TextSpan(
  144. text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
  145. TextSpan(text: " 为好友", style: Theme.of(context).textTheme.subtitle1!)
  146. ]),
  147. );
  148. } else if (value == 2) {
  149. return RichText(
  150. text: TextSpan(children: <InlineSpan>[
  151. TextSpan(text: "您已拒绝了 ", style: Theme.of(context).textTheme.subtitle1!),
  152. TextSpan(
  153. text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
  154. TextSpan(text: " 的好友申请", style: Theme.of(context).textTheme.subtitle1!)
  155. ]),
  156. );
  157. } else {
  158. return RichText(
  159. text: TextSpan(children: <InlineSpan>[
  160. TextSpan(
  161. text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
  162. TextSpan(text: " 请求添加您为好友", style: Theme.of(context).textTheme.subtitle1!)
  163. ]),
  164. );
  165. }
  166. }),
  167. ),
  168. // Space(
  169. // height: 8.0,
  170. // ),
  171. // isShowUserInfo
  172. // ? Row(
  173. // mainAxisAlignment: MainAxisAlignment.center,
  174. // children: <Widget>[
  175. // Row(
  176. // children: <Widget>[
  177. // Image.asset("lib/assets/img/mine_icon_${widget.userInfo.gender == 1 ? "manl" : "girl"}_gray.png"),
  178. // Space(
  179. // width: 4,
  180. // ),
  181. // Text(
  182. // widget.userInfo.gender == 1 ? "男" : "女",
  183. // style: TextStyle(color: Color(0xff999999), fontSize: 12),
  184. // ),
  185. // ],
  186. // ),
  187. // if ((widget.userInfo.age ?? 0) != 0)
  188. // Padding(
  189. // padding: const EdgeInsets.only(left: 10.0),
  190. // child: Text(
  191. // "${widget.userInfo.age}岁",
  192. // style: TextStyle(color: Color(0xff999999), fontSize: 12),
  193. // ),
  194. // ),
  195. // if (widget.userInfo.city != null)
  196. // Padding(
  197. // padding: const EdgeInsets.only(left: 10.0),
  198. // child: Text(
  199. // "${widget.userInfo.province}${widget.userInfo.province == widget.userInfo.city ? '' : widget.userInfo.city}",
  200. // style: TextStyle(color: Color(0xff999999), fontSize: 12),
  201. // ),
  202. // ),
  203. // ],
  204. // )
  205. // : Text(
  206. // "请求添加您为好友",
  207. // style: Theme.of(context).textTheme.subtitle1!,
  208. // ),
  209. ],
  210. );
  211. }
  212. Widget baseDialog(double width, Widget button, bool isShowUserInfo) {
  213. return Center(
  214. child: Container(
  215. width: width,
  216. padding: const EdgeInsets.all(24.0),
  217. decoration: BoxDecoration(
  218. borderRadius: BorderRadius.all(Radius.circular(10.0)),
  219. color: Colors.white,
  220. ),
  221. child: Column(
  222. mainAxisSize: MainAxisSize.min,
  223. children: <Widget>[
  224. ConstrainedBox(
  225. constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height - 100),
  226. child: SingleChildScrollView(child: userInfoContent(isShowUserInfo))),
  227. ValueListenableBuilder(
  228. valueListenable: _addState,
  229. builder: (BuildContext context, int value, Widget? child) {
  230. if (_addState.value == 0)
  231. return Padding(
  232. padding: const EdgeInsets.only(top: 30),
  233. child: Row(
  234. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  235. children: <Widget>[
  236. Expanded(
  237. child: FeelCancelButton(
  238. height: 35,
  239. callback: () async {
  240. // 这里cancel好像不需要?
  241. // 代表是第三个框 可以 取消...
  242. if (isShowUserInfo) {
  243. Navigator.of(context).pop(false);
  244. }else{
  245. await api.postRejectFriend(widget.userInfo.id!);
  246. _addState.value = 2;
  247. }
  248. },
  249. content: isShowUserInfo ? "取消" : "拒绝"),
  250. ),
  251. SizedBox(
  252. width: 16,
  253. ),
  254. Expanded(child: button)
  255. ],
  256. ),
  257. );
  258. return Container();
  259. },
  260. ),
  261. ],
  262. ),
  263. ),
  264. );
  265. }
  266. Widget scanDialog(double width) {
  267. return baseDialog(
  268. width,
  269. PrimaryButton(
  270. height: 35,
  271. callback: () async {
  272. await api.getNewFriend(widget.newFriendCode ?? "");
  273. setState(() {
  274. dialogIndex = 1;
  275. pollGetIsFriend(widget.userInfo.id!); // 是否是好友的
  276. });
  277. initOkText();
  278. },
  279. content: "申请好友"),
  280. true,
  281. );
  282. }
  283. Widget confirmDialog(double width) {
  284. return baseDialog(width, FeelCancelButton(height: 35, content: okText, callback: () {}), true);
  285. }
  286. Widget confirmDialogOther(double width) {
  287. return baseDialog(
  288. width,
  289. PrimaryButton(
  290. height: 35,
  291. callback: () async {
  292. var resp = (await api.postAcceptFriend(widget.userInfo.id!));
  293. // 这里我不想去 争论什么 这里就是返回null的。 从来都没有用result 去 判断...
  294. if (resp.code == 0) {
  295. // ToastUtil.show("添加成功");
  296. _addState.value = 1;
  297. }
  298. // Navigator.of(context).pop(false);
  299. },
  300. content: "添加"),
  301. false);
  302. }
  303. dialogs = [scanDialog, confirmDialog, confirmDialogOther];
  304. return Material(
  305. type: MaterialType.transparency,
  306. child: widget.isWaitAdd == true ? dialogs![2](_width) : dialogs![dialogIndex](_width),
  307. );
  308. }
  309. }