CometChatTextFormatter that detects #hashtags in messages, highlights them, and shows a suggestion dropdown in the composer.
Prerequisites
- CometChat Flutter UI Kit V6 installed
- Familiarity with Text Formatters
Step 1: Create the Formatter Class
ExtendCometChatTextFormatter and set the tracking character to #:
- Dart
Step 2: Implement Search
Thesearch method is called whenever the user types after the tracking character. Filter your data and set the suggestion list:
- Dart
Step 3: Handle Suggestion Selection
When the user taps a suggestion, insert the hashtag into the composer:- Dart
Step 4: Style Hashtags in Message Bubbles
OverridebuildMessageBubbleSpan to apply styling to hashtags when they appear in sent/received messages:
- Dart
Step 5: Pre-Send Hook (Optional)
Modify the message before it’s sent — for example, attach hashtag metadata:- Dart
Step 6: Register the Formatter
Pass your formatter to the components that should use it:- Dart
Complete Example
- Dart
Related
- Text Formatters — Text formatter API reference.
- Mentions Formatter Guide — Built-in mentions formatter.
- Shortcut Formatter Guide — Shortcut text replacement.