TipList.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import '../styles/colors.dart';
  4. import 'dart:ui';
  5. import '../styles/totast.dart';
  6. import 'package:flutter_redux/flutter_redux.dart';
  7. import '../redux/AppState.dart';
  8. import '../model/SystemNotice.dart';
  9. import '../net/HttpManager.dart';
  10. import '../net/Result.dart';
  11. import 'TipInfo.dart';
  12. class TipList extends StatefulWidget {
  13. @override
  14. TipListState createState() => TipListState();
  15. }
  16. class TipListState extends State<TipList> {
  17. int currentPage = 1;
  18. List<SystemNotice> tipList = [];
  19. bool isMore = false;
  20. ScrollController _mControll;
  21. Future<void> getListPage() async {
  22. isMore = false;
  23. if (currentPage != 1) {
  24. Toast.show(context, '加载中', -1, 'loading');
  25. }
  26. Result res = await HttpManager.get('systemNotice/page',
  27. data: {'userId': StoreProvider.of<AppState>(context).state.userInfo.id, 'currentPage': currentPage, 'pageNumber': 20});
  28. Toast.hide();
  29. List<SystemNotice> list = tipList;
  30. if (currentPage == 1) {
  31. list = [];
  32. }
  33. if (res.success) {
  34. for (var item in res.data['pp']) {
  35. SystemNotice tip = SystemNotice.fromJson(item);
  36. list.add(tip);
  37. }
  38. if (res.data['page']['currentPage'] < res.data['page']['totalPage']) {
  39. isMore = true;
  40. }
  41. } else {}
  42. setState(() {
  43. tipList = list;
  44. });
  45. }
  46. @override
  47. void initState() {
  48. super.initState();
  49. _mControll = ScrollController();
  50. Future.delayed(Duration.zero, () => getListPage());
  51. _mControll.addListener(() {
  52. if (_mControll.position.pixels == _mControll.position.maxScrollExtent) {
  53. if (isMore) {
  54. currentPage++;
  55. getListPage();
  56. }
  57. }
  58. });
  59. }
  60. @override
  61. void dispose() {
  62. super.dispose();
  63. _mControll.dispose();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return WillPopScope(
  68. child: Scaffold(
  69. appBar: AppBar(
  70. // backgroundColor: PRIMARY_COLOR,
  71. title: Text('通知列表'),
  72. centerTitle: true,
  73. elevation: 0,
  74. ),
  75. body: RefreshIndicator(
  76. color: PRIMARY_COLOR,
  77. backgroundColor: Colors.white,
  78. onRefresh: () async {
  79. currentPage = 1;
  80. getListPage();
  81. },
  82. child: Container(
  83. color: BG_COLOR,
  84. child: ListView.builder(
  85. physics: AlwaysScrollableScrollPhysics(),
  86. controller: _mControll,
  87. itemCount: tipList.isNotEmpty ? tipList.length : 1,
  88. itemBuilder: (BuildContext context, int index) {
  89. if (tipList.isEmpty) {
  90. return Text(
  91. '数据正在火速加载中...',
  92. style: TextStyle(color: Colors.white30, fontSize: 13, height: 2),
  93. textAlign: TextAlign.center,
  94. );
  95. }
  96. return TipItem(
  97. tipInfo: tipList[index],
  98. tapInfo: () async {
  99. bool res = await Navigator.push(context, CupertinoPageRoute(builder: (context) => TipInfo(tipId: tipList[index].id)));
  100. if (res != null) {
  101. setState(() {
  102. tipList = [];
  103. });
  104. currentPage = 1;
  105. getListPage();
  106. }
  107. });
  108. }),
  109. ),
  110. )),
  111. onWillPop: () {
  112. Toast.hide();
  113. Navigator.of(context).pop(true);
  114. return Future.value(false);
  115. });
  116. }
  117. }
  118. typedef OnTapHomeMenu = Function();
  119. class TipItem extends StatelessWidget {
  120. TipItem({Key key, this.tipInfo, this.tapInfo}) : super(key: key);
  121. final SystemNotice tipInfo; // 用来储存传递过来的值
  122. final OnTapHomeMenu tapInfo;
  123. @override
  124. Widget build(BuildContext context) {
  125. return Container(
  126. // height: 108,
  127. margin: EdgeInsets.only(top: 10),
  128. // decoration: BoxDecoration(
  129. // gradient: LinearGradient(
  130. // colors: [Color(0xFF464B6A), Color(0xFF35395E)],
  131. // begin: Alignment.topCenter,
  132. // end: Alignment.bottomCenter,
  133. // ),
  134. // ),
  135. color: Color(0xFF363759),
  136. child: Material(
  137. color: Colors.transparent,
  138. child: InkWell(
  139. child: Padding(
  140. padding: EdgeInsets.symmetric(horizontal: 15),
  141. child: Column(
  142. children: <Widget>[
  143. Padding(
  144. padding: EdgeInsets.symmetric(vertical: 12),
  145. child: Row(
  146. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  147. children: <Widget>[
  148. Text(readTimestamp(tipInfo.createTime, 'yyyy.MM.dd HH:mm:ss'), style: TextStyle(color: Color(0xFF9BA0AE), fontSize: 13)),
  149. tipInfo.statusFlag == 0
  150. ? Text('未读', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 13, fontWeight: FontWeight.w600))
  151. : Text('已读', style: TextStyle(color: Color(0xFF000000), fontSize: 13, fontWeight: FontWeight.w600))
  152. ],
  153. ),
  154. ),
  155. Container(
  156. height: 1,
  157. color: Colors.black26,
  158. ),
  159. // _tipContent(tipInfo.content),
  160. Padding(
  161. padding: EdgeInsets.only(top: 10, bottom: 15),
  162. child:Text.rich( TextSpan(
  163. style: TextStyle(color: Colors.white, fontSize: 14),
  164. children:_tipContent(tipInfo.content,context)
  165. )
  166. // DefaultTextStyle(
  167. // //1.设置文本默认样式
  168. // style: TextStyle(color: Colors.white, fontSize: 14),
  169. // textAlign: TextAlign.start,
  170. // child: Row(
  171. // crossAxisAlignment: CrossAxisAlignment.start,
  172. // children:_tipContent(tipInfo.content),
  173. // ),
  174. // ),
  175. ))
  176. ],
  177. ),
  178. ),
  179. onTap: tapInfo,
  180. ),
  181. ),
  182. );
  183. }
  184. List<TextSpan> _tipContent(content,context) {
  185. // String content = '很遗憾,你在2019-06-05 09:23开始的游戏竞赛 [15166的吃鸡房间],[无有效结果],无法获得奖励,该局参赛人数[2]人。';
  186. List<String> contentlist1 = content.split('[');
  187. List<Map> totalContent = [];
  188. contentlist1.forEach((item) {
  189. List _list = item.split(']');
  190. if (_list.length > 1) {
  191. totalContent.add({
  192. "content": _list[0],
  193. "isImport": true,
  194. });
  195. totalContent.add({
  196. "content": _list[1],
  197. "isImport": false,
  198. });
  199. } else {
  200. totalContent.add({
  201. "content": _list[0],
  202. "isImport": false,
  203. });
  204. }
  205. });
  206. List<TextSpan> _widgetList=[];
  207. totalContent.forEach((item){
  208. if(item['isImport']){
  209. _widgetList.add(TextSpan(text:' '+item['content']+' ',style:TextStyle(
  210. color: Color(0xFFFFB726),
  211. fontWeight:FontWeight.w600
  212. )));
  213. }
  214. else{
  215. _widgetList.add(TextSpan(text:item['content']));
  216. }
  217. });
  218. // print(_widgetList);
  219. return _widgetList;
  220. }
  221. }