home_page.dart
19.1 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:one_key/app.dart';
import 'package:one_key/models/notebook_detail.dart';
import 'package:one_key/pages/detail/notebook_detail_change_page.dart';
import 'package:one_key/pages/detail/notebook_detail_page.dart';
import 'package:one_key/pages/enter/enter_page.dart';
import 'package:one_key/pages/message/message_page.dart';
import 'package:one_key/pages/my/my_page.dart';
import 'package:one_key/pages/notebook/notebook_page.dart';
import 'package:one_key/pages/table/table_manager_page.dart';
import 'package:one_key/provider/contact_model.dart';
import 'package:one_key/provider/enter_model.dart';
import 'package:one_key/provider/message_dialog_model.dart';
import 'package:one_key/provider/message_model.dart';
import 'package:one_key/provider/my_model.dart';
import 'package:one_key/provider/notebook_detail_change_model.dart';
import 'package:one_key/provider/notebook_model.dart';
import 'package:one_key/provider/table_manager_model.dart';
import 'package:one_key/provider/tools.dart';
import 'package:one_key/provider/update_model.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/misc.dart';
import 'package:one_key/utils/navigator_util.dart';
import 'package:one_key/utils/utils.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tpns_flutter_plugin/tpns_flutter_plugin.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
class HomePage extends StatefulWidget {
final int initialIndex; //初始tab
HomePage({this.initialIndex = 1});
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return new HomePageState();
}
}
class HomePageState extends State<HomePage> with WidgetsBindingObserver {
List<Widget> pages = List<Widget>();
int _currIndex = 1;
Color _activeColor = MyColours.color_199095;
Color _defaultColor = MyColours.color_999999;
int numPush = 0; //推送消息个数
Timer timer;
int xgCount = 1; //注册信鸽次数,最多5次
DateTime lastBack;
@override
void initState() {
// TODO: implement initState
super.initState();
LogUtil.v("home_initState");
WidgetsBinding.instance.addObserver(this);
_currIndex = widget.initialIndex;
pages
..add(ChangeNotifierProvider<MessageModel>(
create: (_) => MessageModel(),
child: SafeArea(child: MessagePage()),
))
// ..add(ChangeNotifierProvider<EnterModel>(
// create: (_) => EnterModel(),
// child: SafeArea(child: EnterPage(detailEntity:NotebookDetail.def(context),rid: "-1",)),
// ))
..add(ChangeNotifierProvider<NoteBookModel>(
create: (_) => NoteBookModel(),
child: SafeArea(child: NoteBookPage()),
))
..add(ChangeNotifierProvider<TableManagerModel>(
create: (_) => TableManagerModel(),
child: SafeArea(child: TableManagerPage()),
))
..add(ChangeNotifierProvider<MyModel>(
create: (_) => MyModel(),
child: MyPage(),
));
WidgetsBinding.instance.addPostFrameCallback((_) {
initPlatformState();
if (widget.initialIndex == 0) return;
_getIntent();
Future.delayed(Duration(milliseconds: 30000), () {
ContactModel.getInstance().uploadLocalContact(context);
UpdateModel updateModel = new UpdateModel();
updateModel.checkUpdate(context, showToast: false);
});
timer = Timer.periodic(Duration(minutes: 3), (_) {
getUntreatedMessage();
});
});
}
//获取点击通知栏的自定义参数
_getIntent() async {
_clearBadge(() {});
Map param = await Tools().getIntent();
Map notifyParam = param;
if (Platform.isIOS && param != null) {
notifyParam = json.decode(param['custom'].toString());
}
LogUtil.v(param.toString());
if (notifyParam != null && notifyParam.isNotEmpty) {
switch (notifyParam["messageType"]) {
case "10":
NavigatorUtil.goHandleOnlinePage(
context, notifyParam["rid"], notifyParam['mid'],
fromNotify: true);
break;
case "11":
NavigatorUtil.goNotebookDetailPage(context, notifyParam['rid'],
mid: notifyParam['mid'], fromNotify: true);
break;
}
} else {
getUntreatedMessage();
}
}
//初始化信鸽推送
initPlatformState() {
/// 开启DEBUG
XgFlutterPlugin().setEnableDebug(false);
/// 添加回调事件
XgFlutterPlugin().addEventHandler(
onRegisteredDeviceToken: (String msg) async {
print("flutter onRegisteredDeviceToken: $msg");
},
onRegisteredDone: (String msg) async {
String token = "";
try {
token = await XgFlutterPlugin.xgToken;
} catch (e) {
LogUtil.v("注册失败:${e.toString()}");
// Utils.showToast("注册失败:${e.toString()}");
}
if (token == null || token.isEmpty) {
LogUtil.v("注册失败:${xgCount.toString()}");
// Utils.showToast("注册失败:${xgCount.toString()}");
registerPush();
} else {
LogUtil.v("注册成功:$token");
// Utils.showToast("注册成功:$token");
userTokenInit(token);
// XgFlutterPlugin().setAppBadge(0);
}
},
unRegistered: (String msg) async {
print("flutter unRegistered: $msg");
},
onReceiveNotificationResponse: (Map<String, dynamic> msg) async {
print("flutter onReceiveNotificationResponse $msg");
numPush++;
_addBadge();
},
onReceiveMessage: (Map<String, dynamic> msg) async {
print("flutter onReceiveMessage $msg");
},
xgPushDidSetBadge: (String msg) async {
print("flutter xgPushDidSetBadge: $msg");
// /// 在此可设置应用角标
// XgFlutterPlugin().setAppBadge(0);
},
xgPushDidBindWithIdentifier: (String msg) async {
print("flutter xgPushDidBindWithIdentifier: $msg");
},
xgPushDidUnbindWithIdentifier: (String msg) async {
print("flutter xgPushDidUnbindWithIdentifier: $msg");
},
xgPushDidUpdatedBindedIdentifier: (String msg) async {
print("flutter xgPushDidUpdatedBindedIdentifier: $msg");
},
xgPushDidClearAllIdentifiers: (String msg) async {
print("flutter xgPushDidClearAllIdentifiers: $msg");
},
xgPushClickAction: (Map<String, dynamic> msg) async {
_clearBadge(() {});
if (Platform.isIOS && msg != null && msg['custom'] != null) {
Map custom = json.decode(msg['custom'].toString());
switch (custom["messageType"].toString()) {
case "10":
NavigatorUtil.goHandleOnlinePage(
context, custom["rid"].toString(), custom['mid'].toString(),
fromNotify: true);
break;
case "11":
NavigatorUtil.goNotebookDetailPage(
context, custom['rid'].toString(),
mid: custom['mid'].toString(), fromNotify: true);
break;
}
}
},
);
registerPush();
}
//注册信鸽
registerPush() async {
if (xgCount > 5) return;
if (Platform.isAndroid) {
/// 设置OPPO AppKey 和 AppSecret
if (await XgFlutterPlugin().getXgAndroidApi().isOppoRom()) {
XgFlutterPlugin().getXgAndroidApi().enableOppoNotification();
XgFlutterPlugin().getXgAndroidApi().setOppoPushAppId(appId: "30260281");
XgFlutterPlugin()
.getXgAndroidApi()
.setOppoPushAppKey(appKey: "9b7e372f93794593953f93f118619570");
}
// 设置小米 AppID 和 AppKey。
if (await XgFlutterPlugin().getXgAndroidApi().isMiuiRom()) {
XgFlutterPlugin()
.getXgAndroidApi()
.setMiPushAppId(appId: "2882303761518639456");
XgFlutterPlugin()
.getXgAndroidApi()
.setMiPushAppKey(appKey: "5231863961456");
}
XgFlutterPlugin().getXgAndroidApi().enableOtherPush();
}
/// 启动TPNS服务
LogUtil.v("register:${xgCount.toString()}");
XgFlutterPlugin().startXg("1600012876", "INUIMFK21WGH");
xgCount++;
}
void _addBadge() {
XgFlutterPlugin().setBadge(numPush);
XgFlutterPlugin().setAppBadge(numPush);
FlutterAppBadger.updateBadgeCount(numPush);
}
void _clearBadge(VoidCallback callback) async {
numPush = 0;
XgFlutterPlugin().setBadge(0);
XgFlutterPlugin().setAppBadge(0);
FlutterAppBadger.removeBadge();
callback();
}
//初始化推送token
userTokenInit(String token) {
UserModel userModel = Provider.of<UserModel>(context, listen: false);
userModel.token = token;
Map<String, String> params = {
"sid": userModel.user.sellerid,
"account": userModel.user.account,
"user_token": Platform.isAndroid ? "Android_$token" : "Ios_$token"
};
userModel.userTokenInit(context, params);
}
//获取未处理在线预订消息
getUntreatedMessage() {
UserModel userModel = Provider.of<UserModel>(context, listen: false);
Map<String, String> params = {
"sid": userModel.user.sellerid,
"type_id": "10", //10 在线预订
"status": "0", //未处理
"offset": "0",
"limit": "100",
};
MessageDialogModel messageModel =
Provider.of<MessageDialogModel>(context, listen: false);
messageModel.getUntreatedMsg(context, params);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
// TODO: implement didChangeAppLifecycleState
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.resumed: //应用前台可见
_clearBadge(() {
getUntreatedMessage();
});
break;
case AppLifecycleState.inactive:
print('inactive');
break;
case AppLifecycleState.paused:
print('paused');
break;
case AppLifecycleState.detached:
print('detached');
break;
default:
break;
}
}
// 系统窗口相关改变回调
void didChangeMetrics() {
super.didChangeMetrics();
print('fffff');
}
// // 系统亮度改变回调
// void didChangePlatformBrightness() {
// super.didChangePlatformBrightness();
// print('fdddddd');
// }
// // 文本缩放系数改变回调
// void didChangeTextScaleFactor() {}
// // 内存不足警告回调
// void didHaveMemoryPressure() {}
@override
Widget build(BuildContext context) {
// TODO: implement build
return new WillPopScope(
child: Scaffold(
bottomNavigationBar: BottomNavigationBar(
iconSize: ScreenUtil().setWidth(48),
unselectedFontSize: ScreenUtil().setSp(22),
selectedFontSize: ScreenUtil().setSp(22),
backgroundColor: MyColours.color_ffffff,
items: [
BottomNavigationBarItem(
icon: Image(
image: AssetImage("assets/message_default.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
activeIcon: Image(
image: AssetImage("assets/message_selected.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
title: Text(
"消息中心",
style: TextStyle(
color: _currIndex == 0 ? _activeColor : _defaultColor,
fontSize: ScreenUtil().setSp(22)),
),
),
// BottomNavigationBarItem(
// icon: Image(
// image: AssetImage("assets/message_default.png"),
// width: ScreenUtil().setWidth(48),
// height: ScreenUtil().setWidth(48),
// ),
// activeIcon: Image(
// image: AssetImage("assets/message_selected.png"),
// width: ScreenUtil().setWidth(48),
// height: ScreenUtil().setWidth(48),
// ),
// title: Text(
// "订座",
// style: TextStyle(
// color: _currIndex == 1 ? _activeColor : _defaultColor,
// fontSize: ScreenUtil().setSp(22)),
// ),
// ),
BottomNavigationBarItem(
icon: Image(
image: AssetImage("assets/notebook_default.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
activeIcon: Image(
image: AssetImage("assets/notebook_selected.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
title: Text(
"已预订",
style: TextStyle(
color: _currIndex == 1 ? _activeColor : _defaultColor,
fontSize: ScreenUtil().setSp(22)),
),
),
BottomNavigationBarItem(
icon: Image(
image: AssetImage("assets/tablemanage_default.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
activeIcon: Image(
image: AssetImage("assets/tablemanage_selected.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
title: Text(
"桌位",
style: TextStyle(
color: _currIndex == 2 ? _activeColor : _defaultColor,
fontSize: ScreenUtil().setSp(22)),
),
),
BottomNavigationBarItem(
icon: Image(
image: AssetImage("assets/my_default.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
activeIcon: Image(
image: AssetImage("assets/my_selected.png"),
width: ScreenUtil().setWidth(48),
height: ScreenUtil().setWidth(48),
),
title: Text(
"我的",
style: TextStyle(
color: _currIndex == 3 ? _activeColor : _defaultColor,
fontSize: ScreenUtil().setSp(22)),
),
),
],
type: BottomNavigationBarType.fixed,
currentIndex: _currIndex,
onTap: (int index) {
if (_currIndex != index) {
setState(() {
_currIndex = index;
});
if (_currIndex == 3) {
Utils.changeStatusColor(color: MyColours.color_199095);
} else {
Utils.changeStatusColor(color: MyColours.color_ffffff);
}
}
},
),
// bottomNavigationBar: BottomAppBar(
// shape: CircularNotchedRectangle(),
// child: Container(
// height: ScreenUtil().setWidth(98),
// // padding: EdgeInsets.fromLTRB(0, 12, 0, 8),
// child: Row(
// mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.spaceAround,
// crossAxisAlignment: CrossAxisAlignment.center,
// children: <Widget>[
// GestureDetector(
// onTap: () {
// // onTap(0);
// },
// child: Column(
// mainAxisSize: MainAxisSize.min,
// children: <Widget>[
// Image(
// image: AssetImage("assets/my_selected.png"),
// width: ScreenUtil().setWidth(44),
// height: ScreenUtil().setWidth(44),
// ),
// Text(
// "我的",
// style: TextStyle(
// color: _currIndex == 2
// ? _activeColor
// : _defaultColor,
// fontSize: ScreenUtil().setSp(22)),
// )
// ],
// )),
// GestureDetector(
// onTap: () {
// // onTap(1);
// },
// child: Column(
// mainAxisSize: MainAxisSize.min,
// children: <Widget>[
// Image(
// image: AssetImage("assets/my_selected.png"),
// width: ScreenUtil().setWidth(44),
// height: ScreenUtil().setWidth(44),
// ),
// Text(
// "我的",
// style: TextStyle(
// color: _currIndex == 2
// ? _activeColor
// : _defaultColor,
// fontSize: ScreenUtil().setSp(22)),
// )
// ],
// )),
// Column(
// mainAxisSize: MainAxisSize.min,
// children: <Widget>[
// Image(
// image: AssetImage("assets/my_selected.png"),
// width: ScreenUtil().setWidth(44),
// height: ScreenUtil().setWidth(44),
// ),
// Text(
// "我的",
// style: TextStyle(
// color:
// _currIndex == 2 ? _activeColor : _defaultColor,
// fontSize: ScreenUtil().setSp(22)),
// )
// ],
// )
// ])),
// ),
body: pages[_currIndex],
),
onWillPop: () async {
if (lastBack != null &&
DateTime.now().millisecondsSinceEpoch -
lastBack.millisecondsSinceEpoch <=
3000) {
return true;
}
lastBack = DateTime.now();
Utils.showToast("再次返回退出一键订");
return false;
},
);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
LogUtil.v("home_dispose");
if (timer != null) {
timer.cancel();
}
WidgetsBinding.instance.removeObserver(this);
}
}