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.
 
 
 
 
 
 

309 lines
9.1 KiB

  1. import 'package:chat/data/constants.dart';
  2. import 'package:chat/generated/i18n.dart';
  3. import 'package:chat/home/AddProgram.dart';
  4. import 'package:chat/utils/CustomUI.dart';
  5. import 'package:chat/utils/MessageBox.dart';
  6. import 'package:chat/utils/MessageMgr.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:chat/utils/PopUpMenu.dart' as myPop;
  9. import 'package:oktoast/oktoast.dart';
  10. import 'package:pull_to_refresh/pull_to_refresh.dart';
  11. import '../utils/HttpUtil.dart';
  12. import 'package:dio/dio.dart';
  13. import "../data/UserData.dart";
  14. import '../utils/TokenMgr.dart';
  15. class MyProgramPage extends StatefulWidget {
  16. @required
  17. final bool isProgram;
  18. @required
  19. final bool isMan;
  20. final userId;
  21. MyProgramPage(
  22. {Key key, this.isProgram = true, this.userId, this.isMan = false})
  23. : super(key: key);
  24. @override
  25. _MyProgramPageState createState() => new _MyProgramPageState();
  26. }
  27. class _MyProgramPageState extends State<MyProgramPage> {
  28. List list = new List(); //列表要展示的数据
  29. RefreshController _refreshController =
  30. RefreshController(initialRefresh: true);
  31. int _page = 1; //加载的页数
  32. int rows = 20;
  33. bool isMyself = false;
  34. addProgramCallback(data) {
  35. list.insert(0, data);
  36. }
  37. @override
  38. void initState() {
  39. isMyself = widget.userId == UserData().basicInfo.userId;
  40. super.initState();
  41. MessageMgr().on('delete_program', msgListDelete);
  42. MessageMgr().on('Add_program', addProgramCallback);
  43. MessageMgr().on('refresh_list', msgRefreshList);
  44. MessageMgr().on('join_program', msgJoinList);
  45. }
  46. msgJoinList(data) {
  47. for (int i = 0; i < list.length; i++) {
  48. if (list[i]['Id'] == data) {
  49. list[i]['EnrollNum']++;
  50. list[i]['IsEnroll']++;
  51. setState(() {});
  52. break;
  53. }
  54. }
  55. }
  56. msgRefreshList(data) {
  57. _onRefresh();
  58. }
  59. msgListLove(data) {
  60. for (int i = 0; i < list.length; i++) {
  61. if (list[i]['Id'] == data) {
  62. list[i]['FabulousNum']++;
  63. list[i]['IsFabulous']++;
  64. setState(() {});
  65. break;
  66. }
  67. }
  68. }
  69. msgListDelete(data) {
  70. for (int i = 0; i < list.length; i++) {
  71. if (list[i]['Id'] == data) {
  72. list.removeAt(i);
  73. setState(() {});
  74. break;
  75. }
  76. }
  77. if (list.length == 0) {
  78. _onRefresh();
  79. }
  80. }
  81. Future getData(callback) async {
  82. var data = {
  83. "userId": UserData().basicInfo.userId,
  84. "visitUserId": widget.userId,
  85. };
  86. data['sign'] = TokenMgr().getSign(data);
  87. data['page'] = _page;
  88. data['rows'] = rows;
  89. Response res = await HttpUtil().post('station/gain/program',
  90. data: data, failback: () => Navigator.of(context).pop());
  91. _refreshController.refreshCompleted();
  92. var resData = res.data;
  93. if (resData['code'] == 0) {
  94. callback(resData['data']);
  95. } else {
  96. showToast(resData['msg']);
  97. }
  98. }
  99. void addProgram(str) async {
  100. if (str == 'program') {
  101. //自己是女性,且未认证,提示去认证
  102. if (!UserData().isMan() && !UserData().basicInfo.isAttestation) {
  103. CustomUI.buildNotTrue(context);
  104. return;
  105. }
  106. if (!UserData().isCanProgram) {
  107. showToast(I18n.of(context).stop_program);
  108. } else {
  109. Navigator.of(context).push(
  110. new MaterialPageRoute(
  111. builder: (context) {
  112. return AddProgram(
  113. isProgram: true,
  114. );
  115. },
  116. ),
  117. );
  118. }
  119. } else if (str == 'dynamic') {
  120. //自己是女性,且未认证,提示去认证
  121. if (!UserData().isMan() && !UserData().basicInfo.isAttestation) {
  122. CustomUI.buildNotTrue(context);
  123. return;
  124. }
  125. Navigator.of(context).push(
  126. new MaterialPageRoute(
  127. builder: (context) {
  128. return AddProgram(
  129. isProgram: false,
  130. );
  131. },
  132. ),
  133. );
  134. }
  135. }
  136. @override
  137. Widget build(BuildContext context) {
  138. Widget appBar = new AppBar(
  139. backgroundColor: AppColors.NewAppbarBgColor,
  140. title: new Text(
  141. // widget.isProgram
  142. // ? (isMyself
  143. // ? I18n.of(context).my_show
  144. // : (widget.isMan
  145. // ? I18n.of(context).his_dynamics
  146. // : I18n.of(context).her_dynamics))
  147. // : (isMyself
  148. // ? I18n.of(context).my_dynamic
  149. // : (widget.isMan
  150. // ? I18n.of(context).his_dynamics
  151. // : I18n.of(context).her_dynamics)),
  152. isMyself
  153. ? I18n.of(context).my_dynamic
  154. : (widget.isMan
  155. ? I18n.of(context).his_dynamics
  156. : I18n.of(context).her_dynamics),
  157. textScaleFactor: 1.0,
  158. style: TextStyle(color: AppColors.NewAppbarTextColor),
  159. ),
  160. centerTitle: true,
  161. leading: CustomUI.buildCustomLeading(context),
  162. elevation: 1,
  163. actions: <Widget>[
  164. isMyself
  165. ? myPop.PopupMenuButton<String>(
  166. padding: EdgeInsets.zero,
  167. offset: Offset(0, 45),
  168. child: Container(
  169. alignment: Alignment.center,
  170. child: new Padding(
  171. padding: EdgeInsets.only(
  172. right: 15, left: 15, top: 10, bottom: 10),
  173. child: new Text(
  174. I18n.of(context).release,
  175. textScaleFactor: 1.0,
  176. style: Constants.AppBarActionTextStyle,
  177. ),
  178. ),
  179. ),
  180. onSelected: (str) {
  181. addProgram(str);
  182. },
  183. itemBuilder: (BuildContext context) =>
  184. <myPop.PopupMenuEntry<String>>[
  185. myPop.PopupMenuItem<String>(
  186. value: 'program',
  187. child: Container(
  188. margin: EdgeInsets.only(top: 15, bottom: 15),
  189. child: Text(
  190. I18n.of(context).release_program,
  191. textScaleFactor: 1.0,
  192. textAlign: TextAlign.center,
  193. ),
  194. )),
  195. myPop.PopupMenuDivider(
  196. height: 1,
  197. ),
  198. myPop.PopupMenuItem<String>(
  199. value: 'dynamic',
  200. child: Container(
  201. margin: EdgeInsets.only(top: 15, bottom: 15),
  202. child: Text(
  203. I18n.of(context).release_dynamics,
  204. textScaleFactor: 1.0,
  205. textAlign: TextAlign.center,
  206. )),
  207. ),
  208. ],
  209. )
  210. : Container()
  211. ],
  212. );
  213. Widget content = Scaffold(
  214. appBar: appBar,
  215. body: SafeArea(
  216. child: SmartRefresher(
  217. enablePullDown: true,
  218. enablePullUp: true,
  219. header: MaterialClassicHeader(),
  220. footer: CustomUI.buildLoadingFooter(),
  221. controller: _refreshController,
  222. onRefresh: _onRefresh,
  223. onLoading: _onLoading,
  224. child: (_refreshController.headerStatus == RefreshStatus.completed &&
  225. list.length == 0)
  226. ? CustomUI.buildNoData(context)
  227. : ListView.builder(
  228. itemBuilder: _renderRow,
  229. itemCount: list.length,
  230. ),
  231. )),
  232. );
  233. return content;
  234. }
  235. Widget _renderRow(BuildContext context, int index) {
  236. // if (list.length == 0) {
  237. // return CustomUI.buildNoData(
  238. // context,
  239. // str: isMyself
  240. // ? (widget.isProgram
  241. // ? I18n.of(context).no_program
  242. // : I18n.of(context).on_dynamic)
  243. // : (widget.isMan
  244. // ? I18n.of(context).low_key2
  245. // : I18n.of(context).low_key),
  246. // );
  247. // }
  248. if (index < list.length) {
  249. var userInfo = list[index];
  250. print('--------------------------------------------');
  251. print(userInfo);
  252. return MessageBox(
  253. programInfo: userInfo,
  254. );
  255. }
  256. return Container();
  257. }
  258. Future<Null> _onRefresh() async {
  259. _page = 1;
  260. getData((data) {
  261. list.clear();
  262. if (data != null) {
  263. list.addAll(data);
  264. }
  265. setState(() {});
  266. });
  267. }
  268. Future _onLoading() async {
  269. _page++;
  270. getData((data) {
  271. if (data == null || data.length == 0) {
  272. _page--;
  273. _refreshController.loadNoData();
  274. } else {
  275. list.addAll(data);
  276. _refreshController.loadComplete();
  277. }
  278. setState(() {});
  279. });
  280. }
  281. @override
  282. void dispose() {
  283. _refreshController.dispose();
  284. MessageMgr().off('delete_program', msgListDelete);
  285. MessageMgr().off('Add_program', addProgramCallback);
  286. MessageMgr().off('refresh_list', msgRefreshList);
  287. MessageMgr().off('join_program', msgJoinList);
  288. super.dispose();
  289. }
  290. }