-
Notifications
You must be signed in to change notification settings - Fork 2
/
home.dart
378 lines (338 loc) · 10.7 KB
/
home.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:toast/toast.dart';
import 'package:connectivity/connectivity.dart';
import '../bean/message.dart' as m;
import '../constant.dart';
import '../presenter/home_presenter.dart';
import '../widget/update_dialog.dart';
import '../widget/message_item.dart';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title, this.themeType, this.changeTheme})
: super(key: key);
final String title;
final Function changeTheme;
final themeType;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with WidgetsBindingObserver, HomePageImpl, AutomaticKeepAliveClientMixin {
var _controller = new TextEditingController();
var _messages = <m.Message>[];
bool _auto = true;
GlobalKey<ScaffoldState> _key = new GlobalKey();
var _listController = ScrollController();
var _count = 0;
HomePresenter presenter;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
var _isBackground = false;
GlobalKey<UpdateDialogState> _dialogKey = new GlobalKey();
var _isDark;
var subscription;
@override
Widget build(BuildContext context) {
_isDark = widget.themeType ?? false;
return new Scaffold(
key: _key,
appBar: new AppBar(
actions: <Widget>[
Container(
padding: EdgeInsets.only(right: 10),
child: GestureDetector(
child:
new Icon(_isDark ? Icons.brightness_2 : Icons.brightness_5),
onTap: () {
setState(() {
_isDark = !_isDark;
widget.changeTheme(_isDark);
});
presenter.saveTheme(_isDark);
},
),
),
],
title: new Text(widget.title),
centerTitle: true,
bottom: new PreferredSize(
preferredSize: Size(0.0, 0.0),
child: new Text(
'在线人数:$_count',
style: TextStyle(color: Theme.of(context).textTheme.body2.color),
),
),
),
body: new Center(
child: new Column(
children: <Widget>[
new Flexible(
child: new Container(
padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
child: _buildChatMessageList(),
),
),
new Divider(
height: 1.0,
),
_buildBottom(),
],
),
),
);
}
Widget _buildChatMessageList() => new ListView.builder(
controller: _listController,
itemCount: _messages.length,
itemBuilder: (_, i) {
return MessageItem(
context,
autoPlay: _auto,
scaffoldKey: _key,
message: _messages[i],
);
});
Widget _buildBottom() => new Container(
decoration:
BoxDecoration(color: Theme.of(context).dialogBackgroundColor),
child: new Row(
children: <Widget>[
new Flexible(
child: new Align(
child: new Container(
padding: EdgeInsets.only(left: 5.0, right: 5.0),
child: new TextField(
controller: _controller,
autofocus: false,
autocorrect: false,
style: Theme.of(context).textTheme.body1,
maxLength: 888,
decoration: new InputDecoration(
border: InputBorder.none,
),
),
),
),
),
new IconButton(
icon: new Icon(Icons.add_circle_outline),
onPressed: () {
presenter.pickImage(ConstantValue.GALLERY);
},
),
new IconButton(
icon: new Icon(Icons.camera),
onPressed: () {
presenter.pickImage(ConstantValue.CAMERA);
},
),
new CupertinoButton(
child: new Text(
"发送",
style: new TextStyle(
color: Theme.of(context).textTheme.button.color),
),
onPressed: () {
if (_controller.text.trim().length > 0) {
presenter.sendMessage(_controller.text);
_controller.clear();
} else {
showSnackBar("请输入有效内容");
}
}),
],
),
);
@override
void initState() {
super.initState();
presenter = new HomePresenter(this);
FlutterBugly.getUpgradeInfo().then((UpgradeInfo info) {
if (info != null && info.id != null) {
showUpdateDialog(info.newFeature, info.apkUrl);
}
});
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
setState(() {
_auto = result == ConnectivityResult.wifi;
});
result == ConnectivityResult.none
? showSnackBar("网络已断开")
: presenter.reStart(true);
});
requestPermission();
WidgetsBinding.instance.addObserver(this);
_initNotification();
presenter.start();
}
@override
void showSnackBar(String text) {
Toast.show(text, context, backgroundColor: Theme.of(context).primaryColor);
// _key.currentState.showSnackBar(new SnackBar(content: new Text(text)));
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.inactive ||
state == AppLifecycleState.paused) {
_isBackground = true;
showNotification(null);
} else {
_isBackground = false;
_cancelAllNotifications();
presenter.reStart(false);
}
}
@override
void dispose() {
subscription.cancel();
super.dispose();
}
@override
void receiverMessage(m.Message message, bool isShowNotification) {
if (message.message.isEmpty) return;
setState(() {
_messages.add(message);
_count = message.count;
_listController.animateTo(
_listController.position.maxScrollExtent + _listController.offset,
duration: new Duration(milliseconds: 200),
curve: Curves.easeOut,
);
});
if (message.type != ConstantValue.CONNECTED &&
message.type != ConstantValue.DISCONNECTED &&
message.type != ConstantValue.HEART &&
isShowNotification &&
_isBackground) {
showNotification(message);
}
}
Widget _buildDialog(String version, String url) {
return new UpdateDialog(
_dialogKey,
version,
(_msg) {
showSnackBar(_msg);
},
() {
presenter.downloadApk(url);
},
);
}
@override
void showUpdateDialog(String version, String url) async {
await showDialog(
barrierDismissible: false,
context: context,
builder: (_) => _buildDialog(version, url),
);
}
@override
bool get wantKeepAlive => true;
@override
Future<bool> didPopRoute() {
presenter.stop();
return new Future<bool>.value(false);
}
@override
BuildContext getContext() => context;
@override
Future showNotification(m.Message message) async {
var channelId = message != null ? "message id" : 'channel id';
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
channelId, 'channel name', 'channel description',
playSound: false,
enableVibration: message != null,
ongoing: _isBackground,
autoCancel: !_isBackground,
styleInformation: new DefaultStyleInformation(true, true));
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
if (message == null) {
await flutterLocalNotificationsPlugin.periodicallyShow(
0,
presenter.getDeviceName() ?? ConstantValue.APP_NAME,
'正在运行中',
RepeatInterval.EveryMinute,
platformChannelSpecifics);
} else {
await flutterLocalNotificationsPlugin.show(0, ConstantValue.APP_NAME,
getNotificationsContent(message), platformChannelSpecifics);
}
}
String getNotificationsContent(message) {
if (message.type == ConstantValue.IMAGE)
return "${message.username}:[图片]";
else if (message.type == ConstantValue.VIDEO)
return "${message.username}:[视频]";
else if (message.type == ConstantValue.GIF)
return "${message.username}:[GIF]";
else
return "${message.username}:${message.message}";
}
void _initNotification() {
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
Future _cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
@override
void showDownloadProgress(double _progress) {
setState(() {
_dialogKey?.currentState?.progress = _progress;
});
}
@override
void requestPermission() {
// if (Platform.isAndroid)
// SimplePermissions.checkPermission(Permission.WriteExternalStorage)
// .then((_b) {
// if (!_b) {
// SimplePermissions.requestPermission(Permission.WriteExternalStorage);
// }
// });
}
@override
void hideProgress() {
Navigator.pop(context);
}
@override
void showProgress() async {
await showDialog(
barrierDismissible: false,
context: context,
builder: (_) => Center(child: CircularProgressIndicator()),
);
}
@override
void showDownloadFailed() {
setState(() {
_dialogKey?.currentState?.progress = 0.0;
});
showSnackBar("下载更新失败,请重试");
}
}
abstract class HomePageImpl {
void showSnackBar(String text);
void receiverMessage(m.Message message, bool isShowNotification);
void showUpdateDialog(String version, String url);
void showNotification(m.Message message);
void showProgress();
void hideProgress();
void showDownloadProgress(double _progress);
void showDownloadFailed();
void requestPermission();
BuildContext getContext();
}