BlackHousePage.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_osc/constants/Constants.dart';
  3. import 'package:flutter_osc/events/LoginEvent.dart';
  4. import 'package:flutter_osc/util/BlackListUtils.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. import '../util/NetUtils.dart';
  7. import '../api/Api.dart';
  8. import 'dart:convert';
  9. import '../pages/LoginPage.dart';
  10. import '../util/DataUtils.dart';
  11. import '../util/Utf8Utils.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);
  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. FlatButton(
  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. }
  111. showSetFreeDialog(item) {
  112. String name = Utf8Utils.decode(item['authorname']);
  113. showDialog(
  114. context: context,
  115. builder: (BuildContext ctx) {
  116. return AlertDialog(
  117. title: Text('提示'),
  118. content: Text('确定要把\"$name\"放出小黑屋吗?'),
  119. actions: <Widget>[
  120. FlatButton(
  121. child: Text(
  122. '确定',
  123. style: TextStyle(color: Colors.red),
  124. ),
  125. onPressed: () {
  126. deleteFromBlack(item['authorid']);
  127. },
  128. )
  129. ],
  130. );
  131. });
  132. }
  133. Widget getBody() {
  134. if (!isLogin) {
  135. return Center(
  136. child: InkWell(
  137. child: Container(
  138. padding: const EdgeInsets.fromLTRB(15.0, 8.0, 15.0, 8.0),
  139. child: Text("去登录"),
  140. decoration: BoxDecoration(
  141. border: Border.all(color: Colors.black),
  142. borderRadius: BorderRadius.all(Radius.circular(5.0))
  143. ),
  144. ),
  145. onTap: () async {
  146. final result = await Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {
  147. return LoginPage();
  148. }));
  149. if (result != null && result == "refresh") {
  150. // 通知动弹页面刷新
  151. Constants.eventBus.fire(LoginEvent());
  152. getUserInfo();
  153. }
  154. },
  155. ),
  156. );
  157. }
  158. if (blackDataList == null) {
  159. return Center(
  160. child: CircularProgressIndicator(),
  161. );
  162. } else if (blackDataList.length == 0) {
  163. return Center(
  164. child: Column(
  165. mainAxisAlignment: MainAxisAlignment.center,
  166. children: [
  167. Text("小黑屋中没人..."),
  168. Text("长按动弹列表即可往小黑屋中加人")
  169. ],
  170. ),
  171. );
  172. }
  173. return GridView.count(
  174. crossAxisCount: 3,
  175. children: List.generate(blackDataList.length, (index) {
  176. String name = Utf8Utils.decode(blackDataList[index]['authorname']);
  177. return Container(
  178. margin: const EdgeInsets.all(2.0),
  179. color: Colors.black,
  180. child: Column(
  181. mainAxisAlignment: MainAxisAlignment.center,
  182. children: [
  183. Container(
  184. width: 45.0,
  185. height: 45.0,
  186. decoration: BoxDecoration(
  187. shape: BoxShape.circle,
  188. color: Colors.transparent,
  189. image: DecorationImage(
  190. image: NetworkImage(
  191. "${blackDataList[index]['authoravatar']}"),
  192. fit: BoxFit.cover),
  193. border: Border.all(
  194. color: Colors.white,
  195. width: 2.0,
  196. ),
  197. ),
  198. ),
  199. Container(
  200. margin: const EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
  201. child:
  202. Text(name, style: TextStyle(color: Colors.white)),
  203. ),
  204. InkWell(
  205. child: Container(
  206. padding: const EdgeInsets.fromLTRB(8.0, 5.0, 5.0, 8.0),
  207. child: Text(
  208. "放我出去",
  209. style: btnStyle,
  210. ),
  211. decoration: BoxDecoration(
  212. border: Border.all(color: Colors.white),
  213. borderRadius:
  214. BorderRadius.all(Radius.circular(5.0))),
  215. ),
  216. onTap: () {
  217. showSetFreeDialog(blackDataList[index]);
  218. },
  219. ),
  220. ],
  221. ),
  222. );
  223. }),
  224. );
  225. }
  226. @override
  227. Widget build(BuildContext context) {
  228. return Scaffold(
  229. appBar: AppBar(
  230. title: Text("动弹小黑屋", style: TextStyle(color: Colors.white)),
  231. iconTheme: IconThemeData(color: Colors.white),
  232. ),
  233. body: Padding(
  234. padding: const EdgeInsets.fromLTRB(2.0, 4.0, 2.0, 0.0),
  235. child: getBody(),
  236. ),
  237. );
  238. }
  239. }