a complete flutter chat ui kit
this flutter package provides you with a base structure as well as a set of tools that allow you to quickly build up a modern chat ui. instead of trying to come up with all in one widgets that meet everyone’s expectations, you are given helpers that are meant to be used as examples and customized at will.
features
- support for group conversations
- list of conversations (chats)
- group avatar
- unread messages badge
- message title (user)
- message avatar
- adaptive message containers depending on relative positioning
- controllers with data management api (add, remove, update)
- automated selection management
- message input with typing events
- examples for text, image, audio and video messages
getting started
first add the following line to your pubspec.yaml dependencies:
chat_ui_kit: ^[latest_version]
next, setup your models to extend the base models:
class chatmessage extends messagebase {
//...
chatuser author;
string text;
@override
datetime get createdat =>
datetime.frommillisecondssinceepoch(creationtimestamp);
@override
string get id => messageid;
@override
string get url => attachment;
@override
messagebasetype get messagetype {
//...
}
}
class chatuser extends userbase {
//...
@override
string get name => username;
@override
string get id => userid;
@override
string get avatar => avatarurl;
}
class chat extends chatbase {
//...
list<chatuser> members;
chatmessage lastmessage;
@override
int get unreadcount => chat.unreadcount;
@override
string get name {
if ((chat?.name ?? null).isnotnullorempty()) return chat.name;
return memberswithoutself.map((e) => e.username).tolist().join(", ");
}
@override
string get id => chat?.id;
@override
list<chatuser> get memberswithoutself {
list<chatuser> memberswithoutself = [];
final localuserid = chat?.localuserid ??
firebaseauth.instance.currentuser?.uid;
for (chatuser chatuser in members) {
if (localuserid != chatuser.userid) memberswithoutself.add(chatuser);
}
return memberswithoutself;
}
}
chatslistscreen
chatslistcontroller controller = chatslistcontroller();
chatslist(
controller: _controller,
appuserid: _currentuser.id,
scrollhandler: _handlescrollevent,
groupavatarstyle: groupavatarstyle(withseparator: true),
builders: chatslisttilebuilders(
groupavatarbuilder:
(context, imageindex, itemindex, size, item) {
final chat = item as chat;
return cachednetworkimage(
cachemanager: customcachemanager(),
imageurl: chat.memberswithoutself[imageindex].avatar,
width: size.width,
height: size.height,
fit: boxfit.cover,
errorwidget: (ctx, url, val) =>
apperrorwidget(true, size: size));
},
lastmessagebuilder: _buildlastmessage,
wrapper: _buildtilewrapper,
datebuilder: (context, date) => padding(
padding: edgeinsets.only(left: 16),
child: text(dateformatter.getverbosedatetimerepresentation(
context, date)))))
@override
void dispose() {
_controller.dispose();
super.dispose();
}
chatscreen
final messageslistcontroller _controller = messageslistcontroller();
@override
void initstate() {
_controller.selectioneventstream.listen((event) {
setstate(() {
_selecteditemscount = event.currentselectioncount;
});
});
super.initstate();
}
widget _buildmessageslist() {
incomingmessagetilebuilders incomingbuilders = _isgroupchat
? incomingmessagetilebuilders(
bodybuilder: (context, index, item, messageposition) =>
_buildmessagebody(context, index, item, messageposition,
messageflow.incoming),
avatarbuilder: (context, index, item, messageposition) {
final _chatmessage = item as chatmessage;
return padding(
padding:
edgeinsets.only(right: 16),
child: _buildavatarwithscore(_chatmessage.author));
})
: incomingmessagetilebuilders(
bodybuilder: (context, index, item, messageposition) =>
_buildmessagebody(context, index, item, messageposition,
messageflow.incoming),
titlebuilder: null);
return expanded(
child: messageslist(
controller: _controller,
appuserid: _currentuser.id,
usecustomtile: (i, item, pos) {
final msg = item as chatmessage;
return msg.istypeevent;
},
messageposition: _messageposition,
builders: messagetilebuilders(
customtilebuilder: _buildeventmessage,
customdatebuilder: _builddate,
incomingmessagebuilders: incomingbuilders,
outgoingmessagebuilders: outgoingmessagetilebuilders(
bodybuilder: (context, index, item, messageposition) =>
_buildmessagebody(context, index, item,
messageposition, messageflow.outgoing)))));
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Comments are closed.