AI Agent Component Spec
AI Agent Component Spec
| Field | Value |
|---|---|
| Package | cometchat_chat_uikit |
| Key components | CometChatCompactMessageComposer, CometChatMessageList, CometChatMessageTemplate |
| Init | CometChatUIKit.init(uiKitSettings) then CometChatUIKit.login(uid) |
| Entry point | Attachment tap → image pick → inline preview → send with caption |
| Extension points | attachmentOptions, headerView, stateCallBack, onSendButtonTap, templates |
| Sample app | GitHub |
| Related | Compact Message Composer · Message Template · All Guides |
- 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.
- Image messages that include a caption display the caption text below the thumbnail in the message bubble.
Components
| Component / Class | Role |
|---|---|
CometChatCompactMessageComposer | Composer with headerView, stateCallBack, onSendButtonTap, and attachmentOptions slots |
CometChatMessageList | Renders messages using custom templates |
CometChatMessageTemplate | Defines a custom contentView for image messages with caption rendering |
MediaPicker | Picks images from gallery or camera |
InlineImagePreview | Custom widget shown above the composer (created in this guide) |
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 usesFileUtils.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 noonItemClick, 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.
5. Intercept the Send Button
When the user taps send with a pending image, build aMediaMessage 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 toCometChatCompactMessageComposer:
7. Render Captions in Image Bubbles
Create a customCometChatMessageTemplate 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 thetemplates parameter on CometChatMessageList to replace the default image template. Filter out the built-in image template and append the custom one.
Key Gotchas
| Issue | Cause | Solution |
|---|---|---|
| Send button stays disabled with image preview | Composer requires non-empty text to enable send | Inject \u200B (zero-width space) into the text field |
Caption is empty in onSendButtonTap | Controller clears textEditingController before calling the callback | Read caption from the TextMessage object passed to the callback |
headerView disappears on keystroke | CometChatMentionsFormatter calls hidePanel(composerTop) on every text change | Use headerView (widget property) instead of showPanel(composerTop) |
Custom template ignored by addTemplate | Template merge only fills null fields; default image template already has a contentView | Use templates parameter and filter out the default image template |
| Image preview crashes on iOS | MediaPicker percent-encodes paths with spaces on iOS | Use FileUtils.normalizeFilePath() to decode before passing to File() |
Feature Matrix
| Feature | Extension Point | Component |
|---|---|---|
| Image pick without auto-send | attachmentOptions | CometChatCompactMessageComposer |
| Inline preview above composer | headerView | CometChatCompactMessageComposer |
| Controller access | stateCallBack | CometChatCompactMessageComposer |
| Send interception | onSendButtonTap | CometChatCompactMessageComposer |
| Caption in image bubbles | templates | CometChatMessageList |
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.