feat: 评论查看更多

This commit is contained in:
guozhigq
2024-10-17 00:36:02 +08:00
parent 0ec14fe1fa
commit 163b8a3013
2 changed files with 98 additions and 34 deletions

View File

@ -1,6 +1,7 @@
class ReplyContent { class ReplyContent {
ReplyContent({ ReplyContent({
this.message, this.message,
this.message2,
this.atNameToMid, // @的用户的mid null this.atNameToMid, // @的用户的mid null
this.members, // 被@的用户List 如果有的话 [] this.members, // 被@的用户List 如果有的话 []
this.emote, // 表情包 如果有的话 null this.emote, // 表情包 如果有的话 null
@ -13,6 +14,7 @@ class ReplyContent {
}); });
String? message; String? message;
String? message2;
Map? atNameToMid; Map? atNameToMid;
List<MemberItemModel>? members; List<MemberItemModel>? members;
Map? emote; Map? emote;
@ -24,10 +26,17 @@ class ReplyContent {
Map? topicsMeta; Map? topicsMeta;
ReplyContent.fromJson(Map<String, dynamic> json) { ReplyContent.fromJson(Map<String, dynamic> json) {
message = json['message'] message = message2 = json['message']
.replaceAll('&gt;', '>') .replaceAll('&gt;', '>')
.replaceAll('&#34;', '"') .replaceAll('&#34;', '"')
.replaceAll('&#39;', "'"); .replaceAll('&#39;', "'")
.replaceAll('&amp;', '&')
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&quot;', '"')
.replaceAll('&apos;', "'")
.replaceAll('&nbsp;', ' ');
atNameToMid = json['at_name_to_mid'] ?? {}; atNameToMid = json['at_name_to_mid'] ?? {};
members = json['members'] != null members = json['members'] != null
? json['members'] ? json['members']
@ -39,8 +48,8 @@ class ReplyContent {
pictures = json['pictures'] ?? []; pictures = json['pictures'] ?? [];
vote = json['vote'] ?? {}; vote = json['vote'] ?? {};
richText = json['rich_text'] ?? {}; richText = json['rich_text'] ?? {};
// 不包含@ 笔记 图片的时候,文字可折叠 // 不包含@ 笔记的时候,文字可折叠
isText = atNameToMid!.isEmpty && vote!.isEmpty && pictures!.isEmpty; isText = atNameToMid!.isEmpty && vote!.isEmpty;
topicsMeta = json['topics_meta'] ?? {}; topicsMeta = json['topics_meta'] ?? {};
} }
} }

View File

@ -238,28 +238,53 @@ class ReplyItem extends StatelessWidget {
// title // title
Container( Container(
margin: const EdgeInsets.only(top: 10, left: 45, right: 6, bottom: 4), margin: const EdgeInsets.only(top: 10, left: 45, right: 6, bottom: 4),
child: Text.rich( child: LayoutBuilder(
style: const TextStyle(height: 1.75), builder: (BuildContext context, BoxConstraints boxConstraints) {
maxLines: String text = replyItem?.content?.message ?? '';
replyItem!.content!.isText! && replyLevel == '1' ? 3 : 999, bool didExceedMaxLines = false;
overflow: TextOverflow.ellipsis, final double maxWidth = boxConstraints.maxWidth;
TextSpan( TextPainter? textPainter;
children: [ final int maxLines =
if (replyItem!.isTop!) replyItem!.content!.isText! && replyLevel == '1' ? 6 : 999;
const WidgetSpan( try {
alignment: PlaceholderAlignment.top, textPainter = TextPainter(
child: PBadge( text: TextSpan(text: text),
text: 'TOP', maxLines: maxLines,
size: 'small', textDirection: Directionality.of(context),
stack: 'normal', );
type: 'line', textPainter.layout(maxWidth: maxWidth);
fs: 9, didExceedMaxLines = textPainter.didExceedMaxLines;
} catch (e) {
debugPrint('Error while measuring text: $e');
didExceedMaxLines = false;
}
return Text.rich(
style: const TextStyle(height: 1.75),
TextSpan(
children: [
if (replyItem!.isTop!)
const WidgetSpan(
alignment: PlaceholderAlignment.top,
child: PBadge(
text: 'TOP',
size: 'small',
stack: 'normal',
type: 'line',
fs: 9,
),
), ),
buildContent(
context,
replyItem!,
replyReply,
null,
didExceedMaxLines,
textPainter,
), ),
buildContent(context, replyItem!, replyReply, null), ],
], ),
), );
), }),
), ),
// 操作区域 // 操作区域
bottonAction(context, replyItem!.replyControl, replySave), bottonAction(context, replyItem!.replyControl, replySave),
@ -465,8 +490,8 @@ class ReplyItemRow extends StatelessWidget {
fs: 9, fs: 9,
), ),
), ),
buildContent( buildContent(context, replies![i], replyReply,
context, replies![i], replyReply, replyItem), replyItem, false, null),
], ],
), ),
), ),
@ -508,7 +533,13 @@ class ReplyItemRow extends StatelessWidget {
} }
InlineSpan buildContent( InlineSpan buildContent(
BuildContext context, replyItem, replyReply, fReplyItem) { BuildContext context,
replyItem,
replyReply,
fReplyItem,
bool didExceedMaxLines,
TextPainter? textPainter,
) {
final String routePath = Get.currentRoute; final String routePath = Get.currentRoute;
bool isVideoPage = routePath.startsWith('/video'); bool isVideoPage = routePath.startsWith('/video');
ColorScheme colorScheme = Theme.of(context).colorScheme; ColorScheme colorScheme = Theme.of(context).colorScheme;
@ -519,6 +550,25 @@ InlineSpan buildContent(
final content = replyItem.content; final content = replyItem.content;
final List<InlineSpan> spanChilds = <InlineSpan>[]; final List<InlineSpan> spanChilds = <InlineSpan>[];
if (didExceedMaxLines && content.message != '') {
final textSize = textPainter!.size;
var position = textPainter.getPositionForOffset(
Offset(
textSize.width,
textSize.height,
),
);
final endOffset = textPainter.getOffsetBefore(position.offset);
if (endOffset != null && endOffset > 0) {
content.message = content.message.substring(0, endOffset);
} else {
content.message = content.message.substring(0, position.offset);
}
} else {
content.message = content.message2;
}
// 投票 // 投票
if (content.vote.isNotEmpty) { if (content.vote.isNotEmpty) {
content.message.splitMapJoin(RegExp(r"\{vote:.*?\}"), content.message.splitMapJoin(RegExp(r"\{vote:.*?\}"),
@ -547,13 +597,6 @@ InlineSpan buildContent(
}); });
} }
content.message = content.message.replaceAll(RegExp(r"\{vote:.*?\}"), ' '); content.message = content.message.replaceAll(RegExp(r"\{vote:.*?\}"), ' ');
content.message = content.message
.replaceAll('&amp;', '&')
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&quot;', '"')
.replaceAll('&apos;', "'")
.replaceAll('&nbsp;', ' ');
// 构建正则表达式 // 构建正则表达式
final List<String> specialTokens = [ final List<String> specialTokens = [
...content.emote.keys, ...content.emote.keys,
@ -874,6 +917,18 @@ InlineSpan buildContent(
} }
} }
} }
if (didExceedMaxLines) {
spanChilds.add(
TextSpan(
text: '\n查看更多',
style: TextStyle(
color: colorScheme.primary,
),
),
);
}
// 图片渲染 // 图片渲染
if (content.pictures.isNotEmpty) { if (content.pictures.isNotEmpty) {
final List<String> picList = <String>[]; final List<String> picList = <String>[];