V2TimMessage.V2TimCustomElem.data in string format. If a large amount of information needs to be delivered, the JSON format is recommended.class CustomMessage {// Define the content here as neededString? link;String? text;String? businessID;CustomMessage.fromJSON(Map json) {link = json["link"];text = json["text"];businessID = json["businessID"];}}
CustomMessage? getCustomMessageData(V2TimCustomElem? customElem) {try {if (customElem?.data != null) {final customMessage = jsonDecode(customElem!.data!);return CustomMessage.fromJSON(customMessage);}return null;} catch (err) {return null;}}
TIMUIKitChat, use customMessageItemBuilder in messageItemBuilder to render the custom message.messageItemBuilder: MessageItemBuilder(customMessageItemBuilder: (message, isShowJump, clearJump) {final CustomMessage customMessage = getCustomMessageData(message.customElem);if (linkMessage != null) {final String option1 = linkMessage.link ?? "";return Column(mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.start,children: [Text(linkMessage.text ?? ""),MarkdownBody(data: TIM_t_para("[View Details >>]({{option1}})", "[View Details >>]($option1)")(option1: option1),styleSheet: MarkdownStyleSheet.fromTheme(ThemeData(textTheme: const TextTheme(bodyText2: TextStyle(fontSize: 16.0)))).copyWith(a: TextStyle(color: LinkUtils.hexToColor("015fff")),),)],);}}),
data, and call the sendMessage API of TIMUIKitChatController to send the custom message.
The following is a code sample showing how to create and send a custom message on the "+" panel.TIMUIKitChat.final TIMUIKitChatController _timuiKitChatController =TIMUIKitChatController();return TIMUIKitChat(controller: _timuiKitChatController,// ... Other parameters)
extraAction array of the morePanelConfig attribute of TIMUIKitChat to add the custom message sending button.
A button on the "+" panel consists of a text title and an image icon.
Sample code:morePanelConfig: MorePanelConfig(extraAction: [MorePanelItem(id: "customMessage",title: imt("Custom message"),onTap: (c) {_sendCustomMessage();},icon: Container(height: 64,width: 64,margin: const EdgeInsets.only(bottom: 4),decoration: const BoxDecoration(color: Colors.white,borderRadius: BorderRadius.all(Radius.circular(5))),child: SvgPicture.asset("images/custom-msg.svg",package: 'tencent_cloud_chat_uikit',height: 64,width: 64,),)),// ... Other buttons on the "+" panel],// ... Other parameters)
_sendCustomMessage() async {// Create a custom message. The `data`, `desc` and `extension` content can be defined by yourself.V2TimValueCallback<V2TimMsgCreateInfoResult> createCustomMessageRes =await TencentImSDKPlugin.v2TIMManager.getMessageManager().createCustomMessage(data:'{"businessID":"text_link","link":"https://www.tencentcloud.com/document/product/269/3794?from_cn_redirect=1","text":"Welcome to Tencent Cloud Chat","version":4}',desc: 'Custom desc',extension: 'Custom extension',);if (createCustomMessageRes.code == 0) {String? id = createCustomMessageRes.data?.id;// Send the custom messageV2TimValueCallback<V2TimMessage>? sendMessageRes =await _timuiKitChatController.sendMessage(messageInfo: createCustomMessageRes.data?.messageInfo);if (sendMessageRes!.code == 0) {// Message sent successfullysendMessageRes.data?.customElem?.data; //Custom `data`sendMessageRes.data?.customElem?.desc; //Custom `desc`sendMessageRes.data?.customElem?.extension; //Custom `extension`}}}
Feedback