Skip to main content
Version: v5

Message Composer

Overview

CometChatMessageComposer is a Widget that enables users to write and send a variety of messages, including text, image, video, and custom messages.

Features such as Live Reaction, Attachments, and Message Editing are also supported by it.

Image

CometChatMessageComposer is comprised of the following Base Widgets:

Base WidgetsDescription
MessageInputThis provides a basic layout for the contents of this component, such as the TextField and buttons
ActionSheetThe ActionSheet widget presents a list of options in either a list or grid mode, depending on the user's preference

Usage

Integration

You can launch CometChatMessageComposer directly using Navigator.push, or you can define it as a widget within the build method of your State class.

1. Using Navigator to Launch CometChatMessageComposer
Navigator.push(context, MaterialPageRoute(builder: (context) => CometChatMessageComposer())); // A user or group object is required to launch this widget.
2. Embedding CometChatMessageComposer as a Widget in the build Method
import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart';
import 'package:flutter/material.dart';

class MessageComposer extends StatefulWidget {
const MessageComposer({super.key});


State<MessageComposer> createState() => _MessageComposerState();
}

class _MessageComposerState extends State<MessageComposer> {


Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
const Spacer(),
CometChatMessageComposer()
],
) // A user or group object is required to launch this widget.
)
);
}
}

Actions

Actions dictate how a widget functions. They are divided into two types: Predefined and User-defined. You can override either type, allowing you to tailor the behavior of the widget to fit your specific needs.

1. OnSendButtonClick

The OnSendButtonClick event gets activated when the send message button is clicked. It has a predefined function of sending messages entered in the composer EditText. However, you can overide this action with the following code snippet.

CometChatMessageComposer(
user: user,
onSendButtonTap: (BuildContext context, BaseMessage baseMessage, PreviewMessageMode? previewMessageMode) {
// TODO("Not yet implemented")
},
)

2. onChange

This action handles changes in the value of text in the input field of the CometChatMessageComposer widget.

CometChatMessageComposer(
user: user,
onChange: (String? text) {
// TODO("Not yet implemented")
},
)

3. onError

This action doesn't change the behavior of the widget but rather listens for any errors that occur in the MessageList widget.

CometChatMessageComposer(
user: user,
onError: (e) {
// TODO("Not yet implemented")
},
)

Filters

CometChatMessageComposer widget does not have any available filters.


Events

Events are emitted by a Widget. By using event you can extend existing functionality. Being global events, they can be applied in Multiple Locations and are capable of being Added or Removed.

The CometChatMessageComposer Widget does not emit any events of its own.


Customization

To fit your app's design requirements, you can customize the appearance of the CometChatMessageComposer widget. We provide exposed methods that allow you to modify the experience and behavior according to your specific needs.

Style

Using Style you can customize the look and feel of the widget in your app, These parameters typically control elements such as the color, size, shape, and fonts used within the widget.

1. CometChatMessageComposerStyle

To modify the styling, you can apply the CometChatMessageComposerStyle to the CometChatMessageComposer Widget using the messageComposerStyle property.

CometChatMessageComposer(
user: user,
messageComposerStyle: CometChatMessageComposerStyle(
sendButtonIconBackgroundColor: Color(0xFFF76808),
secondaryButtonIconColor: Color(0xFFF76808),
auxiliaryButtonIconColor: Color(0xFFF76808)
)
)
Image
2. MediaRecorder Style

To customize the styles of the MediaRecorder widget within the CometChatMessageComposer Widget, use the mediaRecorderStyle property. For more details, please refer to MediaRecorder styles.

CometChatMessageComposer(
user: user,
messageComposerStyle: CometChatMessageComposerStyle(
mediaRecorderStyle: CometChatMediaRecorderStyle(
recordIndicatorBackgroundColor: Color(0xFFF44649),
recordIndicatorBorderRadius: BorderRadius.circular(20),
pauseButtonBorderRadius: BorderRadius.circular(8),
deleteButtonBorderRadius: BorderRadius.circular(8),
stopButtonBorderRadius: BorderRadius.circular(8),
),
),
)
Image

3. AI Options Style

To customize the styles of the AI Options widget within the CometChatMessageComposer Widget, use the aiOptionStyle.

CometChatMessageComposer(
user: user,
messageComposerStyle: CometChatMessageComposerStyle(
aiOptionStyle: AIOptionsStyle(
backgroundColor: Color(0xFFE4EBF5),
border: Border.all(width: 3, color: Colors.red),
)
),
)

Functionality

These are a set of small functional customizations that allow you to fine-tune the overall experience of the widget. With these, you can change text, set custom icons, and toggle the visibility of UI elements.

Image
CometChatMessageComposer(
user: user,
placeholderText: "Type a message...",
disableMentions: true,
)

Below is a list of customizations along with corresponding code snippets

Message Composer Properties

PropertyData TypeDescription
userUser?Sets user for the message composer.
groupGroup?Sets group for the message composer.
messageComposerStyleCometChatMessageComposerStyle?Sets style for the message composer.
placeholderTextString?Hint text for the input field.
textString?Initial text for the input field.
onChangeFunction(String text)?Callback triggered when text changes.
textEditingControllerTextEditingController?Controls the state of the text field.
maxLineint?Maximum number of lines allowed in the input field.
disableMentionsbool?Disables mentions in the composer.
disableTypingEventsboolDisables typing events.
disableSoundForMessagesboolDisables sound for sent messages.
customSoundForMessageString?URL for custom sound when a message is sent.
customSoundForMessagePackageString?Package name for the custom sound.
parentMessageIdintID of the parent message (default is 0).
paddingEdgeInsetsGeometry?Sets padding for the message composer.
messageInputPaddingEdgeInsetsGeometry?Sets padding for the message input field.
sendButtonViewWidget?Custom send button widget.
attachmentIconWidget?Custom attachment icon.
attachmentIconURLString?URL of the attachment icon.
voiceRecordingIconWidget?Custom voice recording icon.
aiIconWidget?Custom AI button icon.
aiIconURLString?URL for the AI button icon.
aiIconPackageNameString?Package name for the AI icon.
auxiliaryButtonViewComposerWidgetBuilder?UI component forwarded to message input as auxiliary button.
secondaryButtonViewComposerWidgetBuilder?UI component forwarded to message input as secondary button.
auxiliaryButtonsAlignmentAuxiliaryButtonsAlignment?Controls position of auxiliary button view.
hideVoiceRecordingButtonbool?Option to hide the voice recording button.
recorderStartButtonIconWidget?Custom start button icon for the recorder.
recorderPauseButtonIconWidget?Custom pause button icon for the recorder.
recorderDeleteButtonIconWidget?Custom delete button icon for the recorder.
recorderStopButtonIconWidget?Custom stop button icon for the recorder.
recorderSendButtonIconWidget?Custom send button icon for the recorder.
attachmentOptionsComposerActionsBuilder?Provides options for file attachments.
hideAttachmentButtonbool?Hide/display attachment button.
hideImageAttachmentOptionbool?Hide/display image attachment option.
hideVideoAttachmentOptionbool?Hide/display video attachment option.
hideAudioAttachmentOptionbool?Hide/display audio attachment option.
hideFileAttachmentOptionbool?Hide/display file attachment option.
hidePollsOptionbool?Hide/display polls option.
hideCollaborativeDocumentOptionbool?Hide/display collaborative document option.
hideCollaborativeWhiteboardOptionbool?Hide/display collaborative whiteboard option.
hideTakePhotoOptionbool?Hide/display take photo option.
onSendButtonTapFunction(BuildContext, BaseMessage, PreviewMessageMode?)?Callback triggered when the send button is tapped.
onErrorOnError?Callback to handle errors.
stateCallBackFunction(CometChatMessageComposerController controller)?Callback to manage state of the message composer.
hideSendButtonbool?Hide/display the send button.
hideStickersButtonbool?Hide/display the sticker button.
sendButtonIconWidget?Custom send button icon.

Advanced

For advanced-level customization, you can set custom widgets to the widget. This lets you tailor each aspect of the widget to fit your exact needs and application aesthetics. You can create and define your widgets and then incorporate those into the widget.


TextFormatters

Assigns the list of text formatters. If the provided list is not null, it sets the list. Otherwise, it assigns the default text formatters retrieved from the data source. To configure the existing Mentions look and feel check out CometChatMentionsFormatter

Example

Here is the complete example for reference:

CometChatMessageComposer(
user: user,
textFormatters: [
CometChatMentionsFormatter(
style: CometChatMentionsStyle(
mentionSelfTextBackgroundColor: Color(0xFFF76808),
mentionTextBackgroundColor: Colors.white,
mentionTextColor: Colors.black,
mentionSelfTextColor: Colors.white,
)
)
],
)
Image

AttachmentOptions

By using attachmentOptions, you can set a list of custom MessageComposerActions for the CometChatMessageComposer Widget. This will override the existing list of MessageComposerActions.

Example

Here is the complete example for reference:

CometChatMessageComposer(
user: user,
attachmentOptions: (context, user, group, id) {
return <CometChatMessageComposerAction>[
CometChatMessageComposerAction(
id: "Custom Option 1",
title: "Custom Option 1",
icon: Icon(
Icons.play_circle,
color: Colors.black,
),
),
CometChatMessageComposerAction(
id: "Custom Option 2",
title: "Custom Option 2",
icon: Icon(
Icons.add_box,
color: Colors.black,
),
),
CometChatMessageComposerAction(
id: "Custom Option 3",
title: "Custom Option 3",
icon: Icon(
Icons.change_circle,
color: Colors.black,
),
),
CometChatMessageComposerAction(
id: "Custom Option 4",
title: "Custom Option 4",
icon: Icon(
Icons.sunny,
color: Colors.black,
),
)
];
},
)
Image

AuxiliaryButton Widget

You can insert a custom widget into the CometChatMessageComposer widget to add additional functionality using the auxiliaryButtonView property.

Example

Here is the complete example for reference:

CometChatMessageComposer(
user: user,
auxiliaryButtonView: (context, user, group, id) {
final existingAuxiliaryOptions = CometChatUIKit.getDataSource()
.getAuxiliaryOptions(user, group, context, id, Color(0xFFA1A1A1));
return Row(
children: [
existingAuxiliaryOptions,
Icon(
Icons.location_pin,
color: Color(0xFF6852D6),
),
],
);
},
)
Image

SecondaryButton Widget

You can add a custom widget into the SecondaryButton widget for additional functionality using the secondaryButtonView property.

Example

Here is the complete example for reference:

CometChatMessageComposer(
user: user,
secondaryButtonView: (context, user, group, id) {
return Icon(
Icons.attach_file,
color: Color(0xFF6852D6),
);
}
)
Image

SendButton Widget

You can set a custom widget in place of the already existing send button widget. Using the sendButtonView property.

Example

Here is the complete example for reference:

 CometChatMessageComposer(
user: user,
sendButtonView: IconButton(
onPressed: () {},
padding: EdgeInsets.all(4),
style: IconButton.styleFrom(
alignment: Alignment.center,
backgroundColor: Color(0xFFEDEAFA),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
)),
icon: Transform.rotate(
angle: 150,
child: Icon(
Icons.send,
size: 20,
color: Color(0xFF6852D6),
),
)
),
)
Image

Header Widget

You can set custom headerView to the CometChatMessageComposer widget using the headerView property

Example

Here is the complete example for reference:

CometChatMessageComposer(
user: user,
headerView: (context, user, group, id) {
return Container(
margin: EdgeInsets.only(
bottom: 2,
left: 8,
right: 8),
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Color(0xFFEDEAFA),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Icon(
Icons.volume_off,
size: 20,
color: Color(0xFF6852D6),
),
Text(
"User has paused their notifications",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
],
),
);
},
)
Image

You can set a custom footer widget to the CometChatMessageComposer widget using the footerView property:

Example

Here is the complete example for reference:

CometChatMessageComposer(
user: user,
footerView: (context, user, group, id) {
return Container(
width: MediaQuery.of(context).size.width/1.08,
color: Colors.yellow,
padding: const EdgeInsets.all(8),
child: const Center(child: Text("Your Footer Widget")),
);
},
)
Image