•2 months ago
We all have probably written chat apps multiple times in our lives, and we all know that it takes time! So I always wondered if there is a way to make a custom and beautiful chat app using some lib/service that does all the heavy lifting for me.
A few weeks ago, I came across Stream, a chat service for quickly adding interactive messaging to your application. Upon a closer look, I was blown away when I saw they support Flutter and provide pre-built widgets!
Fully blown chat app in a matter of hours? FOR FREE? Are you kidding me? 😀
After 6 to 7 hours of trying out their library, I was able to create the following chat preview:
Out of the box, you can use their predefined widgets for standard chat components like channel items (inbox or message item), messages/ threads (single channel or group), and message input for quickly prototyping and building a Flutter chat.
While their pre-made widgets are great, your product manager would probably not be OK with that :). Chat, like every part of an app, has to follow a particular style, which ties nicely to the objective of this article:
Creating custom widgets that developers can quickly incorporate in Stream's chat library 🙂
Before we start working on UI, you need to setup chat client and at least one user, and here they explain how: OFFICIAL TUTORIAL.
Also, take a look at their Github readme and example, for some useful info about permissions etc.
Channel is a group of 1+ users, by default, so you can get very creative.
More info about creating different types of channels can be found HERE.
For the sake of this example, I've created ChatData singleton where I create a client and 2 users, that can be found HERE. Configuration such as this should be done on your back-end side. For the purposes of this tutorial, it is fine.
We will be using the following as our starting point:
StreamChat widget needs to be high as up as possible in our application widget tree, since it will hold our application's
StreamChatClient, a class used for handling all the api calls, websocket connections etc. Also included in the default Stream chat widget is
StreamChatThemeData, our best firend when it comes to customizing styles, colors and the appearance of our app.
We can start customizing our application by chaning properties in our
StreamChatThemeData. My class looks like the following:
It's pretty self explanatory.
Now, when we have the core structure set, let's design 🙂
List of channels & channel item
If you are confused by the code above, don't worry, it is actually very simple. Let's break it down!
ChannelsBloc is a widget which takes care (holds instance and performs operations) on list of channels, channel subscriptions, querying, sorting etc.
ChannelsBloc needs to be below the
StreamChat widget in the element tree, 'cos it uses
StreamChatClient to fetch data.
ChannelsBloc is always accessible via current context(if your context is below
ChannelsBloc in the element tree!):
1ChannelsBlocState channelsBloc = ChannelsBloc.of(context);
The code above finds nearest
ChannelsBlocState in the element tree, providing us access to our list of channels, their associated stream etc.
ChannelListView is a Stream's Flutter widget used for displaying channel, Stream's equivalent of groups or DMs. Out of the box, it handles pagination, filtering, loading, etc. There are also builders and properties allowing you to easily add custom message widgets, custom separators, and lots more.
All data needed to draw messages, title, last message time, etc can be fetched through the channel instance via a builder:
With the boring background information out of the way, let's create our very own custom channel item:
Let us breakdown the above code. When a new unread message arrives, we cross fade initial view, into a colored one. The
StreamBuilder listens to the changes in the
unreadCountStream provided by Stream's SDK which we use to determine whether the channel has unread messages. Based on this value, we can selectively call our
ChannelView with the appropriate boolean value.
Using a boolean parameter allows us to chage the UI for the channel. If we have new (unread) messages it's colored, with light text, and if not, it's plain white, with dark text.
Here is what it looks like in slow motion:
Complete code for the
ListOfChannels (list of channel items) can be found HERE.
Channel(Group or Message) Thread
When user clicks on channel item, we open a new screen with messages -
ChannelThread, a custom message view built and styled entirely by us.
ChannelsBloc, when access a channel, we need to wrap our widget in a
StreamChannel widget. This widget serves a similar purpose to
ChannelsBloc, it allows us to quickly access the current channel instance via an inherited widget/context lookup:
ChannelThread widget, we fetch nearest channel UP in the element tree using the build context...and as you probably already know, context is a node in the tree of elements 😉.
ChannelThread (header, list of messages and input field) code can be found HERE, but's lets go through the code in the following sections.
The channel header is just an
AppBar in the applciation
Scaffold, that contains of the
BackButton (does only the popping),
ChannelName, a package component that fetches channel from the element tree from the
StreamChannel widget we defined above and a
ChannelImage also provided to us by the Stream chat package. In the true spirit of stying and customization we add some shadow to it 🙂
The interesting thing about
ChannelImage widget is it allows us to optionally override the the channel. If no channel is provided, it automatically fetches the nearest instance of
Channel from the element tree.
For those curious, here is a look at the constructor and basic implementation:
💡: Check how they implemented those widgets, it can be used in many different ways 🙂
List of Messages
MessageListView is also a package widget that requires a
StreamChannel widget UP in the element tree to fetch and display messages.
Let's have some fun by changing with default chat bubble with a custom one(complete _buildCustomBubble method can also be found HERE):
In the above code, we make comparison of current user id and message user id. If the id is the same, that means that our current user has created this message, and we'll place its message right on the screen:
The same logic is used for choosing bubble color and layout(if message user is current user, we first display text, then avatar, and if not, it's reversed).
rowItems are place within a Row Widget:
With the majority of our chat application complete, the only thing missing is a field for sending messages. Lucky for us, we can use the default
MessageInput as a starting point.
As you can probably guess by now, we can style this widget using the
StreamChatTheme defined in the
StreamChat Widget in the root of our app.
There, you can change input text color, size, background color, input field borders and radius.. and lots more.
If your company has five or less team members, less than $10k in monthly revenue and have taken less than $100k in funding, Stream is free/gratis/complimentary.
Happy Fluttering 💙