
custom_message_widget.dart 中渲染的,消息内容以 String 格式,存储于 customMessage.data 中,建议使用 JSON 格式。class CustomMessage {String? link;String? text;String? businessID;CustomMessage.fromJSON(Map json) {link = json["link"];text = json["text"];businessID = json["businessID"];}}
_buildDefaultCustomMessage 方法中对自定义消息的解析,得到 CustomMessage 数据对象,通过 _buildTextLinkMessage 渲染消息。Widget _buildDefaultCustomMessage(BuildContext context,SemanticColorScheme colorsTheme,AtomicLocalizations atomicLocale,) {final customMessageData = message.messageBody?.customMessage;final customContent = ChatUtil.jsonData2Dictionary(customMessageData?.data);// 处理 text_link 类型的自定义消息if (customContent != null) {final customMessage = CustomMessage.fromJSON(customContent);if (customMessage.businessID == 'text_link') {return _buildTextLinkMessage(context, colorsTheme, customMessage);}}}Widget _buildTextLinkMessage(BuildContext context,SemanticColorScheme colorsTheme,CustomMessage customMessage,) {final text = customMessage.text ?? '';final link = customMessage.link ?? '';return Container(constraints: BoxConstraints(maxWidth: maxWidth * 0.7,),padding: const EdgeInsets.fromLTRB(16, 12, 16, 12),decoration: BoxDecoration(color: isSelf ? colorsTheme.buttonColorPrimaryDefault : colorsTheme.bgColorDefault,borderRadius: BorderRadius.circular(12),),child: Column(crossAxisAlignment: CrossAxisAlignment.start,mainAxisSize: MainAxisSize.min,children: [Text(text,style: TextStyle(color: isSelf ? colorsTheme.textColorAntiPrimary : colorsTheme.textColorPrimary,fontSize: 14,fontWeight: FontWeight.w500,),),const SizedBox(height: 8),GestureDetector(onTap: () => _launchUrl(link),child: Text('查看详情',style: TextStyle(color: isSelf ? colorsTheme.textColorAntiPrimary : colorsTheme.buttonColorPrimaryDefault,fontSize: 14,fontWeight: FontWeight.w400,decoration: TextDecoration.underline,decorationColor: isSelf ? colorsTheme.textColorAntiPrimary : colorsTheme.buttonColorPrimaryDefault,),),),],),);}
message_utils.dart 中的 getMessageAbstract方法里,对增加的自定义消息类型进行解析,用于生成在会话列表中的文字摘要。static String getMessageAbstract(MessageInfo? messageInfo,BuildContext context,{bool showMergedTitle = false}) {switch (messageInfo.messageType) {// 省略其他类型消息// 自定义消息文字摘要case MessageType.custom:final customMessage = messageInfo.messageBody?.customMessage;if (customMessage == null) {return localizations.messageTypeCustom;}// 省略部分代码final customInfo = ChatUtil.jsonData2Dictionary(customMessage.data);if (customInfo != null) {// 省略其他自定义消息解析// 解析增加的自定义消息if (customInfo['businessID'] == 'text_link') {return customInfo['text']?.toString() ?? localizations.messageTypeCustom;}}return localizations.messageTypeCustom;default:return '';}}

data 中,并调用 MessageInputStore 的 sendMessage 接口发送。// 创建 MessageInputStore 对象MessageInputStore _messageInputStore = MessageInputStore.create(conversationID: widget.conversationID);// 构造自定义消息final messageInfo = MessageInfo();messageInfo.messageType = MessageType.custom;MessageBody messageBody = MessageBody();messageBody.customMessage = CustomMessageInfo(data: '{"businessID":"text_link","link":"https://www.tencentcloud.com/document/product/269/3794?from_cn_redirect=1","text":"欢迎加入腾讯云IM大家庭!"}',);messageInfo.messageBody = messageBody;// 发送消息await _messageInputStore.sendMessage(message: messageInfo);
文档反馈