Documentation Index Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Field Value Package cometchat_chat_uikitKey class CometChatTextFormatter (abstract base class for custom formatters)Required setup CometChatUIKit.init(uiKitSettings: uiKitSettings) then CometChatUIKit.login("UID")Purpose Extend to create custom inline text patterns with regex, styling, and callbacks Features Text formatting, customizable styles, dynamic text replacement, input field integration Sample app GitHub Related ShortCut Formatter | Mentions Formatter | Custom Mentions Formatter | All Guides
CometChatTextFormatter is an abstract class for formatting text in the message composer and message bubbles. Extend it to build custom formatters — hashtags, keywords, or any regex-based pattern.
Capability Description Text formatting Auto-format text based on regex patterns and styles Custom styles Set colors, fonts, and backgrounds for matched text Dynamic replacement Regex-based find-and-replace in user input Input integration Real-time monitoring of the composer input field Attributed text Build styled text spans for message bubbles
Steps
1. Import the base class
import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart' ;
2. Extend CometChatTextFormatter
class HashTagTextFormatter extends CometChatTextFormatter {
HashTagTextFormatter () : super (
trackingCharacter : '#' ,
pattern : RegExp ( r'\B#(\w+)\b' ),
);
// Override required methods...
}
Set the character that triggers formatting and the regex to match.
HashTagTextFormatter () : super (
trackingCharacter : '#' ,
pattern : RegExp ( r'\B#(\w+)\b' ),
showLoadingIndicator : false ,
);
4. Implement required methods
@override
void init () {
// Initialize formatter
}
@override
void handlePreMessageSend ( BuildContext context, BaseMessage baseMessage) {
// Process message before sending
}
@override
TextStyle getMessageInputTextStyle ( BuildContext context) {
return TextStyle (color : Colors .blue);
}
@override
void onScrollToBottom ( TextEditingController textEditingController) {
// Handle scroll events
}
@override
void onChange ( TextEditingController textEditingController, String previousText) {
// Handle text changes
}
5. Customize message bubble styling
@override
TextStyle getMessageBubbleTextStyle (
BuildContext context,
BubbleAlignment ? alignment,
{ bool forConversation = false }
) {
return TextStyle (
color : alignment == BubbleAlignment .right
? Colors .white
: Colors .blue,
fontWeight : FontWeight .bold,
decoration : TextDecoration .underline,
);
}
Example
A hashtag formatter used with CometChatMessageList and CometChatMessageComposer .
import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart' ;
import 'package:flutter/material.dart' ;
class HashTagTextFormatter extends CometChatTextFormatter {
HashTagTextFormatter () : super (
trackingCharacter : '#' ,
pattern : RegExp ( r'\B#(\w+)\b' ),
showLoadingIndicator : false ,
);
@override
void init () {
// Initialization logic
}
@override
void handlePreMessageSend ( BuildContext context, BaseMessage baseMessage) {
// Process hashtags before sending
}
@override
TextStyle getMessageInputTextStyle ( BuildContext context) {
CometChatColorPalette colorPalette = CometChatThemeHelper . getColorPalette (context);
return TextStyle (
color : colorPalette.primary,
fontWeight : FontWeight .w500,
);
}
@override
TextStyle getMessageBubbleTextStyle (
BuildContext context,
BubbleAlignment ? alignment,
{ bool forConversation = false }
) {
CometChatColorPalette colorPalette = CometChatThemeHelper . getColorPalette (context);
return TextStyle (
color : alignment == BubbleAlignment .right
? colorPalette.white
: colorPalette.primary,
fontWeight : FontWeight .bold,
decoration : TextDecoration .underline,
);
}
@override
void onScrollToBottom ( TextEditingController textEditingController) {
// Handle scroll to bottom
}
@override
void onChange ( TextEditingController textEditingController, String previousText) {
// Handle text changes - detect new hashtags
String currentText = textEditingController.text;
if (currentText. contains ( '#' )) {
// Process hashtag
}
}
@override
List < AttributedText > getAttributedText (
String text,
BuildContext context,
BubbleAlignment ? alignment,
{ List < AttributedText > ? existingAttributes,
Function ( String ) ? onTap,
bool forConversation = false }
) {
return super . getAttributedText (
text,
context,
alignment,
existingAttributes : existingAttributes,
onTap : onTap ?? (hashtag) {
// Handle hashtag tap - e.g., search for hashtag
print ( 'Tapped hashtag: $ hashtag ' );
},
forConversation : forConversation,
);
}
}
Pass the formatter via the textFormatters property. import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart' ;
import 'package:flutter/material.dart' ;
class MessageListDemo extends StatefulWidget {
const MessageListDemo ({ super .key});
@override
State < MessageListDemo > createState () => _MessageListDemoState ();
}
class _MessageListDemoState extends State < MessageListDemo > {
User ? chatUser;
@override
void initState () {
super . initState ();
CometChat . getUser ( "uid" ). then ((user) {
setState (() {
chatUser = user;
});
});
}
@override
Widget build ( BuildContext context) {
if (chatUser == null ) {
return const Center (child : CircularProgressIndicator ());
}
return CometChatMessageList (
user : chatUser,
textFormatters : [ HashTagTextFormatter ()],
);
}
}
Methods Reference
Field Description trackingCharacterCharacter that starts tracking (e.g. # for hashtags) patternRegex pattern to match text for formatting showLoadingIndicatorWhether to show loading indicator during search messageBubbleTextStyleFunction to style message bubble text messageInputTextStyleFunction to style composer input text messageCurrent message being processed userUser context for the formatter groupGroup context for the formatter
Override Methods
Initialize the formatter. Called when the formatter is first used. @override
void init () {
// Setup any initial state
}
Process the message before it’s sent. Use this to modify message metadata. @override
void handlePreMessageSend ( BuildContext context, BaseMessage baseMessage) {
// Add hashtag metadata to message
if (baseMessage is TextMessage ) {
final hashtags = pattern ? . allMatches (baseMessage.text)
. map ((m) => m. group ( 1 ))
. toList ();
// Store hashtags in message metadata
}
}
Returns the TextStyle for formatted text in message bubbles. @override
TextStyle getMessageBubbleTextStyle (
BuildContext context,
BubbleAlignment ? alignment,
{ bool forConversation = false }
) {
return TextStyle (
color : Colors .blue,
fontWeight : FontWeight .bold,
);
}
Returns styled text spans for the message bubble. @override
List < AttributedText > getAttributedText (
String text,
BuildContext context,
BubbleAlignment ? alignment,
{ List < AttributedText > ? existingAttributes,
Function ( String ) ? onTap,
bool forConversation = false }
) {
// Return attributed text with styling
return super . getAttributedText (
text, context, alignment,
existingAttributes : existingAttributes,
onTap : onTap,
forConversation : forConversation,
);
}
Called when text changes in the composer. @override
void onChange ( TextEditingController textEditingController, String previousText) {
// Detect and process new patterns
}
Flutter UI Kit includes these pre-built formatters:
Formatter Description CometChatMentionsFormatter@mentions with user suggestions CometChatUrlFormatterAuto-detect and style URLs CometChatEmailFormatterAuto-detect and style email addresses CometChatPhoneNumberFormatterAuto-detect and style phone numbers
Next Steps
Mentions Formatter Add @mentions with styled tokens.
Custom Mentions Formatter Build a custom mentions formatter with filtered suggestions.
Message Composer Customize the message input component.
All Guides Browse all feature and formatter guides.
Sample App Full working sample application on GitHub.