Tried Adding Flutter chat in 2 hours
Tags: flutter, dart, uikit
Published: 2024-04-30 08:48:59

I wanted to create a chat app, but when I tried to build it from the beginning, I was at a loss and wondered what to do, then I came across Tencent Cloud Chat from Tencent Cloud. Since the document is mostly in English, I will upload how I created a chat app using Flutter Chat UIKit from scratch. I'm currently writing this guide while I'm making it.
[Tencent Cloud Chat](https://trtc.io/products/chat?source=flutter_devtoe&utm_campaign=social)
[Flutter Chat UIKit](https://www.tencentcloud.com/document/product/1047/45907?lang=en&pg=)
you can follow my steps to create a chat app like mine.

## Create Flutter app
If you already have the app, skip this part and go to step 2.
Flutter is a suite of application UI tools from Google that can be used on multiple platforms (iOS, Android, web, PC, etc.) with a single code. I'm also a beginner, so I followed [Flutter's beginner's guide](https://codelabs.developers.google.com/codelabs/flutter-codelab-first?hl=ko#1). 
I ran it on my Mac and followed the Flutter guide until step 4 [Add a button].

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82s76cxwxypfrb1ejp9l.png)

## Create Account
1. Go to [Console](https://console.trtc.io/), create an account, enter your personal information, and you're done. I'm going to use the free version, so there's no need to add a payment method. Tip: For personal development purposes, a Google account is faster.
2. Select Free trial and create an application.
Select the function you need among Call, Conference, and RTC Engine, and press [Create] to create a new application. I chose Call and it includes call and chat functions. It is good to remember the application's sdkappid and secretKey as they are important information for initialization and log in.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19xg24638lstwoenpzux.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ab9uxtryll6x4ccgqsa.png)

3. Create a test user.
I'm going to test the chat API, so I'll create two test users in the console in advance. Click the Application created earlier, click Chat, then go to User and create User1 and User2. 
In the image, you can press the buttons sequentially from left to right.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yacc3iz7xs4kxxxeogjn.png)
4. Package import.
Go back to the Flutter app you created earlier and type the following into the terminal.
`flutter pub add tencent_cloud_chat`
If a `TXIMSDK_Plus_Mac` problem occurred at this time and the app could not be run, run
`pod repo update`
Then import the necessary modules. I imported the contents below.

```shell
// message window
flutter pub add tencent_cloud_chat_message
  // History message list
flutter pub add tencent_cloud_chat_conversation
// friend list
flutter pub add tencent_cloud_chat_contact
// user profile
flutter pub add tencent_cloud_chat_user_profile
// group profile
flutter pub add tencent_cloud_chat_group_profile
```

## Coding
●Change `MaterialApp` to `TencentCloudChatMaterialApp`. (It allows you to use UIKit’s language settings, theme settings, etc.)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2pxpt8ri3v9b66fd8dc.png)

●Initialization
After changing `MyHomePage` to `StatefulWidget`, I initialized UIKit in the `initState` function.
The userSig of the user trying to log in was obtained from the console.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvcnp2vjknvgr8jcx5tq.png)

```Dart
TencentCloudChat.controller.initUIKit(
  options: const TencentCloudChatInitOptions(
    sdkAppID: , /// [Required]: The SDKAppID of your Tencent Cloud Chat application
    userID: , /// [Required]: The userID of the logged-in user
    userSig: , /// [Required]: The userSig of the logged-in user
  ),
  components: const TencentCloudChatInitComponentsRelated( /// [Required]: The modular UI components related settings, taking effects on a global scale.
    usedComponentsRegister: [
      /// [Required]: List of registration functions for the components used in the Chat UIKit.
          TencentCloudChatConversationManager.register,
          TencentCloudChatMessageManager.register,
          TencentCloudChatUserProfileManager.register,
          TencentCloudChatGroupProfileManager.register,
          TencentCloudChatContactManager.register,
      ],
  ),
);

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3179hnosugfvgein0sx.png)
●Chat page
First, I used Conversation (history message list) and Contact (friend list).
I changed the build content in the demo and pressed R (refresh)...

```Dart
List<Widget> pages = [
    const TencentCloudChatConversation(),
    const TencentCloudChatContact(),
  ];
  int currentIndex = 0;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        onTap: (index) async {
          if (index != currentIndex) {
            setState(
              () {
                currentIndex = index;
              },
            );
          }
        },
        items: const [
          BottomNavigationBarItem(
              icon: Icon(Icons.chat_bubble_outline), label: "Chats"),
          BottomNavigationBarItem(
              icon: Icon(Icons.contacts), label: "Contacts"),
        ],
      ),
      body: pages[currentIndex],
    );
  }

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kegs95nz10hoy4cm96n.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5r3u7jvr3jlh8rng92c2.png)
●Add friends
First, user1 cannot send a message because he has no friends, so add user2 as a friend.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3ika6d9gk01ba9awee0.png)
After adding, user2 appears in the contact list. If you fail to add friends, check user2's add friend option in the console.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amn7mp7umb7n6t4bai6z.png)
●Send message
Last but not least, send the most important message. If you only use iOS or Android, you can immediately click on the friend list to return to the send message screen. However, since I am developing a PC platform, I had to add the information below to proceed to the send message window.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6abkl22s8tz537fo68e8.png)
Since the conversation and message parts of a PC are combined, when you go to the conversation page, the chat screen also appears immediately. At this time, you have to return false; to process other logic besides moving to the conversation page I wrote. The fact is that return true does not process any logic!
Now you can complete a simple chat app!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26n6ggw9eevlk559drt2.png)
Likewise, if you log in as user2, you can view messages sent by user1.
To be exact, it took me less than half a day to write this article.
If you want to create a quick, easy, and simple multi-platform chat app, I hope you take a look at this.
addition:
Change language and theme (supports Japanese, English, Arabic, Chinese, etc., supports dark mode)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7j3kufcx5iiudtvs65n.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zmuv94iwvyx2teyiczv.png)
In addition to this, you can easily use dark mode and detailed customization, so it would be a good idea to refer to this document.
It is said that there is a complete demo that has already been completed, so it might be a good idea to refer to it.
[demo link](https://github.com/TencentCloud/chat-demo-flutter/tree/v2)