2024-01-12 16:52:14 +01:00
# Flutter Chat
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
Flutter Chat is a package which gives the possibility to add a (personal or group) chat to your Flutter-application.
By default this package adds support for a Firebase back-end but you can also add a custom back-end (like a Websocket-API) by extending the `ChatInterface` interface from the `flutter_chat_interface` package.
2022-10-14 15:00:22 +02:00
2024-01-12 16:52:14 +01:00

2023-06-29 15:57:42 +02:00
2025-02-17 12:00:40 +01:00
The default UI is based on a Figma design that defines this component, however it's only accessible by Iconica developers:
[Figma design ](https://www.figma.com/file/4WkjwynOz5wFeFBRqTHPeP/Iconica-Design-System?type=design&node-id=357%3A3342&mode=design&t=XulkAJNPQ32ARxWh-1 ).
There is also a Figma clickable prototype that demonstrates this component:
[Figma clickable prototype)[https://www.figma.com/proto/PRJoVXQ5aOjAICfkQdAq2A/Iconica-User-Stories?page-id=1%3A2& type=design& node-id=56-6837& viewport=279%2C2452%2C0.2& t=E7Al3Xng2WXnbCEQ-1& scaling=scale-down& starting-point-node-id=56%3A6837& mode=design]
2023-12-14 14:28:16 +01:00
2022-10-14 15:00:22 +02:00
## Setup
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
To use this package, add flutter_chat as a dependency in your `pubspec.yaml` file:
2022-10-14 15:00:22 +02:00
2025-02-17 12:00:40 +01:00
```yaml
2024-01-12 16:52:14 +01:00
flutter_chat:
2023-12-29 12:02:54 +01:00
git:
2024-01-23 16:30:19 +01:00
url: https://github.com/Iconica-Development/flutter_chat
path: packages/flutter_chat
2022-10-14 15:00:22 +02:00
```
2024-04-23 09:57:13 +02:00
You can use the `LocalChatService` to test the package in your project:
2025-02-17 12:00:40 +01:00
```dart
2024-04-23 09:57:13 +02:00
ChatUserStoryConfiguration(
chatService: LocalChatService(),
),
```
2025-02-17 12:00:40 +01:00
If you are going to use Firebase as the back-end of the Chat, you should also add the following package as a dependency to your `pubspec.yaml` file:
2022-10-14 15:00:22 +02:00
2025-02-17 12:00:40 +01:00
```yaml
2024-01-12 16:52:14 +01:00
flutter_chat_firebase:
2023-12-29 12:02:54 +01:00
git:
2024-01-23 16:30:19 +01:00
url: https://github.com/Iconica-Development/flutter_chat
path: packages/flutter_chat_firebase
2022-10-14 15:00:22 +02:00
```
2025-02-17 12:00:40 +01:00
Create a Firebase project for your application and add Firebase Firestore and Storage.
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
Make sure you are authenticated using the `firebase_auth` package or adjust your firebase rules, otherwise you won't be able to retreive data.
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
Also make sure you have the corresponding collections in your Firebase project as defined in `FirebaseChatOptions` , you can override the
2024-04-23 09:57:13 +02:00
default paths as you wish:
2025-02-17 12:00:40 +01:00
```dart
2024-04-23 09:57:13 +02:00
const FirebaseChatOptions({
this.groupChatsCollectionName = 'group_chats',
this.chatsCollectionName = 'chats',
this.messagesCollectionName = 'messages',
this.usersCollectionName = 'users',
this.chatsMetaDataCollectionName = 'chat_metadata',
this.userChatsCollectionName = 'chats',
});
```
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
Also the structure of your data should be equal to our predefined models, you can implement any model by making your own model and implementing one of the predefined interfaces like so:
```dart
2024-01-19 09:23:00 +01:00
class ChatMessageModel implements ChatMessageModelInterface {
ChatMessageModel({
required this.sender,
required this.timestamp,
});
@override
final ChatUserModel sender;
2025-02-17 12:00:40 +01:00
2024-01-19 09:23:00 +01:00
@override
final DateTime timestamp;
}
```
2025-02-17 12:00:40 +01:00
The various interfaces you can implement:
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
- `ChatUserModelInterface` ,
- `ChatImageMessageModelInterface` ,
- `ChatTextMessageModelInterface`
- `ChatMessageModelInterface` ,
- `ChatModelInterface` ,
- `GroupChatModelInterface` ,
- `PersonalChatModelInterface` ,
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
Add the following to your project to use the camera or photo library to send pictures:
2024-01-12 16:52:14 +01:00
For ios add the following lines to your info.plist:
2025-02-17 12:00:40 +01:00
```plist
< key > NSCameraUsageDescription< / key >
< string > Access camera< / string >
< key > NSPhotoLibraryUsageDescription< / key >
< string > Library< / string >
2024-01-12 16:52:14 +01:00
```
For android add the following lines to your AndroidManifest.xml:
2025-02-17 12:00:40 +01:00
```xml
2024-01-12 16:52:14 +01:00
< uses-permission android:name = "android.permission.CAMERA" / >
< uses-permission android:name = "android.permission.GALLERY" / >
```
2022-10-14 15:00:22 +02:00
## How to use
2025-02-17 12:00:40 +01:00
To use the module within your Flutter-application with predefined `go_router` routes you should add the following:
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
Add go_router as dependency to your project, then add the following configuration to your flutter_application:
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
```dart
2024-01-12 16:52:14 +01:00
List< GoRoute > getChatRoutes() => getChatStoryRoutes(
ChatUserStoryConfiguration(
chatService: chatService,
2023-12-29 12:02:54 +01:00
chatOptionsBuilder: (ctx) => const ChatOptions(),
),
);
```
2024-01-19 09:23:00 +01:00
You can override any method in the `ChatUserStoryConfiguration` .
2024-01-12 16:52:14 +01:00
Add the `getChatRoutes()` to your go_router routes like so:
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
```dart
2023-12-29 12:02:54 +01:00
final GoRouter _router = GoRouter(
routes: < RouteBase > [
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return const MyHomePage(
title: "home",
);
},
),
2024-01-12 16:52:14 +01:00
...getChatRoutes()
2023-12-29 12:02:54 +01:00
],
);
2022-10-14 15:00:22 +02:00
```
2023-12-29 12:02:54 +01:00
2024-01-19 09:23:00 +01:00
The routes that can be used to navigate are:
For routing to the `ChatScreen` :
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
```dart
2024-01-19 09:23:00 +01:00
static const String chatScreen = '/chat';
```
For routing to the `ChatDetailScreen` :
2025-02-17 12:00:40 +01:00
```dart
2024-01-19 09:23:00 +01:00
static String chatDetailViewPath(String chatId) => '/chat-detail/$chatId';
static const String chatDetailScreen = '/chat-detail/:id';
```
For routing to the `NewChatScreen` :
2025-02-17 12:00:40 +01:00
```dart
2024-01-19 09:23:00 +01:00
static const String newChatScreen = '/new-chat';
```
For routing to the `ChatProfileScreen` :
you can see the information about a person or group you started a chat with.
If the userId is null a group profile screen will be shown otherwise the profile of a single person will be shown.
2025-02-17 12:00:40 +01:00
```dart
2024-01-19 09:23:00 +01:00
static String chatProfileScreenPath(String chatId, String? userId) =>
'/chat-profile/$chatId/$userId';
static const String chatProfileScreen = '/chat-profile/:id/:userId';
```
2025-02-17 12:00:40 +01:00
Add the following code to the build-method of a chosen widget to use the module within your Flutter-application without predefined `go_router` routes but with Navigator routes:
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
```dart
2024-01-19 09:23:00 +01:00
chatNavigatorUserStory(
ChatUserStoryConfiguration(
chatService: ChatService,
chatOptionsBuilder: (ctx) => const ChatOptions(),
),
context,
);
```
2025-02-17 12:00:40 +01:00
Just like with the `go_router` routes you can override any methods in the `ChatUserStoryConfiguration` .
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
Or create your own routing using the screens.
2023-12-29 12:02:54 +01:00
2025-02-17 12:00:40 +01:00
Add the following code to add the `ChatScreen` :
```dart
2023-12-29 12:02:54 +01:00
ChatScreen(
options: options,
onPressStartChat: onPressStartChat,
onPressChat: onPressChat,
onDeleteChat: onDeleteChat,
service: service,
pageSize: pageSize,
);
2022-10-14 15:00:22 +02:00
```
2024-01-12 16:52:14 +01:00
The `ChatDetailScreen` shows the messages that are in the current chat you selected.
2023-12-29 12:02:54 +01:00
To add the `ChatDetailScreen` add the following code:
2025-02-17 12:00:40 +01:00
```dart
2023-12-29 12:02:54 +01:00
ChatDetailScreen(
options: options,
onMessageSubmit: onMessageSubmit,
onUploadImage: onUploadImage,
onReadChat: onReadChat,
service: service,
);
```
2022-10-14 15:00:22 +02:00
2024-01-12 16:52:14 +01:00
On the `NewChatScreen` you can select a person to chat.
2022-10-14 15:00:22 +02:00
2025-02-17 12:00:40 +01:00
Add the following coe to add the `NewChatScreen` :
```dart
2023-12-29 12:02:54 +01:00
NewChatScreen(
options: options,
onPressCreateChat: onPressCreateChat,
service: service,
);
2022-10-14 15:00:22 +02:00
```
2024-01-19 09:23:00 +01:00
On the `ChatProfileScreen` you can see the information about a person or group you started a chat with.
2025-02-17 12:00:40 +01:00
A group profile screen will be shown if the userId is null, otherwise the profile of a single person will be shown.
2024-01-19 09:23:00 +01:00
2025-02-17 12:00:40 +01:00
```dart
2024-01-19 09:23:00 +01:00
ChatProfileScreen(
chatService: chatservice,
chatId: chatId,
translations: translations,
onTapUser: onTapUser,
userId: userId,
);
```
2025-02-17 12:00:40 +01:00
The `ChatEntryWidget` is a widget you can put anywhere in your app, it displays the amount of unread messages you currently have.
You can choose to add an `onTap` to the `ChatEntryWidget` so it routes to the `ChatScreen` .
2024-01-12 16:52:14 +01:00
2025-02-17 12:00:40 +01:00
Add the following code to add the `ChatEntryWidget` :
2024-01-12 16:52:14 +01:00
2025-02-17 12:00:40 +01:00
```dart
2024-01-12 16:52:14 +01:00
ChatEntryWidget(
chatService: chatService,
onTap: onTap,
);
```
2022-10-14 15:00:22 +02:00
The `ChatOptions` has its own parameters, as specified below:
| Parameter | Explanation |
|-----------|-------------|
2023-12-29 12:02:54 +01:00
| newChatButtonBuilder | Builds the 'New Chat' button, to initiate a new chat session. This button is displayed on the chat overview. |
| messageInputBuilder | Builds the text input which is displayed within the chat view, responsible for sending text messages. |
| chatRowContainerBuilder | Builds a chat row. A row with the users' avatar, name and eventually the last massage sended in the chat. This builder is used both in the _chat overview screen_ as in the _new chat screen_ . |
| imagePickerContainerBuilder | Builds the container around the ImagePicker. |
2022-10-14 15:00:22 +02:00
| closeImagePickerButtonBuilder | Builds the close button for the Image Picker pop-up window. |
2023-12-29 12:02:54 +01:00
| scaffoldBuilder | Builds the default Scaffold-widget around the Community Chat. The chat title is displayed within the Scaffolds' title for example. |
2022-10-14 15:00:22 +02:00
2023-12-29 12:02:54 +01:00
The `ImagePickerTheme` also has its own parameters, how to use these parameters can be found in [the documentation of the flutter_image_picker package ](https://github.com/Iconica-Development/flutter_image_picker ).
2022-10-14 15:00:22 +02:00
## Issues
2024-01-12 16:52:14 +01:00
Please file any issues, bugs or feature request as an issue on our [GitHub ](https://github.com/Iconica-Development/flutter_chat/pulls ) page. Commercial support is available if you need help with integration with your app or services. You can contact us at [support@iconica.nl ](mailto:support@iconica.nl ).
2022-10-14 15:00:22 +02:00
## Want to contribute
2024-02-21 14:18:18 +01:00
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our [contribution guide ](./CONTRIBUTING.md ) and send us your [pull request ](https://github.com/Iconica-Development/flutter_chat/pulls ).
2022-10-14 15:00:22 +02:00
## Author
2024-01-12 16:52:14 +01:00
This `flutter_chat` for Flutter is developed by [Iconica ](https://iconica.nl ). You can contact us at < support @ iconica . nl >