TipList.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: 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. if(index==tipList.length){
  97. return Container(
  98. height: 30,
  99. );
  100. }
  101. return TipItem(
  102. tipInfo: tipList[index],
  103. tapInfo: () async {
  104. bool res = await Navigator.push(context, CupertinoPageRoute(builder: (context) => TipInfo(tipId: tipList[index].id)));
  105. if (res != null) {
  106. setState(() {
  107. tipList = [];
  108. });
  109. currentPage = 1;
  110. getListPage();
  111. }
  112. });
  113. }),
  114. ),
  115. )),
  116. onWillPop: () {
  117. Toast.hide();
  118. Navigator.of(context).pop(true);
  119. return Future.value(false);
  120. });
  121. }
  122. }
  123. typedef OnTapHomeMenu = Function();
  124. class TipItem extends StatelessWidget {
  125. TipItem({Key key, this.tipInfo, this.tapInfo}) : super(key: key);
  126. final SystemNotice tipInfo; // 用来储存传递过来的值
  127. final OnTapHomeMenu tapInfo;
  128. @override
  129. Widget build(BuildContext context) {
  130. return Container(
  131. // height: 108,
  132. margin: EdgeInsets.only(top: 10),
  133. // decoration: BoxDecoration(
  134. // gradient: LinearGradient(
  135. // colors: [Color(0xFF464B6A), Color(0xFF35395E)],
  136. // begin: Alignment.topCenter,
  137. // end: Alignment.bottomCenter,
  138. // ),
  139. // ),
  140. color: Color(0xFF363759),
  141. child: Material(
  142. color: Colors.transparent,
  143. child: InkWell(
  144. child: Padding(
  145. padding: EdgeInsets.symmetric(horizontal: 15),
  146. child: Column(
  147. children: <Widget>[
  148. Padding(
  149. padding: EdgeInsets.symmetric(vertical: 12),
  150. child: Row(
  151. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  152. children: <Widget>[
  153. Text(readTimestamp(tipInfo.createTime, 'yyyy.MM.dd HH:mm:ss'), style: TextStyle(color: Color(0xFF9BA0AE), fontSize: 13)),
  154. tipInfo.statusFlag == 0
  155. ? Text('未读', style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 13, fontWeight: FontWeight.w600))
  156. : Text('已读', style: TextStyle(color: Color(0xFF000000), fontSize: 13, fontWeight: FontWeight.w600))
  157. ],
  158. ),
  159. ),
  160. Container(
  161. height: 1,
  162. color: Colors.black26,
  163. ),
  164. // _tipContent(tipInfo.content),
  165. Container(
  166. padding: EdgeInsets.only(top: 10, bottom: 15),
  167. width: double.infinity,
  168. child:Text.rich( TextSpan(
  169. style: TextStyle(color: Colors.white, fontSize: 14),
  170. children:_tipContent(tipInfo.content,context)
  171. )
  172. // DefaultTextStyle(
  173. // //1.设置文本默认样式
  174. // style: TextStyle(color: Colors.white, fontSize: 14),
  175. // textAlign: TextAlign.start,
  176. // child: Row(
  177. // crossAxisAlignment: CrossAxisAlignment.start,
  178. // children:_tipContent(tipInfo.content),
  179. // ),
  180. // ),
  181. ))
  182. ],
  183. ),
  184. ),
  185. onTap: tapInfo,
  186. ),
  187. ),
  188. );
  189. }
  190. List<TextSpan> _tipContent(content,context) {
  191. // content = '很遗憾';
  192. List<String> contentlist1 = content.split('[');
  193. List<Map> totalContent = [];
  194. contentlist1.forEach((item) {
  195. List _list = item.split(']');
  196. if (_list.length > 1) {
  197. totalContent.add({
  198. "content": _list[0],
  199. "isImport": true,
  200. });
  201. totalContent.add({
  202. "content": _list[1],
  203. "isImport": false,
  204. });
  205. } else {
  206. totalContent.add({
  207. "content": _list[0],
  208. "isImport": false,
  209. });
  210. }
  211. });
  212. List<TextSpan> _widgetList=[];
  213. totalContent.forEach((item){
  214. if(item['isImport']){
  215. _widgetList.add(TextSpan(text:' '+item['content']+' ',style:TextStyle(
  216. color: Color(0xFFFFB726),
  217. fontWeight:FontWeight.w600
  218. )));
  219. }
  220. else{
  221. _widgetList.add(TextSpan(text:item['content']));
  222. }
  223. });
  224. return _widgetList;
  225. }
  226. }