TipList.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. TipList({Key key}) : super(key: key);
  16. }
  17. class TipListState extends State<TipList> {
  18. int currentPage = 1;
  19. List<SystemNotice> tipList = [];
  20. bool isMore = false;
  21. ScrollController _mControll;
  22. Future<void> getListPage() async {
  23. isMore = false;
  24. if (currentPage != 1) {
  25. Toast.show(context, '加载中', -1, 'loading');
  26. }
  27. Result res = await HttpManager.get('systemNotice/page', data: {
  28. 'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
  29. 'currentPage': currentPage,
  30. 'pageNumber': 20,
  31. });
  32. Toast.hide();
  33. List<SystemNotice> list = tipList;
  34. if (res.success) {
  35. if (currentPage == 1) {
  36. list = [];
  37. }
  38. for (var item in res.data['pp']) {
  39. SystemNotice tip = SystemNotice.fromJson(item);
  40. list.add(tip);
  41. }
  42. if (res.data['page']['currentPage'] < res.data['page']['totalPage']) {
  43. isMore = true;
  44. }
  45. } else {}
  46. setState(() {
  47. tipList = list;
  48. PageStorage.of(context).writeState(context, tipList, identifier: ValueKey('tipList'));
  49. });
  50. }
  51. @override
  52. void initState() {
  53. super.initState();
  54. _mControll = ScrollController();
  55. tipList = PageStorage.of(context).readState(context, identifier: ValueKey('tipList')) ?? [];
  56. Future.delayed(Duration.zero, () => getListPage());
  57. _mControll.addListener(() {
  58. if (_mControll.position.pixels == _mControll.position.maxScrollExtent) {
  59. if (isMore) {
  60. currentPage++;
  61. getListPage();
  62. }
  63. }
  64. });
  65. }
  66. @override
  67. void dispose() {
  68. super.dispose();
  69. _mControll.dispose();
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return Scaffold(
  74. appBar: AppBar(
  75. title: Text('消息列表'),
  76. centerTitle: true,
  77. elevation: 0,
  78. ),
  79. body: RefreshIndicator(
  80. color: PRIMARY_COLOR,
  81. backgroundColor: Colors.white,
  82. onRefresh: () async {
  83. currentPage = 1;
  84. getListPage();
  85. },
  86. child: Container(
  87. child: ListView.builder(
  88. physics: AlwaysScrollableScrollPhysics(),
  89. controller: _mControll,
  90. itemCount: tipList.isNotEmpty ? tipList.length : 1,
  91. itemBuilder: (BuildContext context, int index) {
  92. if (tipList.isEmpty) {
  93. return Text(
  94. '数据正在火速加载中...',
  95. style: TextStyle(color: Colors.white30, fontSize: 13, height: 2),
  96. textAlign: TextAlign.center,
  97. );
  98. }
  99. return TipItem(
  100. tipInfo: tipList[index],
  101. tapInfo: () async {
  102. bool res = await Navigator.push(context, CupertinoPageRoute(builder: (context) => TipInfo(notice: tipList[index])));
  103. if (res != null) {
  104. setState(() {
  105. tipList = [];
  106. });
  107. currentPage = 1;
  108. getListPage();
  109. }
  110. });
  111. }),
  112. ),
  113. ),
  114. );
  115. }
  116. }
  117. typedef OnTapHomeMenu = Function();
  118. class TipItem extends StatelessWidget {
  119. TipItem({Key key, this.tipInfo, this.tapInfo}) : super(key: key);
  120. final SystemNotice tipInfo; // 用来储存传递过来的值
  121. final OnTapHomeMenu tapInfo;
  122. @override
  123. Widget build(BuildContext context) {
  124. return Container(
  125. // height: 108,
  126. margin: EdgeInsets.only(top: 10),
  127. color: SUB_COLOR,
  128. child: Material(
  129. color: Colors.transparent,
  130. child: InkWell(
  131. child: Padding(
  132. padding: EdgeInsets.symmetric(horizontal: 15),
  133. child: Column(
  134. mainAxisAlignment: MainAxisAlignment.center,
  135. crossAxisAlignment: CrossAxisAlignment.stretch,
  136. children: <Widget>[
  137. Padding(
  138. padding: EdgeInsets.symmetric(vertical: 12),
  139. child: Row(
  140. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  141. children: <Widget>[
  142. Text(readTimestamp(tipInfo.createTime, 'yyyy.MM.dd HH:mm:ss'), style: TextStyle(color: Color(0xFF9BA0AE), fontSize: 13)),
  143. tipInfo.statusFlag == 0
  144. ? Text('未读', style: TextStyle(color: PRIMARY_COLOR, fontSize: 13, fontWeight: FontWeight.bold))
  145. : Text('已读', style: TextStyle(color: Colors.black, fontSize: 13, fontWeight: FontWeight.bold))
  146. ],
  147. ),
  148. ),
  149. Container(
  150. height: 1,
  151. color: Colors.black26,
  152. ),
  153. Padding(
  154. padding: EdgeInsets.only(top: 10, bottom: 15),
  155. child: Text(
  156. tipInfo.content,
  157. overflow: TextOverflow.ellipsis,
  158. maxLines: 2,
  159. style: TextStyle(color: Colors.white, fontSize: 14),
  160. textAlign: TextAlign.left,
  161. ),
  162. )
  163. ],
  164. ),
  165. ),
  166. onTap: tapInfo,
  167. ),
  168. ),
  169. );
  170. }
  171. }