black_house_page.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_osc/model/api.dart';
  4. import 'package:flutter_osc/model/constants.dart';
  5. import 'package:flutter_osc/events/LoginEvent.dart';
  6. import 'package:flutter_osc/pages/login_page.dart';
  7. import 'package:flutter_osc/util/BlackListUtils.dart';
  8. import 'package:flutter_osc/util/DataUtils.dart';
  9. import 'package:flutter_osc/util/NetUtils.dart';
  10. import 'package:flutter_osc/util/Utf8Utils.dart';
  11. import 'package:shared_preferences/shared_preferences.dart';
  12. class BlackHousePage extends StatefulWidget {
  13. @override
  14. State<StatefulWidget> createState() {
  15. return BlackHousePageState();
  16. }
  17. }
  18. class BlackHousePageState extends State<BlackHousePage> {
  19. bool isLogin = true;
  20. List? blackDataList;
  21. TextStyle btnStyle = TextStyle(color: Colors.white, fontSize: 12.0);
  22. BlackHousePageState() {
  23. queryBlackList();
  24. }
  25. queryBlackList() {
  26. DataUtils.getUserInfo().then((userInfo) {
  27. if (userInfo != null) {
  28. String url = Api.QUERY_BLACK;
  29. url += "/${userInfo.id}";
  30. NetUtils.get(url).then((data) {
  31. if (data != null) {
  32. var obj = json.decode(data);
  33. if (obj['code'] == 0) {
  34. setState(() {
  35. blackDataList = obj['msg'];
  36. });
  37. }
  38. }
  39. });
  40. } else {
  41. setState(() {
  42. isLogin = false;
  43. });
  44. }
  45. });
  46. }
  47. // 获取用户信息
  48. getUserInfo() async {
  49. SharedPreferences sp = await SharedPreferences.getInstance();
  50. String? accessToken = sp.get(DataUtils.SP_AC_TOKEN) as String?;
  51. Map<String, String?> params = Map();
  52. params['access_token'] = accessToken;
  53. NetUtils.get(Api.USER_INFO, params: params).then((data) {
  54. if (data != null) {
  55. var map = json.decode(data);
  56. DataUtils.saveUserInfo(map).then((userInfo) {
  57. queryBlackList();
  58. });
  59. }
  60. });
  61. }
  62. // 从黑名单中删除
  63. deleteFromBlack(authorId) {
  64. DataUtils.getUserInfo().then((userInfo) {
  65. if (userInfo != null) {
  66. String userId = "${userInfo.id}";
  67. Map<String, String> params = Map();
  68. params['userid'] = userId;
  69. params['authorid'] = "$authorId";
  70. NetUtils.get(Api.DELETE_BLACK, params: params).then((data) {
  71. Navigator.of(context).pop();
  72. if (data != null) {
  73. var obj = json.decode(data);
  74. if (obj['code'] == 0) {
  75. // 删除成功
  76. BlackListUtils.removeBlackId(authorId);
  77. queryBlackList();
  78. } else {
  79. showResultDialog("操作失败:${obj['msg']}");
  80. }
  81. }
  82. }).catchError((e) {
  83. Navigator.of(context).pop();
  84. showResultDialog("网络请求失败:$e");
  85. });
  86. }
  87. });
  88. }
  89. showResultDialog(String msg) {
  90. showDialog(
  91. context: context,
  92. builder: (ctx) {
  93. return AlertDialog(
  94. title: Text('提示'),
  95. content: Text(msg),
  96. actions: <Widget>[
  97. TextButton(
  98. child: Text(
  99. '确定',
  100. style: TextStyle(color: Colors.red),
  101. ),
  102. onPressed: () {
  103. Navigator.of(context).pop();
  104. },
  105. )
  106. ],
  107. );
  108. });
  109. }
  110. showSetFreeDialog(item) {
  111. String? name = Utf8Utils.decode(item['authorname']);
  112. showDialog(
  113. context: context,
  114. builder: (BuildContext ctx) {
  115. return AlertDialog(
  116. title: Text('提示'),
  117. content: Text('确定要把\"$name\"放出小黑屋吗?'),
  118. actions: <Widget>[
  119. TextButton(
  120. child: Text(
  121. '确定',
  122. style: TextStyle(color: Colors.red),
  123. ),
  124. onPressed: () {
  125. deleteFromBlack(item['authorid']);
  126. },
  127. )
  128. ],
  129. );
  130. });
  131. }
  132. Widget getBody() {
  133. if (!isLogin) {
  134. return Center(
  135. child: InkWell(
  136. child: Container(
  137. padding: const EdgeInsets.fromLTRB(15.0, 8.0, 15.0, 8.0),
  138. child: Text("去登录"),
  139. decoration: BoxDecoration(
  140. border: Border.all(color: Colors.black),
  141. borderRadius: BorderRadius.all(Radius.circular(5.0))),
  142. ),
  143. onTap: () async {
  144. final result = await Navigator.of(context)
  145. .push(MaterialPageRoute(builder: (BuildContext context) {
  146. return LoginPage();
  147. }));
  148. if (result != null && result == "refresh") {
  149. // 通知动弹页面刷新
  150. Constants.eventBus.fire(LoginEvent());
  151. getUserInfo();
  152. }
  153. },
  154. ),
  155. );
  156. }
  157. if (blackDataList == null) {
  158. return Center(
  159. child: CircularProgressIndicator(),
  160. );
  161. } else if (blackDataList!.length == 0) {
  162. return Center(
  163. child: Column(
  164. mainAxisAlignment: MainAxisAlignment.center,
  165. children: [Text("小黑屋中没人..."), Text("长按动弹列表即可往小黑屋中加人")],
  166. ),
  167. );
  168. }
  169. return GridView.count(
  170. crossAxisCount: 3,
  171. children: List.generate(blackDataList!.length, (index) {
  172. String name = Utf8Utils.decode(blackDataList![index]['authorname'])!;
  173. return Container(
  174. margin: const EdgeInsets.all(2.0),
  175. color: Colors.black,
  176. child: Column(
  177. mainAxisAlignment: MainAxisAlignment.center,
  178. children: [
  179. Container(
  180. width: 45.0,
  181. height: 45.0,
  182. decoration: BoxDecoration(
  183. shape: BoxShape.circle,
  184. color: Colors.transparent,
  185. image: DecorationImage(
  186. image: NetworkImage(
  187. "${blackDataList![index]['authoravatar']}"),
  188. fit: BoxFit.cover),
  189. border: Border.all(
  190. color: Colors.white,
  191. width: 2.0,
  192. ),
  193. ),
  194. ),
  195. Container(
  196. margin: const EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
  197. child: Text(name, style: TextStyle(color: Colors.white)),
  198. ),
  199. InkWell(
  200. child: Container(
  201. padding: const EdgeInsets.fromLTRB(8.0, 5.0, 5.0, 8.0),
  202. child: Text(
  203. "放我出去",
  204. style: btnStyle,
  205. ),
  206. decoration: BoxDecoration(
  207. border: Border.all(color: Colors.white),
  208. borderRadius: BorderRadius.all(Radius.circular(5.0))),
  209. ),
  210. onTap: () {
  211. showSetFreeDialog(blackDataList![index]);
  212. },
  213. ),
  214. ],
  215. ),
  216. );
  217. }),
  218. );
  219. }
  220. @override
  221. Widget build(BuildContext context) {
  222. return Scaffold(
  223. appBar: AppBar(
  224. title: Text("动弹小黑屋", style: TextStyle(color: Colors.white)),
  225. iconTheme: IconThemeData(color: Colors.white),
  226. ),
  227. body: Padding(
  228. padding: const EdgeInsets.fromLTRB(2.0, 4.0, 2.0, 0.0),
  229. child: getBody(),
  230. ),
  231. );
  232. }
  233. }