Flutter即时通讯插件mesibo_flutter_sdk的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter即时通讯插件mesibo_flutter_sdk的使用

mesibo 是一个流行的开发者平台,用于在您的应用程序中添加实时消息传递、通话、会议和聊天机器人功能。

特性

  • 一对一消息和群聊
  • 高质量语音和视频通话及会议
  • 丰富的消息传递(文本、图片、视频、音频及其他文件)
  • 端到端加密
  • 位置共享
  • 消息状态和打字指示器
  • 在线状态(存在)和实时个人资料更新
  • 推送通知

文档与教程


示例代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mesibo_flutter_sdk/mesibo.dart';

import 'dart:developer';

/**************************************************************************************************
 This demo application implements real-time messaging, calls, and conferencing capabilities in a 
 Flutter application. It can serve as a guide for integrating these features into your own Flutter 
 projects. Please refer to the tutorial link below for details on getting started, obtaining 
 authentication tokens and other implementation specifics.

 https://docs.mesibo.com/tutorials/get-started/

 You MUST create tokens and initialize them for user1 and user2 in the code below.

 **************************************************************************************************/

/* ============= IMPORTANT NOTE ============

   Although the mesibo Flutter SDK includes all essential messaging, call, and conferencing features, 
   it is a limited version compared to the complete native Android and iOS mesibo APIs. 
   The Flutter SDK is actively expanding and will soon have all native mesibo API features. 
   However, if you need any specific native mesibo API not yet in the Flutter SDK, 
   please write to us, and we will prioritize adding that feature.
*/

class DemoUser {
  String token = "";
  String address = "";

  DemoUser(String t, String a) {
    token = t;
    address = a;
  }
}

void main() {
  runApp(FirstMesiboApp());
}

/// Home widget to display video chat option.
class FirstMesiboApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Mesibo Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("First Mesibo App"),
        ),
        body:  HomeWidget(),

      ),
      debugShowCheckedModeBanner: false,
    );
  }
}

/// Widget to display start video call layout.
class HomeWidget extends StatefulWidget {
  [@override](/user/override)
  _HomeWidgetState createState() => _HomeWidgetState();
}

class _HomeWidgetState extends State<HomeWidget> implements MesiboConnectionListener, MesiboMessageListener, MesiboGroupListener, MesiboPhoneContactsListener, MesiboCallIncomingListener, MesiboLocationListener, MesiboProfileSearchListener {
  static Mesibo _mesibo = Mesibo();
  static MesiboUI _mesiboUi = MesiboUI();
  String _mesiboStatus = 'Mesibo status: Not Connected.';
  Text? mStatusText;
  bool authFail = false;
  String mAppId = "";

 /**************************************************************************************************
   Please refer to the tutorial link below for details on obtaining user authentication tokens.

   https://docs.mesibo.com/tutorials/get-started/
 **************************************************************************************************/
  DemoUser user1 = DemoUser("user-auth-token-for-user-1", '<user2-address>');
  DemoUser user2 = DemoUser("user-auth-token-for-user-2", '<user1-address>');

  String remoteUser = "";
  bool mOnline = false, mLoginDone = false;
  ElevatedButton? loginButton1, loginButton2;

  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  void Mesibo_onConnectionStatus(int status) {
    print('Mesibo_onConnectionStatus: ' + status.toString());

    if(authFail) return;  // to prevent overwriting displayed status
    String statusText = status.toString();
    if(status == Mesibo.MESIBO_STATUS_ONLINE) {
        statusText = "Online";
    } else if(status == Mesibo.MESIBO_STATUS_CONNECTING) {
        statusText = "Connecting";
    } else if(status == Mesibo.MESIBO_STATUS_CONNECTFAILURE) {
        statusText = "Connect Failed";
    } else if(status == Mesibo.MESIBO_STATUS_NONETWORK) {
        statusText = "No Network";        
    } else if(status == Mesibo.MESIBO_STATUS_AUTHFAIL) {
        authFail = true;
        String warning = "The token is invalid. Ensure that you have used appid \"" + mAppId + "\" to generate Mesibo user access token";
        statusText = warning;
        print(warning);
        showAlert("Auth Fail", warning);
    } 

    _mesiboStatus = 'Mesibo status: ' + statusText;
    setState(() {});

    if(1 == status) mOnline = true;
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        InfoTitle(),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            loginButton1 = ElevatedButton(
              child: Text("Login as User-1"),
              onPressed: _loginUser1,
            ),
            loginButton2 = ElevatedButton(
              child: Text("Login as User-2"),
              onPressed: _loginUser2,
            ),
          ],
        ),

        Container(
          margin: const EdgeInsets.all(10.0),
          padding: const EdgeInsets.all(10.0),
          decoration: BoxDecoration(
          border: Border.all(color: Colors.grey)
        ),
        child: Text(_mesiboStatus, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
        ), 

        mStatusText = Text(""),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            loginButton1 = ElevatedButton(
              child: Text("Send a Message"),
              onPressed: _sendMessage,
            ),
            loginButton2 = ElevatedButton(
              child: Text("Set Profile Info"),
              onPressed: _setProfileInfo,
            ),
          ],
        ),

      Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            ElevatedButton(
              child: Text("Show Messages"),
              onPressed: _showMessages,
            ),
            ElevatedButton(
               child: Text("Show User List"),
                onPressed: _showUserList,
            ),
          ],
        ),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            ElevatedButton(
              child: Text("Read Summary"),
              onPressed: _readSummary,
            ),
            ElevatedButton(
              child: Text("Read Messages"),
              onPressed: _readMessages,
            ),
          ],
        ),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            ElevatedButton(
              child: Text("Video Call"),
              onPressed: _videoCall,
            ),
            ElevatedButton(
              child: Text("Audio Call"),
              onPressed: _audioCall,
            ),
          ],
        ),

        ElevatedButton(
          child: Text("Group Call"),
          onPressed: _groupCall,
        ),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            ElevatedButton(
              child: Text("Sync Phone Contacts"),
              onPressed: _syncPhoneContacts,
            ),
            ElevatedButton(
               child: Text("Get Phone Contact"),
                onPressed: _getPhoneContacts,
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            ElevatedButton(
              child: Text("Sync Location"),
              onPressed: _syncLocation,
            ),
            ElevatedButton(
               child: Text("Get Location"),
                onPressed: _getLocation,
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            ElevatedButton(
              child: Text("Device Location"),
              onPressed: _deviceLocation,
            ),
            ElevatedButton(
               child: Text("Search Location"),
                onPressed: _searchProfiles,
            ),
          ],
        ),

      ],
    );
  }

  [@override](/user/override)
  void dispose() {
    super.dispose();
  }

  void showAlert(String title, String body) {
    AlertDialog alert = AlertDialog(
      title: Text(title),
      content: Text(body),
    );
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

  bool isOnline() {
    if(mOnline) return true;
    showAlert("Not Online", "First login with a valid token");
    return false;
  }

  bool hasLoggedIn() {
	  if(mLoginDone) return true;
	  showAlert("Failed", "Login befor calling other APIs");
	  return false;
  }

  void initMesibo(String token) async {
      // optional - only to show alert in AUTHFAIL case
      Future<String> asyncAppId = _mesibo.getAppIdForAccessToken();
      asyncAppId.then((String appid) { mAppId = appid; });

      // initialize mesibo
      _mesibo.setAccessToken(token);
      _mesibo.setListener(this);
      _mesibo.start();

      /* Optional - to get missed call notification MesiboCall_onNotify */
      MesiboCall.setListener(this);

      /**************************************************************************************************
        Optional - To use mesibo Location APIs
        
	https://docs.mesibo.com/api/secure-location/
      **************************************************************************************************/
      _mesibo.getLocationManager().setListener(this);


      /**************************************************************************************************
         override default UI text, colors, etc.Refer to the documentation 

	 https://docs.mesibo.com/ui-modules/

	 Also refer to the header file for complete list of parameters (applies to both Android/iOS)
	 https://github.com/mesibo/mesiboframeworks/blob/main/mesiboui.framework/Headers/MesiboUI.h#L170
      **************************************************************************************************/

      _mesiboUi.getUiDefaults().then((MesiboUIOptions options) {
        options.enableBackButton = true;
        options.appName = "My First App";
        options.toolbarColor = 0xff00868b;
        _mesiboUi.setUiDefaults(options);
      });

      /**************************************************************************************************
         The code below enables basic UI customization. 

         However, you can customize entire user interface by implementing MesiboUIListner for Android and 
         iOS. Refer to 

	https://docs.mesibo.com/ui-modules/customizing/
      **************************************************************************************************/

      MesiboUIButtons buttons = MesiboUIButtons();
      buttons.message = true;
      buttons.audioCall = true;
      buttons.videoCall = true;
      buttons.groupAudioCall = true;
      buttons.groupVideoCall = true;
      buttons.endToEndEncryptionInfo = false; // e2ee should be enabled 
      _mesiboUi.setupBasicCustomization(buttons, null);
      
      MesiboCallUiProperties cp = MesiboCallUiProperties();
      cp.showScreenSharing = false;
      _mesiboUi.setCallUiDefaults(cp);
  }

  void _loginUser1() {
    if(null == _mesibo) {
      showAlert("Mesibo NULL", "mesibo null");
      return;
    }
    if(mLoginDone) {
      showAlert("Failed", "You have already initiated login. If the connection status is not 1, check the token and the package name/bundle ID");
      return;
    }
    mLoginDone = true;
    initMesibo(user1.token);
    remoteUser = user2.address;
  }

  void _loginUser2() {
    if(mLoginDone) {
      showAlert("Failed", "You have already initiated login. If the connection status is not 1, check the token and the package name/bundle ID");
      return;
    }
    mLoginDone = true;
    initMesibo(user2.token);
    remoteUser = user1.address;
  }


  void _setProfileInfo() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getSelfProfile() as MesiboProfile;
    profile.reset(); // reset to start fresh
    
    profile.name = "Joe from Flutter";
    profile.setImageUrl("https://images.freeimages.com/images/large-previews/bbb/autumn-in-new-york-5-1360120.jpg");
    profile.setString("status", "setting status from Flutter app");
    
    // You can set more values as required by your app
    //profile.setInt("year", 2024);
    //profile.setBoolean("test", true);
    
    // get all profile values
    //Map<String?, Object?>? values = await profile.getValues();
    //print("profile values: " + values.toString());

    profile.save();
    
  }

  void _sendMessage() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    
    MesiboMessage m = profile.newMessage();
    m.message = "Hello from Flutter";
    m.send();
  }

  void _sendPresence() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;

    MesiboPresence m = profile.newPresence();
    m.sendTyping();
  }

  void _showMessages() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    
    _mesiboUi.launchMessaging(profile);
  }

  void _showUserList() {
    if(!isOnline()) return;
    _mesiboUi.launchUserList();
  }

  void _readSummary() async {
    if(!isOnline()) return;
    MesiboReadSession rs = MesiboReadSession.createReadSummarySession(this);
    rs.read(100);
  }
  
  void _readMessages() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    MesiboReadSession rs = profile.createReadSession(this);
    rs.read(100);	
  }

  void _audioCall() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    _mesiboUi.call(profile, false);
  }

  void _videoCall() async {
    if(!isOnline()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    _mesiboUi.call(profile, true);
  }

  void _groupCall() async {
    if(!isOnline()) return;
    int groupid = 0; // create group first, add memmbers and then execute the following. 

    // disabled by defaut
    if(0 == groupid) {
      showAlert("No group defined", "Refer to the group management documentation to create a group and add members before using group call function");
      return;
    }

    MesiboProfile profile = await _mesibo.getGroupProfile(groupid) as MesiboProfile;
    _mesiboUi.groupCall(profile, true, true, false, false);
  }

      /**************************************************************************************************
         The code below is to read phone contacts, if required by your app

	https://docs.mesibo.com/api/phone-contacts-and-addressbook/
      **************************************************************************************************/

  [@override](/user/override)
  void Mesibo_onPhoneContactsChanged() {
  }

  [@override](/user/override)
  void Mesibo_onPhoneContactsAdded(List<String?> phones) {
  }
        
  [@override](/user/override)
  void Mesibo_onPhoneContactsDeleted(List<String?> phones) {
  }

  void _syncPhoneContacts() async {
    _mesibo.getPhoneContactsManager().setListener(this);
    _mesibo.getPhoneContactsManager().start();
  }

  void _getPhoneContacts() async {
    MesiboPhoneContact contact = (await _mesibo.getPhoneContactsManager().getPhoneNumberInfo("+18005551234", null, true))! as MesiboPhoneContact;

    print("Mesibo Contact: name ${contact.name} phone ${contact.phoneNumber} formatted ${contact.formattedPhoneNumber} country ${contact.country}");
  }

  void _syncLocation() async {
    if(!hasLoggedIn()) return;
    _mesibo.getLocationManager().start(null);
  }
  
  void printLocation(MesiboLocation? loc, String name) {
        if(null == loc)
		print("${name}: null");
       else 
		print("${name}: lat ${loc.latitude} lon ${loc.longitude}");
  }

  void _deviceLocation() async {
    if(!hasLoggedIn()) return;
    MesiboLocation? loc = await _mesibo.getLocationManager().getDeviceLocation() as MesiboLocation?;
    printLocation(loc, "device location");
  }
  
  void _getLocation() async {
    if(!hasLoggedIn()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    MesiboLocation? loc = await profile.location().get() as MesiboLocation?;
    printLocation(loc, "profile Location");
  }
  
  void _shareLocation() async {
    if(!hasLoggedIn()) return;
    MesiboProfile profile = await _mesibo.getUserProfile(remoteUser) as MesiboProfile;
    profile.location().share(3600);
  }
  
  void _searchProfiles() async {
    if(!hasLoggedIn()) return;
	MesiboProfileSearch ps = MesiboProfileSearch();
	ps.setDistance(10000);
	ps.setListener(this);
	ps.search();
  }

  void __createGroup() async {
    MesiboGroupSettings settings = MesiboGroupSettings();
    settings.name = "My Group";
  }
  
  [@override](/user/override)
  void Mesibo_onMessage(MesiboMessage message) {
    String groupName = "";
    if(null != message.groupProfile)
      groupName = message.groupProfile!.name!;

    print('Mesibo_onMessage: from: (' + message.profile!.name! + ") group: (" + groupName + ") Message: " + message.message!);
    print('Mesibo_onMessage: date: (' + message.getTimestamp().getDate() + ') time: (' + message.getTimestamp().getTime() + ')');

	if(message.isRealtimeMessage()) {
		print("real-time message");
	} else if(message.isDbMessage()) {
		print("db message");
	}
	
	if(message.isIncoming()) {
		print("incoming message");
	} else if(message.isOutgoing()) {
		print("outgoing message");
	}
  }
  
  [@override](/user/override)
  void Mesibo_onMessageStatus(MesiboMessage message) {
    print('Mesibo_onMessageStatus: ' + message.status.toString());
  }
  
  [@override](/user/override)
  void Mesibo_onMessageUpdate(MesiboMessage message) {
    print('Mesibo_onMessageUpdate: ' + message.message!);
  }

  [@override](/user/override)
  void Mesibo_onGroupCreated(MesiboProfile profile) {

  }

  [@override](/user/override)
  void Mesibo_onGroupJoined(MesiboProfile profile) {
      print("Mesibo Group Joined: " + profile.name! + " group id: " + profile.groupId.toString());
      MesiboMessage m = profile.newMessage();
      m.message = "Hey, I have joined this group from Flutter";
      m.send();

      MesiboGroupProfile? groupProfile = profile.getGroupProfile();
      if(groupProfile == null) return;
      groupProfile.getMembers(10, false, this);
  }

  [@override](/user/override)
  void Mesibo_onGroupLeft(MesiboProfile profile) {
    print("Mesibo Group left: " + profile.name! + " group id: " + profile.groupId.toString());
  }

  [@override](/user/override)
  void Mesibo_onGroupMembers(MesiboProfile profile, List<MesiboGroupMember?> members) {
    print("Mesibo Group members: " + profile.name! + " group id: " + profile.groupId.toString());

    for(final m in members) {
        String? name = m?.getProfile()!.getNameOrAddress();
        print("Mesibo group member: " + name!);
    }
  }

  [@override](/user/override)
  void Mesibo_onGroupMembersJoined(MesiboProfile profile, List<MesiboGroupMember?> members) {
    print("Mesibo Group members joined: " + profile.name! + " group id: " + profile.groupId.toString());
  }

  [@override](/user/override)
  void Mesibo_onGroupMembersRemoved(MesiboProfile profile, List<MesiboGroupMember?> members) {

  }

  [@override](/user/override)
  void Mesibo_onGroupSettings(MesiboProfile profile, MesiboGroupSettings? groupSettings, MesiboMemberPermissions? memberPermissions, List<MesiboGroupPin?> groupPins) {

  }

  [@override](/user/override)
  void Mesibo_onGroupError(MesiboProfile profile, int error) {

  }

  [@override](/user/override)
  void MesiboCall_onNotify(int type, MesiboProfile profile, bool video, MesiboDateTime ts) {
    print("MesiboCall_onNotify: " + profile.name!);
  }
	      
  [@override](/user/override)
  void Mesibo_onDeviceLocation(MesiboLocation location) {
	printLocation(location, "Device Location Update");
  }

  [@override](/user/override)
  void Mesibo_onLocationReceived(MesiboProfile profile) async {
	print("Mesibo_onLocationReceived");
    	MesiboLocation? loc = await profile.location().get() as MesiboLocation?;
	printLocation(loc, "profile Location updated");
  }

  [@override](/user/override)
  void Mesibo_onLocationRequest(MesiboProfile profile, int duration) {
	print("Mesibo_onLocationRequest");
  }

  [@override](/user/override)
  void Mesibo_onLocationShared(MesiboProfile profile, int duration) {
	print("Mesibo_onLocationShared");
  }

  [@override](/user/override)
  void Mesibo_onLocationError(MesiboProfile profile, int error) {
	print("Mesibo_onLocationError");
  }

  [@override](/user/override)
  void Mesibo_onProfileSearchResults(List<MesiboProfile?> profiles, MesiboProfileSearch search) {
	print("Mesibo_onProfileSearchResults: count ${profiles.length}");
	for(final profile in profiles){
  		//var currentElement = e;
		print("Mesibo_onProfileSearchResults: ${profile!.name!}");
	}
  }

  [@override](/user/override)
  void Mesibo_onProfileSearchUpdate(List<MesiboProfile?> profiles, MesiboProfileSearch search) {
  }
}

/// Widget to display start video call title.
class InfoTitle extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: Center(
        child: Text(
          "Login as User-1 from one device and as User-2 from another!",
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}

更多关于Flutter即时通讯插件mesibo_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter即时通讯插件mesibo_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用mesibo_flutter_sdk来实现即时通讯功能的代码示例。这个示例将展示如何初始化Mesibo SDK、登录用户、发送和接收消息。

首先,确保你已经在pubspec.yaml文件中添加了mesibo_flutter_sdk依赖:

dependencies:
  flutter:
    sdk: flutter
  mesibo_flutter_sdk: ^最新版本号 # 替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

1. 初始化Mesibo SDK

在你的Flutter应用的入口文件(通常是main.dart)中,初始化Mesibo SDK:

import 'package:flutter/material.dart';
import 'package:mesibo_flutter_sdk/mesibo_flutter_sdk.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化Mesibo SDK
  await Mesibo.initMesibo("YOUR_APP_KEY", "YOUR_APP_SECRET");
  
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChatScreen(),
    );
  }
}

请替换YOUR_APP_KEYYOUR_APP_SECRET为你的Mesibo应用密钥和密钥。

2. 用户登录

ChatScreen中,添加用户登录逻辑:

class ChatScreen extends StatefulWidget {
  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  String _username = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Mesibo Chat'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: [
            TextField(
              decoration: InputDecoration(labelText: 'Username'),
              onChanged: (value) {
                setState(() {
                  _username = value;
                });
              },
            ),
            ElevatedButton(
              onPressed: () async {
                // 用户登录
                bool result = await Mesibo.userLogin(_username, "password123"); // 使用适当的密码
                if (result) {
                  print("User logged in successfully");
                  // 这里可以跳转到聊天界面或显示聊天列表
                } else {
                  print("User login failed");
                }
              },
              child: Text('Login'),
            ),
            // 假设用户已经登录,这里是发送和接收消息的示例区域
            Expanded(
              child: ChatArea(),
            ),
          ],
        ),
      ),
    );
  }
}

3. 发送和接收消息

创建一个ChatArea小部件来处理消息的发送和接收:

class ChatArea extends StatefulWidget {
  @override
  _ChatAreaState createState() => _ChatAreaState();
}

class _ChatAreaState extends State<ChatArea> {
  final TextEditingController _messageController = TextEditingController();
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();

    // 监听新消息
    Mesibo.addListener(Mesibo.EVENT_INCOMING_MESSAGE, (data) {
      // 解析并显示新消息
      print("New message received: ${data.toString()}");
      // 更新UI,这里需要用到setState
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    // 显示消息列表(这里简化处理,仅打印到控制台)
    // 实际应用中,你需要维护一个消息列表并在UI中显示

    return Column(
      children: [
        Expanded(
          child: ListView.builder(
            controller: _scrollController,
            itemCount: 100, // 假设有100条消息,实际应用中需要动态计算
            itemBuilder: (context, index) {
              // 显示每条消息的内容
              return ListTile(
                title: Text("Message $index"), // 替换为实际消息内容
              );
            },
          ),
        ),
        TextField(
          controller: _messageController,
          decoration: InputDecoration(labelText: 'Type a message'),
          onSubmitted: (value) async {
            // 发送消息
            String toUser = "recipient_username"; // 替换为实际接收者用户名
            bool result = await Mesibo.sendMessage(toUser, value);
            if (result) {
              print("Message sent successfully");
              // 清空文本框并滚动到底部
              _messageController.clear();
              _scrollController.animateTo(
                _scrollController.position.maxScrollExtent,
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeOut,
              );
            } else {
              print("Message send failed");
            }
          },
        ),
      ],
    );
  }
}

请注意,这个示例代码非常简化,仅用于演示基本流程。在实际应用中,你需要处理更多细节,比如错误处理、UI更新、消息存储等。此外,Mesibo.sendMessageMesibo.userLogin等方法的参数和返回值可能需要根据Mesibo SDK的文档进行调整。

确保你已经阅读并理解了Mesibo Flutter SDK的官方文档,以便正确实现所有功能。

回到顶部