/
login.dart
350 lines (324 loc) · 12.1 KB
/
login.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// ignore_for_file: unused_import, avoid_print
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tencent_calls_uikit/tencent_calls_uikit.dart';
import 'package:tencent_cloud_chat_demo/src/config.dart';
import 'package:tencent_cloud_chat_demo/src/pages/home_page.dart';
import 'package:tencent_cloud_chat_demo/src/pages/privacy/privacy_webview.dart';
import 'package:tencent_cloud_chat_demo/src/provider/theme.dart';
import 'package:tencent_cloud_chat_demo/src/routes.dart';
import 'package:tencent_cloud_chat_demo/utils/GenerateUserSig.dart';
import 'package:tencent_cloud_chat_demo/utils/commonUtils.dart';
import 'package:tencent_cloud_chat_demo/utils/toast.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import 'package:tencent_cloud_chat_vote_plugin/tencent_cloud_chat_vote_plugin.dart';
class LoginPage extends StatelessWidget {
final Function? initIMSDK;
const LoginPage({Key? key, this.initIMSDK}) : super(key: key);
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
return false;
},
child: Scaffold(
body: AppLayout(initIMSDK: initIMSDK),
resizeToAvoidBottomInset: false,
));
}
}
class AppLayout extends StatelessWidget {
final Function? initIMSDK;
const AppLayout({Key? key, this.initIMSDK}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
SystemChannels.textInput.invokeMethod('TextInput.hide');
},
child: Stack(
children: [
const AppLogo(),
LoginForm(
initIMSDK: initIMSDK,
),
],
),
);
}
}
class AppLogo extends StatelessWidget {
const AppLogo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final double height = MediaQuery.of(context).size.height;
final theme = Provider.of<DefaultThemeData>(context).theme;
return Stack(
alignment: Alignment.topCenter,
children: [
Container(
decoration: BoxDecoration(
gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [theme.lightPrimaryColor ?? CommonColor.lightPrimaryColor, theme.primaryColor ?? CommonColor.primaryColor]),
),
child: Image.asset("assets/hero_image.png", color: Colors.transparent, width: MediaQuery.of(context).size.width, height: 500)),
Positioned(
child: Container(
padding: EdgeInsets.only(top: height / 30, left: 24),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: CommonUtils.adaptWidth(380),
width: CommonUtils.adaptWidth(140),
child: const Image(
image: AssetImage("assets/logo_transparent.png"),
),
),
Expanded(
child: Container(
margin: const EdgeInsets.only(right: 5),
height: CommonUtils.adaptHeight(180),
padding: const EdgeInsets.only(top: 10, left: 12, right: 15),
child: Column(
children: <Widget>[
Text(
TIM_t("腾讯云即时通信IM"),
style: TextStyle(
color: Colors.white,
fontSize: CommonUtils.adaptFontSize(58),
),
),
Text(
TIM_t("欢迎使用本 APP 体验腾讯云 IM 产品服务"),
style: TextStyle(
color: Colors.white,
fontSize: CommonUtils.adaptFontSize(26),
),
),
],
crossAxisAlignment: CrossAxisAlignment.start,
),
)),
],
),
),
)
],
);
}
}
class LoginForm extends StatefulWidget {
final Function? initIMSDK;
const LoginForm({Key? key, required this.initIMSDK}) : super(key: key);
@override
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final CoreServicesImpl coreInstance = TIMUIKitCore.getInstance();
String userID = '';
@override
initState() {
super.initState();
checkFirstEnter();
if (widget.initIMSDK != null) {
widget.initIMSDK!();
}
}
TextSpan webViewLink(String title, String url) {
return TextSpan(
text: TIM_t(title),
style: const TextStyle(
color: Color.fromRGBO(0, 110, 253, 1),
),
recognizer: TapGestureRecognizer()
..onTap = () {
Navigator.push(context, MaterialPageRoute(builder: (context) => PrivacyDocument(title: title, url: url)));
},
);
}
void checkFirstEnter() async {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
SharedPreferences prefs = await _prefs;
String? firstTime = prefs.getString("firstTime");
if (firstTime != null && firstTime == "true") {
return;
}
showDialog<bool>(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
content: Text.rich(
TextSpan(style: const TextStyle(fontSize: 14, color: Colors.black, height: 2.0), children: [
TextSpan(
text: TIM_t("欢迎使用腾讯云即时通信 IM,为保护您的个人信息安全,我们更新了《隐私政策》,主要完善了收集用户信息的具体内容和目的、增加了第三方SDK使用等方面的内容。"),
),
const TextSpan(
text: "\n",
),
TextSpan(
text: TIM_t("请您点击"),
),
webViewLink("《用户协议》", 'https://web.sdk.qcloud.com/document/Tencent-IM-User-Agreement.html'),
TextSpan(
text: TIM_t(", "),
),
webViewLink("《隐私协议》", 'https://privacy.qq.com/document/preview/1cfe904fb7004b8ab1193a55857f7272'),
TextSpan(
text: TIM_t(", "),
),
webViewLink("《信息收集清单》", 'https://privacy.qq.com/document/preview/45ba982a1ce6493597a00f8c86b52a1e'),
TextSpan(
text: TIM_t("和"),
),
webViewLink("《信息共享清单》", 'https://privacy.qq.com/document/preview/dea84ac4bb88454794928b77126e9246'),
TextSpan(text: TIM_t("并仔细阅读,如您同意以上内容,请点击“同意并继续”,开始使用我们的产品与服务!")),
]),
overflow: TextOverflow.clip,
),
actions: [
CupertinoDialogAction(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 100, vertical: 10),
decoration: const BoxDecoration(
color: Color.fromRGBO(0, 110, 253, 1),
borderRadius: BorderRadius.all(
Radius.circular(24),
),
),
child: Text(TIM_t("同意并继续"), style: const TextStyle(color: Colors.white, fontSize: 16))),
onPressed: () {
prefs.setString("firstTime", "true");
Navigator.of(context).pop(true);
},
),
CupertinoDialogAction(
child: Text(TIM_t("不同意并退出"), style: const TextStyle(color: Colors.grey, fontSize: 16)),
isDestructiveAction: true,
onPressed: () {
exit(0);
},
),
],
);
},
);
}
directToHomePage() {
Routes().directToHomePage();
}
userLogin() async {
if (userID.trim() == '') {
ToastUtils.toast(TIM_t("请输入用户名"));
return;
}
String key = IMDemoConfig.key;
int sdkAppId = IMDemoConfig.sdkappid;
if (key == "") {
ToastUtils.toast(TIM_t("请在环境变量中写入key"));
return;
}
GenerateTestUserSig generateTestUserSig = GenerateTestUserSig(
sdkappid: sdkAppId,
key: key,
);
String userSig = generateTestUserSig.genSig(identifier: userID, expire: 99999);
await TUICallKit.instance.login(sdkAppId, userID, userSig);
var data = await coreInstance.login(
userID: userID,
userSig: userSig,
);
if (data.code != 0) {
final option1 = data.desc;
ToastUtils.toast(TIM_t_para("登录失败{{option1}}", "登录失败$option1")(option1: option1));
return;
}
// Initialize the poll plug-in
TencentCloudChatVotePlugin.initPlugin();
directToHomePage();
}
@override
Widget build(BuildContext context) {
ScreenUtil.init(
context,
designSize: const Size(750, 1624),
minTextAdapt: true,
);
return Stack(
children: [
Positioned(
bottom: CommonUtils.adaptHeight(200),
child: Container(
padding: const EdgeInsets.fromLTRB(16, 40, 16, 0),
decoration: const BoxDecoration(
//背景
color: Colors.white,
borderRadius: BorderRadius.only(topLeft: Radius.circular(30.0), topRight: Radius.circular(30.0), bottomLeft: Radius.circular(30.0), bottomRight: Radius.circular(30.0)),
//设置四周边框
),
// color: Colors.white,
height: MediaQuery.of(context).size.height - CommonUtils.adaptHeight(600),
width: MediaQuery.of(context).size.width,
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(top: CommonUtils.adaptFontSize(34)),
child: Text(
TIM_t("用户名"),
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: CommonUtils.adaptFontSize(34),
),
),
),
TextField(
autofocus: false,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: CommonUtils.adaptWidth(14)),
hintText: TIM_t("请输入用户名"),
hintStyle: TextStyle(fontSize: CommonUtils.adaptFontSize(32)),
//
),
keyboardType: TextInputType.number,
onChanged: (v) {
setState(() {
userID = v;
});
},
),
Container(
margin: EdgeInsets.only(
top: CommonUtils.adaptHeight(46),
),
child: Row(
children: [
Expanded(
child: ElevatedButton(
child: Text(TIM_t("登录")),
onPressed: userLogin,
),
)
],
),
),
Expanded(
child: Container(),
)
],
),
),
))
],
);
}
}