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.
 
 
 
 
 
 

308 line
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. }
  78. Future getData(callback) async {
  79. var data = {
  80. "userId": UserData().basicInfo.userId,
  81. "visitUserId": widget.userId,
  82. //"type": widget.isProgram == true ? 1 : 2,
  83. };
  84. data['sign'] = TokenMgr().getSign(data);
  85. data['page'] = _page;
  86. data['rows'] = rows;
  87. Response res = await HttpUtil().post('station/gain/program',
  88. data: data, failback: () => Navigator.of(context).pop());
  89. _refreshController.refreshCompleted();
  90. var resData = res.data;
  91. if (resData['code'] == 0) {
  92. callback(resData['data']);
  93. } else {
  94. showToast(resData['msg']);
  95. }
  96. }
  97. void addProgram(str) async {
  98. if (str == 'program') {
  99. //自己是女性,且未认证,提示去认证
  100. if (!UserData().isMan() && !UserData().basicInfo.isAttestation) {
  101. CustomUI.buildNotTrue(context);
  102. return;
  103. }
  104. if (!UserData().isCanProgram) {
  105. showToast(I18n.of(context).stop_program);
  106. } else {
  107. Navigator.of(context).push(
  108. new MaterialPageRoute(
  109. builder: (context) {
  110. return AddProgram(
  111. isProgram: true,
  112. );
  113. },
  114. ),
  115. );
  116. }
  117. } else if (str == 'dynamic') {
  118. //自己是女性,且未认证,提示去认证
  119. if (!UserData().isMan() && !UserData().basicInfo.isAttestation) {
  120. CustomUI.buildNotTrue(context);
  121. return;
  122. }
  123. Navigator.of(context).push(
  124. new MaterialPageRoute(
  125. builder: (context) {
  126. return AddProgram(
  127. isProgram: false,
  128. );
  129. },
  130. ),
  131. );
  132. }
  133. }
  134. @override
  135. Widget build(BuildContext context) {
  136. Widget appBar = new AppBar(
  137. backgroundColor: AppColors.NewAppbarBgColor,
  138. title: new Text(
  139. // widget.isProgram
  140. // ? (isMyself
  141. // ? I18n.of(context).my_show
  142. // : (widget.isMan
  143. // ? I18n.of(context).his_dynamics
  144. // : I18n.of(context).her_dynamics))
  145. // : (isMyself
  146. // ? I18n.of(context).my_dynamic
  147. // : (widget.isMan
  148. // ? I18n.of(context).his_dynamics
  149. // : I18n.of(context).her_dynamics)),
  150. isMyself
  151. ? I18n.of(context).my_dynamic
  152. : (widget.isMan
  153. ? I18n.of(context).his_dynamics
  154. : I18n.of(context).her_dynamics),
  155. textScaleFactor: 1.0,
  156. style: TextStyle(color: AppColors.NewAppbarTextColor),
  157. ),
  158. centerTitle: true,
  159. leading: CustomUI.buildCustomLeading(context),
  160. elevation: 1,
  161. actions: <Widget>[
  162. isMyself
  163. ? myPop.PopupMenuButton<String>(
  164. padding: EdgeInsets.zero,
  165. offset: Offset(0, 45),
  166. child: Container(
  167. alignment: Alignment.center,
  168. child: new Padding(
  169. padding: EdgeInsets.only(
  170. right: 15, left: 15, top: 10, bottom: 10),
  171. child: new Text(
  172. I18n.of(context).release,
  173. textScaleFactor: 1.0,
  174. style: Constants.AppBarActionTextStyle,
  175. ),
  176. ),
  177. ),
  178. onSelected: (str) {
  179. addProgram(str);
  180. },
  181. itemBuilder: (BuildContext context) =>
  182. <myPop.PopupMenuEntry<String>>[
  183. myPop.PopupMenuItem<String>(
  184. value: 'program',
  185. child: Container(
  186. margin: EdgeInsets.only(top: 15, bottom: 15),
  187. child: Text(
  188. I18n.of(context).release_program,
  189. textScaleFactor: 1.0,
  190. textAlign: TextAlign.center,
  191. ),
  192. )),
  193. myPop.PopupMenuDivider(
  194. height: 1,
  195. ),
  196. myPop.PopupMenuItem<String>(
  197. value: 'dynamic',
  198. child: Container(
  199. margin: EdgeInsets.only(top: 15, bottom: 15),
  200. child: Text(
  201. I18n.of(context).release_dynamics,
  202. textScaleFactor: 1.0,
  203. textAlign: TextAlign.center,
  204. )),
  205. ),
  206. ],
  207. )
  208. : Container()
  209. ],
  210. );
  211. Widget content = Scaffold(
  212. appBar: appBar,
  213. body: SafeArea(
  214. child: SmartRefresher(
  215. enablePullDown: true,
  216. enablePullUp: true,
  217. header: MaterialClassicHeader(),
  218. footer: CustomUI.buildLoadingFooter(),
  219. controller: _refreshController,
  220. onRefresh: _onRefresh,
  221. onLoading: _onLoading,
  222. child: (_refreshController.headerStatus == RefreshStatus.completed &&
  223. list.length == 0)
  224. ? CustomUI.buildNoData(context)
  225. : ListView.builder(
  226. itemBuilder: _renderRow,
  227. itemCount: list.length,
  228. ),
  229. )),
  230. );
  231. return content;
  232. }
  233. Widget _renderRow(BuildContext context, int index) {
  234. // if (list.length == 0) {
  235. // return CustomUI.buildNoData(
  236. // context,
  237. // str: isMyself
  238. // ? (widget.isProgram
  239. // ? I18n.of(context).no_program
  240. // : I18n.of(context).on_dynamic)
  241. // : (widget.isMan
  242. // ? I18n.of(context).low_key2
  243. // : I18n.of(context).low_key),
  244. // );
  245. // }
  246. if (index < list.length) {
  247. var userInfo = list[index];
  248. print('--------------------------------------------');
  249. print(userInfo);
  250. return MessageBox(
  251. programInfo: userInfo,
  252. );
  253. }
  254. return Container();
  255. }
  256. Future<Null> _onRefresh() async {
  257. _page = 1;
  258. getData((data) {
  259. list.clear();
  260. if (data != null) {
  261. list.addAll(data);
  262. }
  263. setState(() {});
  264. });
  265. }
  266. Future _onLoading() async {
  267. _page++;
  268. getData((data) {
  269. if (data == null || data.length == 0) {
  270. _page--;
  271. _refreshController.loadNoData();
  272. } else {
  273. list.addAll(data);
  274. _refreshController.loadComplete();
  275. }
  276. setState(() {});
  277. });
  278. }
  279. @override
  280. void dispose() {
  281. _refreshController.dispose();
  282. MessageMgr().off('delete_program', msgListDelete);
  283. MessageMgr().off('Add_program', addProgramCallback);
  284. MessageMgr().off('refresh_list', msgRefreshList);
  285. MessageMgr().off('join_program', msgJoinList);
  286. super.dispose();
  287. }
  288. }