Skip to main content

Groups

Overview

CometChatGroups functions as a standalone Widget designed to create a screen displaying a list of groups, with the added functionality of enabling users to search for specific groups. Acting as a container widget, CometChatGroups encapsulates and formats the CometChatListBase and CometChatGroupList widgets without introducing any additional behavior of its own.

Image

The CometChatGroups widget is composed of the following BaseWidget:

WidgetsDescription
CometChatListBaseCometChatListBase serves as a container widget equipped with a title (navigationBar), search functionality (search-bar), background settings, and a container for embedding a list view.
CometChatListItemThis widget renders information extracted from a Group object onto a tile, featuring a title, subtitle, leading view, and trailing view.

Usage

Integration

As CometChatGroups is a custom widget, it can be launched directly by user actions such as button clicks or other interactions.

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

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


State<Groups> createState() => _GroupsState();
}

class _GroupsState extends State<Groups> {


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

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. onItemTap

This method proves valuable when users seek to override onItemTap functionality within CometChatGroups, empowering them with greater control and customization options.

The onItemTap action doesn't have a predefined behavior. You can override this action using the following code snippet.

CometChatGroups(
onItemTap: (context, group) {
// TODO("Not yet implemented")
},
)

2. onBack

Enhance your application's functionality by leveraging the onBack feature. This capability allows you to customize the behavior associated with navigating back within your app. Utilize the provided code snippet to override default behaviors and tailor the user experience according to your specific requirements.

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

3. onError

You can customize this behavior by using the provided code snippet to override the onError and improve error handling.

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

4. onItemLongPress

This method becomes invaluable when users seek to override long-click functionality within CometChatGroups, offering them enhanced control and flexibility in their interactions.

The onItemLongPress action doesn't have a predefined behavior. You can override this action using the following code snippet.

CometChatGroups(
onItemLongPress: (context, group) {
// TODO("Not yet implemented")
},
)

5. onSelection

When the onSelection event is triggered, it furnishes the list of selected groups. 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 groups.

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.

CometChatGroups(
selectionMode: SelectionMode.multiple,
activateSelection: ActivateSelection.onClick,
onSelection: (groupList) {
// TODO("Not yet implemented")
}
)

Filters

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

1. GroupsRequestBuilder

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

CometChatGroups(
groupsRequestBuilder: GroupsRequestBuilder()
..limit = 10
)
PropertyDescriptionCode
Joined OnlyFlag to include only joined groups. Defaults to false.joinedOnly: bool
LimitNumber of results to limit the query.limit: int
Search KeywordKeyword for searching groups.searchKeyword: String
TagsTags for filtering groups.tags: List<String>
With TagsFlag to include tags in the results. Defaults to false.withTags: bool

Events

Events are emitted by a CometChatGroups 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 list of events emitted by the CometChatGroups widget is as follows.

EventsDescription
ccGroupCreatedThis gets triggered when the logged in user creates a group.
ccGroupDeletedThis gets triggered when the logged in user deletes a group.
ccGroupLeftThis gets triggered when the logged in user leaves a group.
ccGroupMemberScopeChangedThis gets triggered when the logged in user changes the scope of another group member.
ccGroupMemberBannedThis gets triggered when the logged in user bans a group member from the group.
ccGroupMemberKickedThis gets triggered when the logged in user kicks another group member from the group.
ccGroupMemberUnbannedThis gets triggered when the logged in user unbans a user banned from the group.
ccGroupMemberJoinedThis gets triggered when the logged in user joins a group.
ccGroupMemberAddedThis gets triggered when the logged in user adds new members to the group.
ccOwnershipChangedThis gets triggered when the logged in user transfers the ownership of their group to some other member.
your_screen.dart
import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart'; 
import 'package:cometchat_sdk/models/action.dart' as cc;
import 'package:flutter/material.dart';

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


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

class _YourScreenState extends State<YourScreen> with CometChatGroupEventListener {


void initState() {
super.initState();
CometChatGroupEvents.addGroupsListener("listenerId", this); // Add the listener
}


void dispose(){
super.dispose();
CometChatGroupEvents.removeGroupsListener("listenerId"); // Remove the listener
}



void ccGroupCreated(Group group) {
// TODO("Not yet implemented")
}


void ccGroupDeleted(Group group) {
// TODO("Not yet implemented")
}


void ccGroupLeft(cc.Action message, User leftUser, Group leftGroup) {
// TODO("Not yet implemented")
}


void ccGroupMemberScopeChanged(cc.Action message, User updatedUser, String scopeChangedTo, String scopeChangedFrom, Group group) {
// TODO("Not yet implemented")
}


void ccGroupMemberBanned(cc.Action message, User bannedUser, User bannedBy, Group bannedFrom) {
// TODO("Not yet implemented")
}


void ccGroupMemberKicked(cc.Action message, User kickedUser, User kickedBy, Group kickedFrom) {
// TODO("Not yet implemented")
}


void ccGroupMemberUnbanned(cc.Action message, User unbannedUser, User unbannedBy, Group unbannedFrom) {
// TODO("Not yet implemented")
}


void ccGroupMemberJoined(User joinedUser, Group joinedGroup) {
// TODO("Not yet implemented")
}


void ccGroupMemberAdded(List<cc.Action> messages, List<User> usersAdded, Group groupAddedIn, User addedBy) {
// TODO("Not yet implemented")
}


void ccOwnershipChanged(Group group, GroupMember newOwner) {
// TODO("Not yet implemented")
}


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

}

Customization

To fit your app's design requirements, you can customize the appearance of the groups 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. Groups Style

Enhance your CometChatGroups Widget by setting the GroupsStyle to customize its appearance.

CometChatGroups(
groupsStyle: GroupsStyle(
background: Color(0xFFE4EBF5),
titleStyle: TextStyle(color: Colors.red),
backIconTint: Colors.red
),
)
Image

List of properties exposed by GroupsStyle

PropertyDescriptionCode
Back Icon TintProvides color for the back iconbackIconTint: Color?
Empty Text StyleProvides styling for text to indicate group list is emptyemptyTextStyle: TextStyle?
Error Text StyleProvides styling for text to indicate some error has occurrederrorTextStyle: TextStyle?
Loading Icon TintProvides color to loading iconloadingIconTint: Color?
Password Group Icon BackgroundProvides color to the icon for password protected grouppasswordGroupIconBackground: Color?
Private Group Icon BackgroundProvides color to the icon for private groupprivateGroupIconBackground: Color?
Search BackgroundProvides color for the search boxsearchBackground: Color?
Search Border ColorProvides color for the border around the search boxsearchBorderColor: Color?
Search Border RadiusProvides radius for the border around the search boxsearchBorderRadius: double?
Search Border WidthProvides width to the border around the search boxsearchBorderWidth: double?
Search Icon TintProvides color for the search iconsearchIconTint: Color?
Search Placeholder StyleProvides styling for the hint text inside the search boxsearchPlaceholderStyle: TextStyle?
Search Text StyleProvides styling for text inside the search boxsearchTextStyle: TextStyle?
Selection Icon TintSet selection icon colorselectionIconTint: Color?
Submit Icon TintSet submit icon tintsubmitIconTint: Color?
Subtitle Text StyleProvides styling to the text in the subtitlesubtitleTextStyle: TextStyle?
Title StyleProvides styling for title texttitleStyle: TextStyle?
2. Avatar Style

To apply customized styles to the Avatar widget in the CometChatGroups widget, you can use the following code snippet. For further insights on Avatar Styles refer

CometChatGroups(
avatarStyle: AvatarStyle(
border: Border.all(width: 5),
borderRadius: 20,
background: Colors.red
),
)

3. StatusIndicator Style

To apply customized styles to the Status Indicator widget in the CometChatGroups widget, You can use the following code snippet. For further insights on Status Indicator Styles refer

CometChatGroups(
statusIndicatorStyle: StatusIndicatorStyle(
borderRadius: 10,
gradient: LinearGradient(colors: [Colors.red, Colors.orange], begin: Alignment.topLeft, end: Alignment.bottomRight)
),
)

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

List of properties exposed by CometChatGroups

PropertyDescriptionCode
Activate SelectionLets the widget know if groups are allowed to be selectedactivateSelection: ActivateSelection?
Back ButtonBack buttonbackButton: Widget?
ControllerSets controller for the listcontroller: ScrollController?
Controller TagGroup tag to create from, if this is passed it's parent responsibility to close thiscontrollerTag: String?
Empty State TextText to be displayed when the list is emptyemptyStateText: String?
Error State TextText to be displayed when error occurserrorStateText: String?
Hide AppbarToggle visibility for app barhideAppbar: bool?
Hide ErrorToggle visibility of error dialoghideError: bool?
Hide SearchSwitch on/off search inputhideSearch: bool
Hide SeparatorhideSeparator: bool
Password Group IconSets icon in status indicator for password grouppasswordGroupIcon: Widget?
Private Group IconSets icon in status indicator for private groupprivateGroupIcon: Widget?
Search Box IconSearch iconsearchBoxIcon: Widget?
Search PlaceholderPlaceholder text of search inputsearchPlaceholder: String?
Selection IconChange selection iconselectionIcon: Widget?
Selection ModeSpecifies mode groups module is opening inselectionMode: SelectionMode?
Submit IconOverride the default submit iconsubmitIcon: Widget?
ThemeCan pass custom themetheme: CometChatTheme?
TitleSets title for the listtitle: String?

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.


ListItemView

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

CometChatGroups(
listItemView: (group) {
return Placeholder(); // Replace this placeholder with your custom widget.
},
)

Example

Here is the complete example for reference:

custom_list_item.dart
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../helper/utils/custom_colors.dart';

class CustomListItems extends StatelessWidget {
final String name;
final DateTime? lastMessageTime;
final String? avatarUrl;

const CustomListItems({
super.key,
required this.name,
this.lastMessageTime,
this.avatarUrl,
});

String formatDateTime(DateTime dateTime) {
final now = DateTime.now();
final difference = now.difference(dateTime);

if (difference.inDays == 0) {
return DateFormat('HH:mm').format(dateTime);
} else if (difference.inDays == 1) {
return 'Yesterday';
} else {
return DateFormat('yyyy-MM-dd').format(dateTime);
}
}


Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(top: 5, bottom: 5),
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFF6851D6), width: 1), // Example border color
borderRadius: BorderRadius.circular(8.0),
color: Color(0xFFEEEEEE)
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
name,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
lastMessageTime == null ? Container() : Text(formatDateTime(lastMessageTime!)),
],
),
),
const SizedBox(width: 8.0),
if (avatarUrl != null)
ClipOval(
child: Image.network(
avatarUrl!,
width: 40.0,
height: 40.0,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return const Icon(
Icons.person,
size: 40.0,
);
},
),
)
else
const Icon(
Icons.person,
size: 40.0,
),
],
),
);
}
}
main.dart

Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child:
CometChatGroups(
listItemView: (group) {
return CustomListItems(
name: group.name,
avatarUrl: group.icon,
);
},
)
)
);
}
Image

SubtitleView

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

CometChatGroups(
subtitleView: (context, conversation) {
return const Row(
children: [
Icon(Icons.call, color: Color(0xFF6851D6), size: 25,),
SizedBox(width: 10),
Icon(Icons.video_call, color: Color(0xFF6851D6), size: 25,),
],
);
},
)
Image

AppBarOptions

You can set the Custom appBarOptions to the CometChatGroups widget.

CometChatGroups(
appBarOptions: (context) {
return [
InkWell(
onTap: () {
// TODO("Not yet implemented")
},
child: const Icon(Icons.ac_unit, color: Color(0xFF6851D6)),
),
const SizedBox(width: 10)
];
},
)
Image

EmptyStateView

You can set a custom EmptyStateView using emptyStateView to match the empty UI of your app.

CometChatGroups(
emptyStateView: (context) {
return const Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Spacer(),
Icon(Icons.error_outline, color: Colors.red, size: 100,),
SizedBox(height: 20,),
Text("Your Custom Error Message"),
Spacer(),
],
);
},
)
Image

ErrorStateView

You can set a custom ErrorStateView using errorStateView to match the error UI of your app.

CometChatGroups(
errorStateView: (context) {
return SizedBox(
width: MediaQuery.of(context).size.width,
child: const Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Spacer(),
Icon(Icons.error_outline, color: Colors.red, size: 100,),
SizedBox(height: 20,),
Text("Your Custom Error Message"),
Spacer(),
],
),
);
},
)
Image

LoadingStateView

You can set a custom LoadingStateView using loadingStateView to match the loading UI of your app.

CometChatGroups(
errorStateView: (context) {
return SizedBox(
width: MediaQuery.of(context).size.width,
child: const Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Spacer(),
Icon(Icons.error_outline, color: Colors.red, size: 100,),
SizedBox(height: 20,),
Text("Your Custom Error Message"),
Spacer(),
],
),
);
},
)
Image