123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import 'package:flutter/material.dart';
- import 'package:agora_chat_sdk/agora_chat_sdk.dart';
- import 'package:agora_chat_demo/config.dart';
- class HomePage extends StatefulWidget {
- const HomePage({Key? key, required this.title}) : super(key: key);
- final String title;
- @override
- State<HomePage> createState() => _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- ScrollController scrollController = ScrollController();
- String? _messageContent, _chatId;
- final List<String> _logText = [];
- @override
- void initState() {
- super.initState();
- _initSDK();
- _addChatListener();
- }
- @override
- void dispose() {
- ChatClient.getInstance.chatManager.removeEventHandler("UNIQUE_HANDLER_ID");
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: Container(
- padding: const EdgeInsets.only(left: 10, right: 10),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- mainAxisSize: MainAxisSize.max,
- children: [
- const SizedBox(height: 10),
- const Text("login userId: ${Config.userId}"),
- const Text("agoraToken: ${Config.agoraToken}"),
- const SizedBox(height: 10),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: [
- Expanded(
- flex: 1,
- child: TextButton(
- onPressed: _signIn,
- style: ButtonStyle(
- foregroundColor: MaterialStateProperty.all(Colors.white),
- backgroundColor:
- MaterialStateProperty.all(Colors.lightBlue),
- ),
- child: const Text("SIGN IN"),
- ),
- ),
- const SizedBox(width: 10),
- Expanded(
- child: TextButton(
- onPressed: _signOut,
- style: ButtonStyle(
- foregroundColor: MaterialStateProperty.all(Colors.white),
- backgroundColor:
- MaterialStateProperty.all(Colors.lightBlue),
- ),
- child: const Text("SIGN OUT"),
- ),
- ),
- ],
- ),
- const SizedBox(height: 10),
- TextField(
- decoration: const InputDecoration(
- hintText: "Enter recipient's userId",
- ),
- onChanged: (chatId) => _chatId = chatId,
- ),
- TextField(
- decoration: const InputDecoration(
- hintText: "Enter message",
- ),
- onChanged: (msg) => _messageContent = msg,
- ),
- const SizedBox(height: 10),
- TextButton(
- onPressed: _sendMessage,
- style: ButtonStyle(
- foregroundColor: MaterialStateProperty.all(Colors.white),
- backgroundColor: MaterialStateProperty.all(Colors.lightBlue),
- ),
- child: const Text("SEND TEXT"),
- ),
- Flexible(
- child: ListView.builder(
- controller: scrollController,
- itemBuilder: (_, index) {
- return Text(_logText[index]);
- },
- itemCount: _logText.length,
- ),
- ),
- ],
- ),
- ),
- );
- }
- void _initSDK() async {
- ChatOptions options = ChatOptions(
- appKey: Config.appKey,
- autoLogin: false,
- );
- await ChatClient.getInstance.init(options);
- }
- void _addChatListener() {
- ChatClient.getInstance.chatManager.addEventHandler(
- "UNIQUE_HANDLER_ID",
- ChatEventHandler(onMessagesReceived: onMessagesReceived),
- );
- }
- void _signIn() async {
- try {
- await ChatClient.getInstance.loginWithAgoraToken(
- Config.userId,
- Config.agoraToken,
- );
- _addLogToConsole("login succeed, userId: ${Config.userId}");
- } on ChatError catch (e) {
- _addLogToConsole("login failed, code: ${e.code}, desc: ${e.description}");
- }
- }
- void _signOut() async {
- try {
- await ChatClient.getInstance.logout(true);
- _addLogToConsole("sign out succeed");
- } on ChatError catch (e) {
- _addLogToConsole(
- "sign out failed, code: ${e.code}, desc: ${e.description}");
- }
- }
- void _sendMessage() async {
- if (_chatId == null || _messageContent == null) {
- _addLogToConsole("single chat id or message content is null");
- return;
- }
- var msg = ChatMessage.createTxtSendMessage(
- targetId: _chatId!,
- content: _messageContent!,
- );
- // msg.setMessageStatusCallBack(MessageStatusCallBack(
- // onSuccess: () {
- // _addLogToConsole("send message: $_messageContent");
- // },
- // onError: (e) {
- // _addLogToConsole(
- // "send message failed, code: ${e.code}, desc: ${e.description}",
- // );
- // },
- // ));
- ChatClient.getInstance.chatManager.sendMessage(msg);
- }
- void onMessagesReceived(List<ChatMessage> messages) {
- for (var msg in messages) {
- switch (msg.body.type) {
- case MessageType.TXT:
- {
- ChatTextMessageBody body = msg.body as ChatTextMessageBody;
- _addLogToConsole(
- "receive text message: ${body.content}, from: ${msg.from}",
- );
- }
- break;
- case MessageType.IMAGE:
- {
- _addLogToConsole(
- "receive image message, from: ${msg.from}",
- );
- }
- break;
- case MessageType.VIDEO:
- {
- _addLogToConsole(
- "receive video message, from: ${msg.from}",
- );
- }
- break;
- case MessageType.LOCATION:
- {
- _addLogToConsole(
- "receive location message, from: ${msg.from}",
- );
- }
- break;
- case MessageType.VOICE:
- {
- _addLogToConsole(
- "receive voice message, from: ${msg.from}",
- );
- }
- break;
- case MessageType.FILE:
- {
- _addLogToConsole(
- "receive image message, from: ${msg.from}",
- );
- }
- break;
- case MessageType.CUSTOM:
- {
- _addLogToConsole(
- "receive custom message, from: ${msg.from}",
- );
- }
- break;
- case MessageType.CMD:
- {}
- break;
- }
- }
- }
- void _addLogToConsole(String log) {
- _logText.add("$_timeString: $log");
- setState(() {
- scrollController.jumpTo(scrollController.position.maxScrollExtent);
- });
- }
- String get _timeString {
- return DateTime.now().toString().split(".").first;
- }
- }
|