post_comment.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:sport/bean/comment.dart';
  4. import 'package:sport/pages/social/notification.dart';
  5. import 'package:sport/services/api/inject_api.dart';
  6. import 'package:sport/services/userid.dart';
  7. import 'package:sport/utils/DateFormat.dart';
  8. import 'package:sport/utils/toast.dart';
  9. import 'package:sport/widgets/dialog/alert_dialog.dart';
  10. import 'package:sport/widgets/dialog/comment_dialog.dart';
  11. import 'package:sport/widgets/dialog/modal_bottom_action.dart';
  12. import 'package:sport/widgets/dialog/request_dialog.dart';
  13. import 'package:sport/widgets/image.dart';
  14. import 'package:sport/widgets/misc.dart';
  15. import 'package:sport/widgets/space.dart';
  16. class PostCommentWidget extends StatefulWidget {
  17. final Comment comment;
  18. final int avatarWidth;
  19. PostCommentWidget(this.comment, {this.avatarWidth = 30});
  20. @override
  21. State<StatefulWidget> createState() => _PostCommentState();
  22. }
  23. class _PostCommentState extends State<PostCommentWidget> with InjectApi, UserId {
  24. var _padding = Space(
  25. width: 4,
  26. );
  27. @override
  28. void initState() {
  29. super.initState();
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. Comment item = widget.comment;
  34. return Column(
  35. children: <Widget>[
  36. Column(
  37. crossAxisAlignment: CrossAxisAlignment.start,
  38. children: <Widget>[
  39. Row(
  40. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  41. children: <Widget>[
  42. buildSocialUserWidget(context, item?.socialInfo, item.createTime, widget.avatarWidth),
  43. Padding(
  44. padding: const EdgeInsets.only(right: 12.0),
  45. child: GestureDetector(
  46. behavior: HitTestBehavior.opaque,
  47. onTap: () async {
  48. await (item.isLiked ?? false ? api.postForumUnLike(item.id, "comment_id") : api.postForumLike(item.id, "comment_id"));
  49. setState(() {
  50. item.toggleLike();
  51. });
  52. },
  53. child: Row(
  54. crossAxisAlignment: CrossAxisAlignment.end,
  55. children: <Widget>[
  56. Image.asset("lib/assets/img/bbslist_icon_like${item.isLiked ?? false ? 'd' : ''}.png"),
  57. _padding,
  58. Text(item.likeCount == 0 ? "" : "${item.likeCount}", style: Theme.of(context).textTheme.bodyText1),
  59. ],
  60. ),
  61. ),
  62. ),
  63. ],
  64. ),
  65. GestureDetector(
  66. behavior: HitTestBehavior.opaque,
  67. onTap: () {
  68. CommentInputNotification(item).dispatch(context);
  69. },
  70. onLongPress: () async {
  71. await showActionDialog(context, {
  72. if (item.userId != selfId)
  73. "举报评论": () {
  74. request(context, () async {
  75. await api.postForumReport(commentId: item.id).catchError((onError) {});
  76. ToastUtil.show("举报成功");
  77. });
  78. },
  79. if (item.userId != selfId)
  80. "屏蔽此条": () {
  81. request(context, () async {
  82. await api.postForumBlockObject(item.id, "comment").catchError((onError) {});
  83. ToastUtil.show("屏蔽成功");
  84. CommentNotification(item, CommentNotification.TYPE_DEL).dispatch(context);
  85. });
  86. },
  87. if (item.userId == selfId)
  88. "删除此条": () async {
  89. if (await showDialog(
  90. context: context,
  91. builder: (context) => CustomAlertDialog(title: '确定删除此条?', ok: () => Navigator.of(context).pop(true)),
  92. ) ==
  93. true) {
  94. request(context, () async {
  95. await api.postDelComment(item.id);
  96. ToastUtil.show("删除成功");
  97. CommentNotification(item, CommentNotification.TYPE_DEL).dispatch(context);
  98. });
  99. }
  100. },
  101. "复制文字": () {
  102. Clipboard.setData(ClipboardData(text: item.content));
  103. ToastUtil.show("已复制到粘贴板");
  104. }
  105. });
  106. },
  107. child: Padding(
  108. padding: EdgeInsets.fromLTRB(widget.avatarWidth + 8.0 + 12.0, 6.0, 12.0, 20.0),
  109. child: Column(
  110. crossAxisAlignment: CrossAxisAlignment.start,
  111. children: <Widget>[
  112. Padding(
  113. padding: const EdgeInsets.only(bottom: 8.0),
  114. child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
  115. item.isExpansion ?? false == false
  116. ? RichText(
  117. maxLines: 5,
  118. text: TextSpan(style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 15.0), children: <InlineSpan>[
  119. if (item.toUserName != null && item.toUserName.isNotEmpty)
  120. TextSpan(
  121. text: '@${item.toUserName} ',
  122. style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor),
  123. ),
  124. TextSpan(text: item.content),
  125. ]),
  126. )
  127. : RichText(
  128. text: TextSpan(style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 15.0), children: <InlineSpan>[
  129. if (item.toUserName != null && item.toUserName.isNotEmpty)
  130. TextSpan(
  131. text: '@${item.toUserName} ',
  132. style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor),
  133. ),
  134. TextSpan(text: item.content),
  135. ]),
  136. ),
  137. if (isExpansion(item.content))
  138. GestureDetector(
  139. onTap: () {
  140. item.isExpansion = !(item.isExpansion ?? true);
  141. setState(() {});
  142. },
  143. child: Padding(
  144. padding: const EdgeInsets.only(top: 5.0),
  145. child: Row(
  146. children: <Widget>[
  147. Text(
  148. item.isExpansion ?? false == false ? "全部" : "收起",
  149. style: Theme.of(context).textTheme.bodyText1,
  150. strutStyle: fixedLine,
  151. ),
  152. SizedBox(
  153. width: 4,
  154. ),
  155. item.isExpansion ?? false == false ? arrowBottom() : arrowTop()
  156. ],
  157. ),
  158. )),
  159. ])),
  160. Row(
  161. children: <Widget>[
  162. Text(DateFormat.formatTime(item.createTime), style: Theme.of(context).textTheme.bodyText1, strutStyle: fixedLine),
  163. dot,
  164. InkWell(
  165. onTap: () {
  166. _showCommentList(context, item);
  167. },
  168. child: Text("${item.commentCount > 0 ? "${item.commentCount}条" : ""}回复",
  169. style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 12.0), strutStyle: fixedLine),
  170. )
  171. ],
  172. )
  173. ],
  174. ),
  175. ),
  176. ),
  177. ],
  178. ),
  179. ],
  180. );
  181. }
  182. bool isExpansion(String text) {
  183. TextPainter _textPainter = TextPainter(maxLines: 5, text: TextSpan(text: text, style: TextStyle(fontSize: 16.0)), textDirection: TextDirection.ltr)
  184. ..layout(maxWidth: MediaQuery.of(context).size.width - 24);
  185. if (_textPainter.didExceedMaxLines) {
  186. return true;
  187. } else {
  188. return false;
  189. }
  190. }
  191. void _showCommentList(BuildContext context, Comment currentItem, {bool comment = false}) async {
  192. await showCommentList(context, currentItem, comment: comment);
  193. setState(() {});
  194. }
  195. }