Skip to main content
FieldValue
Packagecometchat_chat_uikit
Key classCometChatTextFormatter (abstract base class for custom formatters)
Required setupCometChatUIKit.init(uiKitSettings: uiKitSettings) then CometChatUIKit.login("UID")
PurposeExtend to create custom inline text patterns with regex, styling, and callbacks
FeaturesText formatting, customizable styles, dynamic text replacement, input field integration
Sample appGitHub
RelatedShortCut Formatter | 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.
CapabilityDescription
Text formattingAuto-format text based on regex patterns and styles
Custom stylesSet colors, fonts, and backgrounds for matched text
Dynamic replacementRegex-based find-and-replace in user input
Input integrationReal-time monitoring of the composer input field
Attributed textBuild 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...
}

3. Configure tracking character and regex

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,
    );
  }
}

Methods Reference

FieldDescription
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
}

Built-in Formatters

Flutter UI Kit includes these pre-built formatters:
FormatterDescription
CometChatMentionsFormatter@mentions with user suggestions
CometChatUrlFormatterAuto-detect and style URLs
CometChatEmailFormatterAuto-detect and style email addresses
CometChatPhoneNumberFormatterAuto-detect and style phone numbers

Next Steps