/
main.dart
156 lines (116 loc) · 4.7 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_tim_plugin/tim_flutter_plugin.dart';
import 'package:flutter_tim_plugin/common_define.dart';
import 'package:flutter_tim_plugin/message/text_message.dart';
import 'package:flutter_tim_plugin/message/image_message.dart';
import 'package:flutter_tim_plugin/message/sound_message.dart';
import 'package:flutter_tim_plugin/message/location_message.dart';
import 'package:flutter_tim_plugin/message/emoji_message.dart';
import 'package:flutter_tim_plugin/message/file_message.dart';
import 'package:flutter_tim_plugin/message/custom_message.dart';
import 'package:flutter_tim_plugin/message/message.dart';
import 'package:flutter_tim_plugin/message/video_message.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String result ="result";
TextEditingController loginController;
TextEditingController messageController;
@override
void initState() {
super.initState();
loginController=TextEditingController();
messageController=TextEditingController();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home:
Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body:SingleChildScrollView(
child: Column(
children: <Widget>[
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
onPressed: () => this.onPressed("初始化"),
child: Text("初始化"),
),
SizedBox(
height: 50,
width: double.infinity,
child: TextField(
decoration: InputDecoration(hintText: "请输入userid"),
controller: this.loginController,
),
),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
onPressed: () => this.onPressed("登录"),
child: Text("登录"),
),
SizedBox(
height: 50,
width: double.infinity,
child: TextField(
decoration: InputDecoration(hintText: "请输入发送内容"),
controller: this.messageController,
),
),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
onPressed: () => this.onPressed("发送文字消息"),
child: Text("发送文字消息"),
),
Container(
padding: EdgeInsets.all(5),
color: Colors.white30,
height: 100,
width: double.infinity,
child: Text(this.result),
)
],
)
)
),
);
}
Future<void> onPressed(type) async {
if(type=="初始化"){
//1.初始化 im SDK
var initSdk= await TimFlutterPlugin.init(1400294549);
setState(() {
this.result=initSdk.toString();
});
} else if (type == "登录") {
//2.获取sig 这里是本地计算sig也可通过自己服务器获取
String sig =await TimFlutterPlugin.getUserSig(1400294549, loginController.text, (7 * 24 * 60 * 60), "32fcda1bb51e94ca08eddeb80da0f030be66101b1525f3e7b910ff4dc6e631fb");
//3.连接 im SDK
var login =await TimFlutterPlugin.login(loginController.text, sig);
setState(() {
this.result=login.toString();
});
}else if(type == "发送文字消息"){
var msg=await TimFlutterPlugin.sendMessage( id: 2255,conversationType: TIMConversationType.C2C,content: TextMessage.obtain(messageController.text));
setState(() {
this.result=msg.toString();
});
//TimFlutterPlugin.sendMessage( id: 2255,conversationType: TIMConversationType.C2C,content: ImageMessage.obtain(path));//图片消息
// TimFlutterPlugin.sendMessage( id: 2255,conversationType: TIMConversationType.C2C,content: CustomMessage.obtain(Uint8List.fromList(utf8.encode("自定义消息"))));
// TimFlutterPlugin.sendMessage( id: 2255,conversationType: TIMConversationType.C2C,content: FileMessage.obtain(path,"文件发送"));
// TimFlutterPlugin.sendMessage( id: 2255,conversationType: TIMConversationType.C2C,content: LocationMessage.obtain(66.6,99.9,"位置信息"));
// TimFlutterPlugin.getLocalMessage(conversationType: TIMConversationType.C2C, id: 2255, count: 5);//获取消息
}
}
}