textSpan static method
List<TextSpan>
textSpan(
- BuildContext context,
- bool isSelf,
- String text,
- int start, {
- int? end,
- ChatUIConfig? chatUIConfig,
- Map<
String, dynamic> ? remoteExtension,
解析Text消息,将@消息和普通文本分开
Implementation
static List<TextSpan> textSpan(
BuildContext context,
bool isSelf,
String text,
int start, {
int? end,
ChatUIConfig? chatUIConfig,
Map<String, dynamic>? remoteExtension,
}) {
//定义文本字体大小和颜色
final textSize = (isSelf
? chatUIConfig?.sendMessageTextSize
: chatUIConfig?.receiveMessageTextSize) ??
16;
final textColor = (isSelf
? chatUIConfig?.sendMessageTextColor
: chatUIConfig?.receiveMessageTextColor) ??
CommonColors.color_333333;
final textAitColor =
chatUIConfig?.messageLinkColor ?? CommonColors.color_007aff;
//需要返回的spans
final List<TextSpan> spans = [];
//如果有@消息,则需要将@消息的文本和普通文本分开
if (remoteExtension?[ChatMessage.keyAitMsg] != null) {
//获取@消息的文本list
List<AitItemModel> aitSegments = [];
//将所有@的文本和位置提取出来
try {
var aitMap = remoteExtension![ChatMessage.keyAitMsg] as Map;
final AitContactsModel aitContactsModel = AitContactsModel.fromMap(
Map<String, dynamic>.from(aitMap),
);
aitContactsModel.aitBlocks.forEach((key, value) {
var aitMsg = value as AitMsg;
aitMsg.segments.forEach((segment) {
aitSegments.add(AitItemModel(key, aitMsg.text, segment));
});
});
} catch (e) {
Alog.e(
tag: 'ChatKitMessageTextItem',
content: 'aitContactsModel.fromMap error: $e',
);
}
//如果没有解析到@消息,则直接返回
if (aitSegments.isEmpty) {
spans.add(
TextSpan(
text: text,
style: TextStyle(fontSize: textSize, color: textColor),
),
);
return spans;
}
//根据@消息的位置,将文本分成多个部分
aitSegments.sort((a, b) => a.segment.start.compareTo(b.segment.start));
int preIndex = start;
for (var aitItem in aitSegments) {
if (preIndex > aitItem.segment.endIndex) {
continue;
}
//@之前的部分
if (preIndex < text.length && aitItem.segment.start > preIndex) {
spans.add(
TextSpan(
text: text.substring(
preIndex,
min(aitItem.segment.start, text.length),
),
style: TextStyle(fontSize: textSize, color: textColor),
),
);
}
//@部分
if (end == null) {
spans.add(
TextSpan(
text: aitItem.text,
style: TextStyle(fontSize: textSize, color: textAitColor),
recognizer: TapGestureRecognizer()
..onTap = () {
//点击@消息,如果有自定义回调,则回调,否则跳转到用户详情页
if (chatUIConfig?.onTapAitLink != null) {
chatUIConfig?.onTapAitLink?.call(
aitItem.account,
aitItem.text,
);
} else if (aitItem.account != AitContactsModel.accountAll) {
if (IMKitClient.account() != aitItem.account) {
goToContactDetail(context, aitItem.account);
} else {
gotoMineInfoPage(context);
}
}
},
),
);
preIndex = aitItem.segment.endIndex;
} else if (aitItem.segment.start < end) {
spans.add(
TextSpan(
text: aitItem.text,
style: TextStyle(fontSize: textSize, color: textAitColor),
recognizer: TapGestureRecognizer()
..onTap = () {
//点击@消息,如果有自定义回调,则回调,否则跳转到用户详情页
if (chatUIConfig?.onTapAitLink != null) {
chatUIConfig?.onTapAitLink?.call(
aitItem.account,
aitItem.text,
);
} else if (aitItem.account != AitContactsModel.accountAll) {
if (IMKitClient.account() != aitItem.account) {
goToContactDetail(context, aitItem.account);
} else {
gotoMineInfoPage(context);
}
}
},
),
);
preIndex = aitItem.segment.endIndex;
}
}
//最后一个@之后的部分
final lastStartIndex = preIndex - start;
if (lastStartIndex < text.length - 1) {
spans.add(
TextSpan(
text: text.substring(lastStartIndex, text.length),
style: TextStyle(fontSize: textSize, color: textColor),
),
);
}
} else {
//没有@消息,直接返回
spans.add(
TextSpan(
text: text,
style: TextStyle(fontSize: textSize, color: textColor),
),
);
}
return spans;
}