Skip to main content
Version: v5

Users

Overview

The CometChatUsers is a Widget, showcasing an accessible list of all available users. It provides an integral search functionality, allowing you to locate any specific user swiftly and easily. For each user listed, the widget displays the user's name by default, in conjunction with their avatar when available. Furthermore, it includes a status indicator, visually informing you whether a user is currently online or offline.

Image

The CometChatUsers widget is composed of the following Base Widgets:

WidgetsDescription
ListBasea reusable container widget having title, search box, customisable background and a List View
ListItema widget that renders data obtained from a User object on a Tile having a title, subtitle, leading and trailing view

Usage

Integration

As CometChatUsers is a custom widget, it can be launched directly by user actions such as button clicks or other interactions. It's also possible to integrate it into a tab widget. CometChatUsers offers several parameters and methods for UI customization.

You can launch CometChatUsers 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 CometChatUsers
Navigator.push(context, MaterialPageRoute(builder: (context) => const CometChatUsers()));
2. Embedding CometChatUsers as a Widget in the build Method
import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart';
import 'package:flutter/material.dart';

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


State<Users> createState() => _UsersState();
}

class _UsersState extends State<Users> {


Widget build(BuildContext context) {
return const Scaffold(
body: SafeArea(
child: CometChatUsers()
)
);
}
}

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.

onSelection

When the onSelection event is triggered, it furnishes the list of selected users. This event can be invoked by any button or action within the interface. You have the flexibility to implement custom actions or behaviors based on the selected users.

This action does not come with any predefined behavior. However, you have the flexibility to override this event and tailor it to suit your needs using the following code snippet.

CometChatUsers(
selectionMode: SelectionMode.multiple,
activateSelection: ActivateSelection.onClick,
onSelection: (list, context) {
//TODO: This action will trigger the end of selection.
},
)

onItemTap

The onItemTap method is used to override the onClick behavior in CometChatUsers. This action does not come with any predefined behavior. However, you have the flexibility to override this event and tailor it to suit your needs using the following code snippet.

CometChatUsers(
onItemTap: (context, user) {
// TODO("Not yet implemented")
},
)

onBack

This method allows users to override the onBack Pressed behavior in CometChatUsers by utilizing the onBack , providing customization options for handling the back action.

By default, this action has a predefined behavior: it simply dismisses the current widget. However, the flexibility of CometChat UI Kit allows you to override this standard behavior according to your application's specific requirements. You can define a custom action that will be performed instead when the back button is pressed.

CometChatUsers(
onBack: () {
// TODO("Not yet implemented")
},
)

onError

This method onError, allows users to override error handling within CometChatUsers, providing greater control over error responses and actions.

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

onItemLongPress

This method onItemLongPress, empowers users to customize long-click actions within CometChatUsers, offering enhanced functionality and interaction possibilities.

CometChatUsers(
onItemLongPress: (context, user) {
// TODO("Not yet implemented")
},
)

onLoad

Invoked when the list is successfully fetched and loaded, helping track component readiness.

CometChatUsers(
onLoad: (list) {
// print("User List: $list");
},
)

onEmpty

Called when the list is empty, enabling custom handling such as showing a placeholder message.

CometChatUsers(
onEmpty: () {
// print("User List is empty");
},
)

Filters

Filters allow you to customize the data displayed in a list within a Widget. You can filter the list based on your specific criteria, allowing for a more customized. Filters can be applied using RequestBuilders of Chat SDK.

1. UsersRequestBuilder

The UsersRequestBuilder enables you to filter and customize the user list based on available parameters in UsersRequestBuilder. This feature allows you to create more specific and targeted queries when fetching users. The following are the parameters available in UsersRequestBuilder

CometChatUsers(
usersRequestBuilder: UsersRequestBuilder()
..limit = 10
)

Events

Events are emitted by a CometChatUsers 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.

To handle events supported by Users you have to add corresponding listeners by using CometChatUserEvents

EventsDescription
ccUserBlockedThis will get triggered when the logged in user blocks another user
ccUserUnblockedThis will get triggered when the logged in user unblocks another user
import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart';
import 'package:flutter/material.dart';

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


State<YourScreen> createState() => _YourScreenState();
}

class _YourScreenState extends State<YourScreen> with CometChatUserEventListener {


void initState() {
super.initState();
CometChatUserEvents.addUsersListener("listenerId", this);
}


void dispose(){
super.dispose();
CometChatUserEvents.removeUsersListener("listenerId");
}


void ccUserBlocked(User user) {
// TODO
}


void ccUserUnblocked(User user) {
// TODO
}


Widget build(BuildContext context) {
return const Placeholder();
}

}

Customization

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

Style

You can set the CometChatUsersStyle to the CometChatUsers widget to customize the styling.

CometChatUsers(
usersStyle: CometChatUsersStyle(
avatarStyle: CometChatAvatarStyle(
borderRadius: BorderRadius.circular(8),
backgroundColor: Color(0xFFFBAA75),
),
titleTextColor: Color(0xFFF76808),
separatorColor: Color(0xFFF76808),
),
)
Image

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
CometChatUsers(
title: "Your Title",
backButton: Icon(Icons.add_alert, color: Color(0xFF6851D6)),
searchPlaceholder: "Search Users",
)

List of properties exposed by CometChatUsers

PropertyData TypeDescription
usersProtocolUsersBuilderProtocol?Custom users request builder protocol.
usersRequestBuilderUsersRequestBuilder?Custom request builder for fetching users.
subtitleViewWidget? Function(BuildContext, User)Widget to set subtitle for each user item.
listItemViewWidget Function(User)Custom view for each user item.
usersStyleCometChatUsersStyleStyling options for the users list.
scrollControllerScrollController?Controller for scrolling the list.
searchPlaceholderString?Placeholder text for the search input box.
backButtonWidget?Widget for the back button in the app bar.
showBackButtonboolFlag to show/hide the back button.
searchBoxIconWidget?Widget for the search box icon.
hideSearchboolFlag to show/hide the search input box.
selectionModeSelectionMode?Mode defining how users can be selected.
onSelectionFunction(List<User>?, BuildContext)?Callback for handling user selection.
loadingStateViewWidgetBuilder?View displayed during loading state.
emptyStateViewWidgetBuilder?View displayed when the list is empty.
errorStateViewWidgetBuilder?View displayed when an error occurs.
appBarOptionsList<Widget> Function(BuildContext context)?Options available in the app bar.
usersStatusVisibilitybool?Hide status indicator on user avatars.
activateSelectionActivateSelection?Controls whether selection is allowed.
onErrorOnError?Callback for handling errors.
onBackVoidCallback?Callback triggered when going back.
onItemTapFunction(BuildContext context, User)?Callback triggered when tapping a user.
onItemLongPressFunction(BuildContext context, User)?Callback triggered on long press of a user.
submitIconWidget?Widget for displaying the submit icon.
hideAppbarbool?Flag to show/hide the app bar.
controllerTagString?Identifier tag for controller management.
heightdouble?Height of the widget.
widthdouble?Width of the widget.
stickyHeaderVisibilitybool?Hide alphabets used to separate users.
searchKeywordString?Keyword used to fetch initial user list.
onLoadOnLoad<User>?Callback triggered when the list loads.
onEmptyOnEmpty?Callback triggered when the list is empty.
addOptionsList<CometChatOption>? Function(User, CometChatUsersController, BuildContext)?Additional long-press actions for users.
setOptionsList<CometChatOption>? Function(User, CometChatUsersController, BuildContext)?Actions available on long-press of a user.
titleViewWidget? Function(BuildContext, User)?Custom title view for each user.
leadingViewWidget? Function(User)?Widget for leading section of each user.
trailingViewWidget? Function(User)?Widget for trailing section of each user.

Advance

For advanced-level customization, you can set custom views 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 own widget and then incorporate those into the widget.


setOptions

This method sets a predefined list of actions that users can perform when they long press a user in the list. These options typically include:

  • Muting notifications for a specific user.

By customizing these options, developers can provide a streamlined and contextually relevant user experience

CometChatUsers(
setOptions: (user, controller, context) {
// return options
},
)

addOptions

This method extends the existing set of actions available when users long press a user item. Unlike setOptionsDefines, which replaces the default options, addOptionsAdds allows developers to append additional actions without removing the default ones. Example use cases include:

  • Adding a "Report Spam" action.
  • Introducing a "Save to Notes" option.
  • Integrating third-party actions such as "Share to Cloud Storage".

This method provides flexibility in modifying user interaction capabilities.

CometChatUsers(
addOptions: (user, controller, context) {
// return options
},
)

loadingStateView

This method allows developers to set a custom loading view that is displayed when data is being fetched or loaded within the component. Instead of using a default loading spinner, a custom animation, progress bar, or branded loading screen can be displayed.

  • Showing a skeleton loader for users while data loads.
  • Displaying a custom progress indicator with branding.
  • Providing an animated loading experience for a more engaging UI.
CometChatUsers(
loadingStateView: (context) {
// return loading view
},
)

emptyStateView

Configures a custom view to be displayed when there are no users. This improves the user experience by providing meaningful content instead of an empty screen.

  • Displaying a message like "No users yet. Start a new chat!".
  • Showing an illustration or animation to make the UI visually appealing.
  • Providing a button to start a new user.
CometChatUsers(
emptyStateView: (context) {
// return empty view
},
)

errorStateView

Defines a custom error state view that appears when an issue occurs while loading users or messages. This enhances the user experience by displaying friendly error messages instead of generic system errors.

  • Showing "Something went wrong. Please try again." with a retry button.
  • Displaying a connection issue message if the user is offline.
  • Providing troubleshooting steps for the error.
CometChatUsers(
errorStateView: (context) {
// return error view
},
)

leadingView

This method allows developers to set a custom leading view element that appears at the beginning of each user item. Typically, this space is used for profile pictures, avatars, or user badges.

  • Profile Pictures & Avatars – Display user profile images with online/offline indicators.
  • Custom Icons or Badges – Show role-based badges (Admin, VIP, Verified) before the user name.
  • Status Indicators – Add an active status ring or colored border based on availability.
CometChatUsers(
leadingView: (context, user) {
// return leading view
},
)

titleView

This method customizes the title view of each user item, which typically displays the user’s name. It allows for styling modifications, additional metadata, or inline action buttons.

  • Styled Usernames – Customize fonts, colors, or text sizes for the name display.
  • Additional Metadata – Show extra details like username handles or job roles.
  • Inline Actions – Add a follow button or verification checkmark next to the name.
CometChatUsers(
titleView: (context, user) {
// return title view
},
)

trailingView

This method allows developers to customize the trailing (right-end) section of each user item, typically used for actions like buttons, icons, or extra information.

  • Quick Actions – Add a follow/unfollow button.
  • Notification Indicators – Show unread message counts or alert icons.
  • Custom Info Display – Display last active time or mutual connections.
CometChatUsers(
trailingView: (context, user) {
// return trailing view
},
)

ListItemView

With this function, you can assign a custom ListItem to the CometChatUsers Widget.

CometChatUsers(
listItemView: (user) {
return Placeholder();
},
)
Image

Example

Here is the complete example for reference:

custom_list_item.dart
 Widget getCustomUserListItem(User user) {
return CometChatListItem(
id: user.uid,
avatarName: user.name,
avatarURL: user.avatar,
avatarHeight: 40,
avatarWidth: 40,
title: user.name,
key: UniqueKey(),
statusIndicatorStyle: CometChatStatusIndicatorStyle(
border: Border.all(
width: 2,
color: Color(0xFFFFFFFF),
),
backgroundColor: Color(0xFF56E8A7),
),
hideSeparator: true,
style: ListItemStyle(
background: user.status == CometChatUserStatus.online
? const Color(0xFFE6F4ED)
: Colors.transparent,
titleStyle: TextStyle(
overflow: TextOverflow.ellipsis,
fontSize: 16,
fontWeight: FontWeight.w500,
color: Color(0xFF141414),
),
padding: EdgeInsets.only(
left: 16,
right: 16,
top: 8,
bottom: 8,
),
),
);
}
main.dart
CometChatUsers(
listItemView: (user) {
return getCustomUserListItem(user);
},
)

SubtitleView

You can customize the subtitle view for each item to meet your specific preferences and needs.

CometChatUsers(
subtitleView: (context, user) {
String subtitle = "";

final dateTime = user.lastActiveAt ?? DateTime.now();
subtitle = "Last Active at ${DateFormat('dd/MM/yyyy, HH:mm:ss').format(dateTime)}";

return Text(subtitle,
style: TextStyle(
color: Color(0xFF727272),
fontSize: 14,
fontWeight: FontWeight.w400,
),
);
},
)
Image

AppBarOptions

You can set the Custom AppBarOptions to the Conversations widget.

CometChatUsers(
appBarOptions: (context) => [
IconButton(
onPressed: () {},
icon: Icon(
Icons.add_comment_outlined,
color: Color(0xFF141414),
),
),
],
)
Image