Skip to main content
This guide adds two capabilities to the default messaging experience:
  1. When a user picks an image, an inline preview appears above the composer. They can type a caption, then tap send — or cancel to discard.
  2. Image messages that include a caption display the caption text below the thumbnail in the message bubble.
No UIKit source files are modified. Everything uses the UIKit’s public extension points. Before starting, complete the Getting Started guide.

Components


Architecture


Integration Steps

1. Add State for Pending Image

Track the pending image path and a reference to the composer controller in your messages screen state.

2. Create the Inline Image Preview Widget

This widget renders a thumbnail and cancel button above the composer. It uses FileUtils.normalizeFilePath() to handle percent-encoded iOS file paths. File: image_preview_screen.dart

3. Build Custom Attachment Options

Replace the default image/camera attachment options with versions that pick the file but show a preview instead of sending immediately. Non-image options (video, audio, file) have no onItemClick, so they fall through to the default pick-and-send behavior.

4. Show and Clear the Preview Panel

When an image is picked, store the path and inject a zero-width space (\u200B) into the text field to enable the send button. On cancel, clear the state and remove the placeholder only if no real text was typed.
The zero-width space trick is needed because the composer’s send button only enables when the text field is non-empty. This invisible character activates the button without showing any visible text.

5. Intercept the Send Button

When the user taps send with a pending image, build a MediaMessage with the caption and send it via CometChatUIKit.sendMediaMessage(). Otherwise, forward to the default SDK send. The caption is read from the TextMessage object passed to the callback — not from the text field — because the controller clears the text field before calling onSendButtonTap.

6. Wire Up the Composer

Pass all four extension points to CometChatCompactMessageComposer:

7. Render Captions in Image Bubbles

Create a custom CometChatMessageTemplate for image messages that wraps the default image bubble with a caption Text widget below it. CometChat stores the caption in MediaMessage.caption and also in metadata['text']. The template checks both locations.

8. Apply the Custom Template to the Message List

Use the templates parameter on CometChatMessageList to replace the default image template. Filter out the built-in image template and append the custom one.
Use templates, not addTemplate. The addTemplate parameter only fills in null fields on existing templates — it cannot override an existing contentView.

Key Gotchas


Feature Matrix


Next Steps

Compact Message Composer

Full reference for the compact composer component.

Message Template

Customize how message types are rendered.

Message List

Configure the message list component.

All Guides

Browse all feature and formatter guides.