message_detail_page.dart 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_easyrefresh/easy_refresh.dart';
  5. import 'package:sport/bean/comment.dart';
  6. import 'package:sport/bean/notice.dart';
  7. import 'package:sport/bean/post.dart';
  8. import 'package:sport/bean/post_user.dart';
  9. import 'package:sport/provider/lib/provider_widget.dart';
  10. import 'package:sport/provider/lib/view_state_lifecycle.dart';
  11. import 'package:sport/provider/message_detail_model.dart';
  12. import 'package:sport/router/navigator_util.dart';
  13. import 'package:sport/services/api/resp.dart';
  14. import 'package:sport/utils/DateFormat.dart';
  15. import 'package:sport/utils/toast.dart';
  16. import 'package:sport/widgets/appbar.dart';
  17. import 'package:sport/widgets/dialog/comment_dialog.dart';
  18. import 'package:sport/widgets/dialog/request_dialog.dart';
  19. import 'package:sport/widgets/error.dart';
  20. import 'package:sport/widgets/image.dart';
  21. import 'package:sport/widgets/loading.dart';
  22. import 'package:sport/widgets/misc.dart';
  23. import 'package:sport/widgets/space.dart';
  24. class MessageDetailPage extends StatefulWidget {
  25. final String type;
  26. MessageDetailPage(this.type);
  27. @override
  28. State<StatefulWidget> createState() => _PageState();
  29. }
  30. class _PageState extends ViewStateLifecycle<MessageDetailPage, MessageDetailModel> {
  31. String _title = "";
  32. @override
  33. MessageDetailModel createModel() => MessageDetailModel(widget.type);
  34. @override
  35. void initState() {
  36. super.initState();
  37. if (widget.type == "comment") {
  38. _title = "回复我的";
  39. } else if (widget.type == "like") {
  40. _title = "点赞我的";
  41. }
  42. }
  43. @override
  44. void dispose() {
  45. super.dispose();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. return Scaffold(
  50. backgroundColor: Colors.white,
  51. body: ProviderWidget<MessageDetailModel>(
  52. model: model,
  53. onModelReady: (model) => model.initData(),
  54. builder: (_, model, __) {
  55. return EasyRefresh.custom(
  56. controller: model.refreshController,
  57. enableControlFinishRefresh: true,
  58. enableControlFinishLoad: true,
  59. onRefresh: () => model.refresh(),
  60. onLoad: model.isIdle ? () => model.loadMore() : null,
  61. header: buildClassicalHeader(
  62. extent: 100.0 + MediaQuery.of(context).padding.top,
  63. triggerDistance: 120.0 + MediaQuery.of(context).padding.top,
  64. ),
  65. footer: buildClassicalFooter(),
  66. slivers: <Widget>[
  67. buildSliverAppBar(context, _title),
  68. if (model.isBusy)
  69. SliverToBoxAdapter(
  70. child: RequestLoadingWidget(),
  71. ),
  72. if (model.isIdle)
  73. SliverList(
  74. delegate: SliverChildBuilderDelegate(
  75. (context, index) {
  76. if (index.isOdd) {
  77. return Divider();
  78. }
  79. return _buildDetailWidget(context, model.list[index ~/ 2]);
  80. },
  81. childCount: model.list.length * 2 - 1,
  82. ),
  83. ),
  84. if (model.isEmpty)
  85. SliverFillRemaining(
  86. child: Center(
  87. child: RequestErrorWidget(
  88. // ()=>model.readAll(),
  89. // action: widget.type == "comment" ? "查看所有回复" : "查看所有点赞",
  90. null,
  91. msg: widget.type == "comment" ? "暂无回复~" : "暂无点赞~",
  92. assets: RequestErrorWidget.ASSETS_NO_COMMENT,
  93. ),
  94. ),
  95. )
  96. ],
  97. );
  98. },
  99. ));
  100. }
  101. Widget _buildDetailWidget(BuildContext context, Notice item) {
  102. return Padding(
  103. padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 6.0),
  104. child: Column(
  105. crossAxisAlignment: CrossAxisAlignment.start,
  106. children: <Widget>[
  107. if (item.fromUserInfo != null)
  108. GestureDetector(
  109. onTap: () => NavigatorUtil.goSocialUserDetail(context, item.fromUserInfo!),
  110. child: Row(
  111. children: <Widget>[
  112. CircleAvatar(backgroundColor: Colors.black26,backgroundImage: userAvatarProvider(item.fromUserInfo!.avatar), radius: 18),
  113. Space(
  114. width: 8,
  115. ),
  116. Column(
  117. crossAxisAlignment: CrossAxisAlignment.start,
  118. children: <Widget>[
  119. Text(
  120. item.fromUserInfo!.name ??"",
  121. style: Theme.of(context).textTheme.subtitle1!.copyWith(fontWeight: FontWeight.w600),
  122. ),
  123. Space(
  124. height: 4,
  125. ),
  126. Text(DateFormat.formatTime(item.createTime), style: Theme.of(context).textTheme.bodyText1!)
  127. ],
  128. )
  129. ],
  130. ),
  131. ),
  132. Padding(
  133. padding: const EdgeInsets.symmetric(vertical: 12.0),
  134. child: widget.type == "like"
  135. ? Row(
  136. children: <Widget>[
  137. Image.asset("lib/assets/img/bbs_icon_like_complete.png"),
  138. SizedBox(
  139. width: 8,
  140. ),
  141. Text(
  142. "赞了我的${item.commentDetail == null ? '帖子' : '评论'}",
  143. style: Theme.of(context).textTheme.subtitle1!.copyWith(fontSize: 16),
  144. )
  145. ],
  146. )
  147. : RichText(
  148. maxLines: 3,
  149. text: TextSpan(children: <InlineSpan>[
  150. TextSpan(text: '回复:', style: TextStyle(color: Theme.of(context).accentColor, fontSize: 16)),
  151. TextSpan(text: item.content, style: Theme.of(context).textTheme.subtitle1!.copyWith(fontSize: 16)),
  152. ]),
  153. ),
  154. ),
  155. item.subjectDetail == null
  156. ? Container()
  157. : item.commentDetail == null
  158. ? _buildPostWidget(item.subjectDetail!)
  159. : _buildCommentWidget(item.commentDetail!, item.subjectDetail!)
  160. ],
  161. ),
  162. );
  163. }
  164. Widget _buildCommentWidget(Comment item, Post post) {
  165. return GestureDetector(
  166. behavior: HitTestBehavior.opaque,
  167. onTap: () {
  168. Comment current = Comment.fromJson(item.toJson());
  169. current.socialInfo = PostUser(id: item.userId, name: item.userName, avatar: item.userAvatar);
  170. showCommentList(context, current);
  171. },
  172. child: Container(
  173. padding: EdgeInsets.all(12.0),
  174. decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10)), color: Color(0xfff1f1f1)),
  175. child: Column(
  176. crossAxisAlignment: CrossAxisAlignment.start,
  177. children: <Widget>[
  178. RichText(
  179. maxLines: 3,
  180. text: TextSpan(children: <InlineSpan>[
  181. TextSpan(text: '我的评论:', style: TextStyle(color: Theme.of(context).accentColor)),
  182. if (item.toUserName?.isNotEmpty == true)
  183. TextSpan(
  184. text: '@${item.toUserName} ',
  185. style: Theme.of(context).textTheme.subtitle1!.copyWith(fontSize: 16, color: Theme.of(context).accentColor),
  186. ),
  187. TextSpan(text: item.content, style: TextStyle(color: Color(0xff666666))),
  188. ]),
  189. ),
  190. if (post != null)
  191. Container(
  192. margin: EdgeInsets.symmetric(vertical: 12),
  193. padding: const EdgeInsets.symmetric(vertical: 7.0, horizontal: 8.0),
  194. decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10)), color: Colors.white),
  195. child: _postWidget(post)),
  196. ],
  197. ),
  198. ),
  199. );
  200. }
  201. Widget _buildPostWidget(Post post) {
  202. return Container(
  203. padding: const EdgeInsets.symmetric(vertical: 7.0, horizontal: 8.0),
  204. decoration: new BoxDecoration(
  205. color: Color(0xfff1f1f1), // 底色
  206. borderRadius: new BorderRadius.all(Radius.circular(10.0)),
  207. ),
  208. child: _postWidget(post));
  209. }
  210. _postWidget(Post post) {
  211. return GestureDetector(
  212. behavior: HitTestBehavior.opaque,
  213. onTap: () async {
  214. Post? netPost = await request(context, () async {
  215. RespData<Post> data = await model.api.getPostDetail(post.id!).catchError((onError) {});
  216. if (data.code == 0) {
  217. return data.data;
  218. } else {
  219. ToastUtil.show(data.msg );
  220. return null;
  221. }
  222. });
  223. if (netPost != null) NavigatorUtil.goSocialPostDetail(context, netPost, comment: true);
  224. },
  225. child: Row(
  226. children: <Widget>[
  227. if (post.images?.isNotEmpty == true)
  228. ClipRRect(
  229. child: CachedNetworkImage(
  230. imageUrl: post.images![0].src ??"",
  231. fit: BoxFit.cover,
  232. width: 50,
  233. height: 50,
  234. ),
  235. // 也可控件一边圆角大小
  236. borderRadius: new BorderRadius.all(Radius.circular(6.0)),
  237. ),
  238. Expanded(
  239. child: Container(
  240. margin: const EdgeInsets.all(5.0),
  241. child: Text(post.content??"", maxLines: 1, overflow: TextOverflow.ellipsis, style: Theme.of(context).textTheme.subtitle1!),
  242. ),
  243. )
  244. ],
  245. ));
  246. }
  247. }