> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-audit-mechanical-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Message Template

> Message Template — CometChat documentation.

## Overview

A MessageTemplate provides you with the capability to define and customize both the structure and the behavior of the [MessageBubble](/ui-kit/flutter/v4/message-bubble). It acts as a schema or design blueprint for the creation of a variety of [MessageBubble](/ui-kit/flutter/v4/message-bubble) widgets, allowing you to manage the appearance and interactions of [MessageBubble](/ui-kit/flutter/v4/message-bubble) within your application effectively and consistently.

### Structure

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/tu3JK9T7iskLt8fa/images/46b5bca5-message_template_structure-24d1e42bb8ad4c89cea437855da36c78.png?fit=max&auto=format&n=tu3JK9T7iskLt8fa&q=85&s=2affdf1db2c7cc452d19be78e6e3b685" width="630" height="312" data-path="images/46b5bca5-message_template_structure-24d1e42bb8ad4c89cea437855da36c78.png" />
</Frame>

The MessageBubble structure can typically be broken down into the following widgets:

1. **Leading widget**: This is where the sender's avatar is displayed. It's typically on the left of the MessageBubble for messages from others and on the right for messages from the current user.

2. **Header widget**: This displays the sender's name and is especially useful in group chats where multiple users are sending messages.

3. **Content widget**: This is the core of the MessageBubble where the message content (text, images, videos, etc.) is displayed.

4. **Bottom widget**: This widget can be used to extend the MessageBubble with additional elements, such as link previews or a 'load more' button for long messages. It's typically placed beneath the Content widget.

5. **Footer widget**: This is where the timestamp of the message and its delivery or read status are displayed. It's located at the bottom of the MessageBubble.

### Properties

MessageTemplate provides you with methods that allow you to alter various properties of the MessageBubble. These properties include aspects such as the `type` and `category` of a message, the appearance and behavior of the header, content, and footer sections of the message bubble,

1. **Type**

   Using `type` you can set the type of CometChatMessage, This will map your MessageTemplate to the corresponding CometChatMessage. You can set the MessageTemplate Type using the following code snippet.

   <Tabs>
     <Tab title="Dart">
       ```
       CometChatMessageTemplate cometChatMessageTemplate = CometChatMessageTemplate(type: MessageTypeConstants.text);
       ```
     </Tab>
   </Tabs>

2. **Category**

   Using `category` you can set the category of a MessageTemplate. This will create a MessageTemplate with the specified category and link it with a CometChatMessage of the same category.

   Please refer to our guide on [Message Categories](/sdk/flutter/v4/message-structure-and-hierarchy) for a deeper understanding of message categories.

   <Tabs>
     <Tab title="Dart">
       ```
       CometChatMessageTemplate cometChatMessageTemplate = CometChatMessageTemplate(category: MessageCategoryConstants.custom);
       ```
     </Tab>
   </Tabs>

3. **Header Widget**

   The. `headerView` method allows you to assign a custom header widget to the MessageBubble. By default, it is configured to display the sender's name.

   <Tabs>
     <Tab title="Dart">
       ```
       cometChatMessageTemplate.headerView = (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment) {
           return const Placeholder();
       };
       ```
     </Tab>
   </Tabs>

4. **Content Widget**

   The `contentView` method allows you to assign a custom content widget to the MessageBubble. By default, it displays the [Text Bubble](/ui-kit/flutter/v4/text-bubble), [Image Bubble](/ui-kit/flutter/v4/image-bubble), [File Bubble](/ui-kit/flutter/v4/file-bubble), [Audio Bubble](/ui-kit/flutter/v4/audio-bubble), or [Video Bubble](/ui-kit/flutter/v4/video-bubble), depending on the message type.

   <Tabs>
     <Tab title="Dart">
       ```
       cometChatMessageTemplate.contentView = (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment, {AdditionalConfigurations? additionalConfigurations}) {
           return const Placeholder();
       };
       ```
     </Tab>
   </Tabs>

5. **Footer Widget**

   The `footerView` method allows you to assign a custom Footer widget to the MessageBubble. By default, it displays the receipt and timestamp.

   <Tabs>
     <Tab title="Dart">
       ```
       cometChatMessageTemplate.footerView = (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment) {
           return const Placeholder();
       };
       ```
     </Tab>
   </Tabs>

6. **Bottom Widget**

   The `bottomView` method allows you to assign a custom Bottom widget to the MessageBubble.By defuault is has buttons such as link previews or a 'load more' button for long messages.

   <Tabs>
     <Tab title="Dart">
       ```
       cometChatMessageTemplate.bottomView = (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment) {
           return const Placeholder();
       };
       ```
     </Tab>
   </Tabs>

7. **Bubble Widget**

   The `bubbleView` method allows you to assign a custom Bubble widget to the MessageBubble. By default, headerView, contentView, and footerView together form a message bubble.

   <Tabs>
     <Tab title="Dart">
       ```
       cometChatMessageTemplate.bubbleView = (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment) {
           return const Placeholder();
       };
       ```
     </Tab>
   </Tabs>

8. **Options**

   The `options` lets you set the list of actions that a user can perform on a message. This includes actions like reacting to, editing, or deleting a message.

   <Tabs>
     <Tab title="Dart">
       ```
       cometChatMessageTemplate.options = (User loggedInUser, BaseMessage messageObject, BuildContext context, Group? group) {
           return <CometChatMessageOption>[];
       };
       ```
     </Tab>
   </Tabs>

## Customization

Let's dive into how you can use the [properties](#properties) of MessageTemplate to customize an existing template or add a new one to the [MessageList](/ui-kit/flutter/v4/message-list) Widget.

#### Header widget

The `headerView` method of MessageTemplate allows you to add custom widgets to the header of your message bubbles.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        templates: [
          CometChatMessageTemplate(
            type: MessageTypeConstants.text, // Define the template type
            category: MessageCategoryConstants.message, // Define the template category
            headerView: (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment) {
              return const Text("You_Header_Widget"); // Replace this placeholder Widget with your custom Widget
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/VZsUD1BhmDvn_t8v/images/ccbe767a-message_template_set_header_view_cometchat_screens-8642e8a685af0a0388aa73db93d87120.png?fit=max&auto=format&n=VZsUD1BhmDvn_t8v&q=85&s=421b8eab89ec521290dee511cbb00573" alt="Image" width="4498" height="3121" data-path="images/ccbe767a-message_template_set_header_view_cometchat_screens-8642e8a685af0a0388aa73db93d87120.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/Kgc1yhSJ8H43ZuSx/images/61d0065f-message_template_set_header_view_cometchat_screens-11451bf95e4e8326a371e5af382e6f36.png?fit=max&auto=format&n=Kgc1yhSJ8H43ZuSx&q=85&s=5e22ad227ac81b95be57d58d424aa2f0" alt="Image" width="4498" height="3121" data-path="images/61d0065f-message_template_set_header_view_cometchat_screens-11451bf95e4e8326a371e5af382e6f36.png" />
  </Tab>
</Tabs>

***

#### Content widget

The `contentView` method of MessageTemplate allows you to add a custom widget to the content of your message bubbles.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        templates: [
          CometChatMessageTemplate(
            type: MessageTypeConstants.text, // Define the template type
            category: MessageCategoryConstants.message, // Define the template category
            contentView: (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment, {AdditionalConfigurations? additionalConfigurations}) {
              return Container(
                margin: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Color(0xFFE4EBF5),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Text((baseMessage as TextMessage).text),
              ); // Replace this placeholder Widget with your custom Widget
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/W073onWd9v58kwAH/images/909f443c-message_template_set_content_view_cometchat_screens-7c7173caba1bb34def775ec8946f6b40.png?fit=max&auto=format&n=W073onWd9v58kwAH&q=85&s=bd5beb2a12bd63c1dc5ec60e95028329" alt="Image" width="4498" height="3121" data-path="images/909f443c-message_template_set_content_view_cometchat_screens-7c7173caba1bb34def775ec8946f6b40.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/OYyzjEKX00dVTAyy/images/beee9c33-message_template_set_content_view_cometchat_screens-ba94885407879d7884c2d9fe6554acd9.png?fit=max&auto=format&n=OYyzjEKX00dVTAyy&q=85&s=9795bd44bb47d2f161b7c2d5bb7d02a0" alt="Image" width="4498" height="3121" data-path="images/beee9c33-message_template_set_content_view_cometchat_screens-ba94885407879d7884c2d9fe6554acd9.png" />
  </Tab>
</Tabs>

***

#### Bottom Widget

The `bottomView` method of MessageTemplate allows you to add a custom button widget to your message bubbles.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        templates: [
          CometChatMessageTemplate(
            type: MessageTypeConstants.text, // Define the template type
            category: MessageCategoryConstants.message, // Define the template category
            bottomView: (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment, {AdditionalConfigurations? additionalConfigurations}) {
              return Container(
                padding: const EdgeInsets.all(10),
                margin: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Colors.yellow,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: const Text("Your_Bottom_Widget"),
              ); // Replace this placeholder Widget with your custom Widget
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/Kgc1yhSJ8H43ZuSx/images/5cf7a9d4-message_template_set_bottom_view_cometchat_screens-08f65b9a11bb8d6330c5a5c3fb5df823.png?fit=max&auto=format&n=Kgc1yhSJ8H43ZuSx&q=85&s=bf37633c9b54cc1dd339cdfcf78bfcd1" alt="Image" width="4498" height="3121" data-path="images/5cf7a9d4-message_template_set_bottom_view_cometchat_screens-08f65b9a11bb8d6330c5a5c3fb5df823.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/vmiPTG90MErM-yzE/images/3ebfefa6-message_template_set_bottom_view_cometchat_screens-dec8949a14d05256c2104229c5451639.png?fit=max&auto=format&n=vmiPTG90MErM-yzE&q=85&s=930ddad1fa35542cf82e4b06de567043" alt="Image" width="4498" height="3121" data-path="images/3ebfefa6-message_template_set_bottom_view_cometchat_screens-dec8949a14d05256c2104229c5451639.png" />
  </Tab>
</Tabs>

***

#### Footer Widget

The `footerView` method of MessageTemplate allows you to add a footer widget to your message bubbles.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        templates: [
          CometChatMessageTemplate(
            type: MessageTypeConstants.text, // Define the template type
            category: MessageCategoryConstants.message, // Define the template category
            footerView: (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment, {AdditionalConfigurations? additionalConfigurations}) {
              return Container(
                padding: const EdgeInsets.all(10),
                margin: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Colors.yellow,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: const Text("Your_Bottom_Widget"),
              ); // Replace this placeholder Widget with your custom Widget
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/tu3JK9T7iskLt8fa/images/4c20f5fe-message_template_set_footer_view_cometchat_screens-09168fe8a76d492c97be0e735cd0ca98.png?fit=max&auto=format&n=tu3JK9T7iskLt8fa&q=85&s=dab64381c71f1027d2622f96e94012cf" alt="Image" width="4498" height="3121" data-path="images/4c20f5fe-message_template_set_footer_view_cometchat_screens-09168fe8a76d492c97be0e735cd0ca98.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/VZsUD1BhmDvn_t8v/images/cc948ef8-message_template_set_footer_view_cometchat_screens-22eb2e49219dabc4f13a9a9993916576.png?fit=max&auto=format&n=VZsUD1BhmDvn_t8v&q=85&s=2bdcd222c790b44db2463de98a061da1" alt="Image" width="4498" height="3121" data-path="images/cc948ef8-message_template_set_footer_view_cometchat_screens-22eb2e49219dabc4f13a9a9993916576.png" />
  </Tab>
</Tabs>

***

#### Bubble Widget

The `bubbleView` method of MessageTemplate allows you to add a bubble widget to your message bubbles.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        templates: [
          CometChatMessageTemplate(
            type: MessageTypeConstants.text, // Define the template type
            category: MessageCategoryConstants.message, // Define the template category
            bubbleView: (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment, {AdditionalConfigurations? additionalConfigurations}) {
              return Container(
                width: MediaQuery.of(context).size.width/1.2,
                padding: const EdgeInsets.all(10),
                margin: const EdgeInsets.only(top: 10, bottom: 10),
                decoration: BoxDecoration(
                  color: Colors.yellow,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Flexible(
                    child: Text.rich(
                      TextSpan(
                        style: const TextStyle(
                          fontSize: 16,
                          color: Colors.black,
                        ),
                        children: <TextSpan>[
                          TextSpan(
                            text: (baseMessage as TextMessage).sender!.name,
                            style: const TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Color(0xFF6851D6)
                            ),
                          ),
                          const TextSpan(text: ": "),
                          TextSpan(
                            text: (baseMessage).text,
                            style: const TextStyle(
                              fontSize: 16,
                              color: Colors.black,
                            ),
                          ),
                        ],
                      ),
                    )
                )
              ); // Replace this placeholder Widget with your custom Widget
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/tu3JK9T7iskLt8fa/images/4e2f5305-message_template_set_bubble_view_cometchat_screens-de9880bbc92cebc6a844e150398098db.png?fit=max&auto=format&n=tu3JK9T7iskLt8fa&q=85&s=ce3a3627ddb13fff90e81815b45e24de" alt="Image" width="4498" height="3121" data-path="images/4e2f5305-message_template_set_bubble_view_cometchat_screens-de9880bbc92cebc6a844e150398098db.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/VZsUD1BhmDvn_t8v/images/c9294209-message_template_set_bubble_view_cometchat_screens-f46f771c61d933acc4e5107452ea6079.png?fit=max&auto=format&n=VZsUD1BhmDvn_t8v&q=85&s=99307e1cbe395cda96c5ecc75401dcc8" alt="Image" width="4498" height="3121" data-path="images/c9294209-message_template_set_bubble_view_cometchat_screens-f46f771c61d933acc4e5107452ea6079.png" />
  </Tab>
</Tabs>

***

#### Options List

The `options` method in the MessageTemplate allows you to customize the options that appear in the action sheet when a message is long-pressed. By default, CometChat UI Kit provides a set of options like "Reply", "Forward", "Edit", and "Delete".

However, if you wish to override or modify these options, you can use the `options` method and pass a list of `CometChatMessageOption`. This list of options will replace the default set.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        templates: [
          CometChatMessageTemplate(
            type: MessageTypeConstants.text, // Define the template type
            category: MessageCategoryConstants.message, // Define the template category
            options: (User loggedInUser, BaseMessage messageObject, BuildContext context, Group? group) {
              return <CometChatMessageOption>[
                CometChatMessageOption(
                  id: 'opt1',
                  title: "Option 1",
                  icon: "assets/img/envelope.png",
                  onClick: (BaseMessage message, CometChatMessageListControllerProtocol state) {
                    // TODO("Not yet implemented")
                  },
                ),
                CometChatMessageOption(
                  id: 'opt1',
                  title: "Option 2",
                  icon: "assets/img/envelope.png",
                  onClick: (BaseMessage message, CometChatMessageListControllerProtocol state) {
                    // TODO("Not yet implemented")
                  },
                ),
                CometChatMessageOption(
                  id: 'opt1',
                  title: "Option 3",
                  icon: "assets/img/envelope.png",
                  onClick: (BaseMessage message, CometChatMessageListControllerProtocol state) {
                    // TODO("Not yet implemented")
                  },
                ),
                CometChatMessageOption(
                  id: 'opt1',
                  title: "Option 4",
                  icon: "assets/img/envelope.png",
                  onClick: (BaseMessage message, CometChatMessageListControllerProtocol state) {
                    // TODO("Not yet implemented")
                  },
                ),
              ];
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/UfmwHah38AQUh8_c/images/a0e952f9-message_template_set_option_list_cometchat_screens-f56c5d083c98fe747411e3de0a1be79f.png?fit=max&auto=format&n=UfmwHah38AQUh8_c&q=85&s=c4723208f4de2f12f0c764829da4fb41" alt="Image" width="4498" height="3121" data-path="images/a0e952f9-message_template_set_option_list_cometchat_screens-f56c5d083c98fe747411e3de0a1be79f.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/axXRyft3PU-ANo3t/images/89760d69-message_template_set_option_list_cometchat_screens-9235718bd1c7f5f5182f9d68cf57edfe.png?fit=max&auto=format&n=axXRyft3PU-ANo3t&q=85&s=53102243eb6d5462835c4c37fc7f1957" alt="Image" width="4498" height="3121" data-path="images/89760d69-message_template_set_option_list_cometchat_screens-9235718bd1c7f5f5182f9d68cf57edfe.png" />
  </Tab>
</Tabs>

***

## New Templates

You can create an entirely new template for custom messages is one of the powerful features of CometChat's MessageTemplate.

Here is the complete example for reference:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChatMessages(
      user: user, // User object
      group: group, // Group object
      messageListConfiguration: MessageListConfiguration(
        messagesRequestBuilder: MessagesRequestBuilder()
          ..limit = 30
          ..types = CometChatUIKit.getDataSource().getAllMessageTypes() + ["location"] // Add the custom type here
          ..categories = CometChatUIKit.getDataSource().getAllMessageCategories(),
        templates: [
          CometChatMessageTemplate(
            type: "location", // Define the template type
            category: MessageCategoryConstants.custom, // Define the template category
            bubbleView: (BaseMessage baseMessage, BuildContext buildContext, BubbleAlignment alignment, {AdditionalConfigurations? additionalConfigurations}) {
              return Container(
                width: MediaQuery.of(context).size.width/2.5,
                padding: const EdgeInsets.all(10),
                margin: const EdgeInsets.only(top: 10, bottom: 10),
                decoration: BoxDecoration(
                  color: Colors.yellow,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Flexible(
                    child: Column(
                      children: [
                        Icon(
                          Icons.location_on_outlined,
                          color: Colors.black,
                          size: 100,
                        ),
                        SizedBox(height: 10,),
                        Text((baseMessage as CustomMessage).customData!['address'] as String, style: const TextStyle(
                          fontSize: 16,
                          color: Colors.black,
                        ))
                      ],
                    )
                ),
              ); // Replace this placeholder Widget with your custom Widget
            },
          ),
        ],
      ),
    )
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Android">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/0Dzaagp0tKHyO4xb/images/56c8ea67-message_template_set_new_cometchat_screens-1f770656032ea3fbecb6ede5bd427bc5.png?fit=max&auto=format&n=0Dzaagp0tKHyO4xb&q=85&s=c9157bb424426bcc53495b36b4c3f1fb" alt="Image" width="4498" height="3121" data-path="images/56c8ea67-message_template_set_new_cometchat_screens-1f770656032ea3fbecb6ede5bd427bc5.png" />
  </Tab>

  <Tab title="iOS">
    <img src="https://mintcdn.com/cometchat-22654f5b-docs-audit-mechanical-fixes/NMYZOZ87vKoeIGQT/images/087e2ca1-message_template_set_new_cometchat_screens-00e11b3a507749d4349960d5d0410c37.png?fit=max&auto=format&n=NMYZOZ87vKoeIGQT&q=85&s=7b6249413e820bac8af4cee62f8557d7" alt="Image" width="4498" height="3121" data-path="images/087e2ca1-message_template_set_new_cometchat_screens-00e11b3a507749d4349960d5d0410c37.png" />
  </Tab>
</Tabs>
