forked from alibaba/flutter-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
223 lines (203 loc) · 6.42 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
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
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/rendering.dart';
import 'routers/routers.dart';
import 'routers/application.dart' show Application;
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_go/utils/provider.dart';
import 'package:flutter_go/utils/shared_preferences.dart';
import 'package:flutter_go/views/home.dart';
import 'package:flutter_go/model/search_history.dart';
import 'package:flutter_go/utils/analytics.dart' as Analytics;
import 'package:flutter_go/views/login_page/login_page.dart';
import 'package:flutter_go/utils/data_utils.dart';
import 'package:flutter_go/model/user_info.dart';
import 'package:flutter_jpush/flutter_jpush.dart';
import 'package:flutter_go/event/event_bus.dart';
import 'package:flutter_go/event/event_model.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter_go/model/widget.dart';
import 'package:flutter_go/standard_pages/index.dart';
//import 'views/welcome_page/index.dart';
import 'package:flutter_go/utils/net_utils.dart';
SpUtil sp;
var db;
class MyApp extends StatefulWidget {
MyApp() {
final router = new Router();
Routes.configureRoutes(router);
// 这里设置项目环境
Application.router = router;
}
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _hasLogin = false;
bool _isLoading = true;
UserInformation _userInfo;
bool isConnected = false;
String registrationId;
List notificationList = [];
int themeColor = 0xFFC91B3A;
_MyAppState() {
final eventBus = new EventBus();
ApplicationEvent.event = eventBus;
}
/// 服务端控制是否显示业界动态
Future _reqsMainPageIsOpen() async {
const reqs = 'https://flutter-go.pub/api/isInfoOpen';
var response;
try {
response = await NetUtils.get(reqs, {});
print('response-$response');
if (response['status'] == 200 &&
response['success'] == true &&
response['data'] is Map &&
response['data']['isOpen'] == true) {
Application.pageIsOpen = true;
print('是否需要展开【业界动态】${Application.pageIsOpen}');
}
} catch (e) {
print('response-$e');
}
return response;
}
@override
void initState() {
super.initState();
_reqsMainPageIsOpen();
_startupJpush();
FlutterJPush.addConnectionChangeListener((bool connected) {
setState(() {
/// 是否连接,连接了才可以推送
print("连接状态改变:$connected");
this.isConnected = connected;
if (connected) {
//在启动的时候会去连接自己的服务器,连接并注册成功之后会返回一个唯一的设备号
try {
FlutterJPush.getRegistrationID().then((String regId) {
print("主动获取设备号:$regId");
setState(() {
this.registrationId = regId;
});
});
} catch (error) {
print('主动获取设备号Error:$error');
}
}
});
});
FlutterJPush.addReceiveNotificationListener(
(JPushNotification notification) {
setState(() {
/// 收到推送
print("收到推送提醒: $notification");
notificationList.add(notification);
});
});
FlutterJPush.addReceiveOpenNotificationListener(
(JPushNotification notification) {
setState(() {
print("打开了推送提醒: $notification");
/// 打开了推送提醒
notificationList.add(notification);
});
});
FlutterJPush.addReceiveCustomMsgListener((JPushMessage msg) {
setState(() {
print("收到推送消息提醒: $msg");
/// 打开了推送提醒
notificationList.add(msg);
});
});
DataUtils.checkLogin().then((hasLogin) {
if (hasLogin.runtimeType == UserInformation) {
setState(() {
_hasLogin = true;
_isLoading = false;
_userInfo = hasLogin;
// 设置初始化的主题色
// if (hasLogin.themeColor != 'default') {
// themeColor = int.parse(hasLogin.themeColor);
// }
});
} else {
setState(() {
_hasLogin = hasLogin;
_isLoading = false;
});
}
}).catchError((onError) {
setState(() {
_hasLogin = false;
_isLoading = false;
});
print('身份信息验证失败:$onError');
});
ApplicationEvent.event.on<UserSettingThemeColorEvent>().listen((event) {
print('接收到的 event $event');
});
}
showWelcomePage() {
if (_isLoading) {
return Container(
color: Color(this.themeColor),
child: Center(
child: SpinKitPouringHourglass(color: Colors.white),
),
);
} else {
// 判断是否已经登录
if (_hasLogin) {
return AppPage(_userInfo);
} else {
return LoginPage();
}
}
}
@override
Widget build(BuildContext context) {
// WidgetTree.getCommonItemByPath([15, 17], Application.widgetTree);
return new MaterialApp(
title: 'titles',
theme: new ThemeData(
primaryColor: Color(this.themeColor),
backgroundColor: Color(0xFFEFEFEF),
accentColor: Color(0xFF888888),
textTheme: TextTheme(
//设置Material的默认字体样式
body1: TextStyle(color: Color(0xFF888888), fontSize: 16.0),
),
iconTheme: IconThemeData(
color: Color(this.themeColor),
size: 35.0,
),
),
home: new Scaffold(body: showWelcomePage()),
debugShowCheckedModeBanner: false,
onGenerateRoute: Application.router.generator,
navigatorObservers: <NavigatorObserver>[Analytics.observer],
);
}
}
void _startupJpush() async {
print("初始化jpush");
await FlutterJPush.startup();
print("初始化jpush成功");
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final provider = new Provider();
await provider.init(true);
sp = await SpUtil.getInstance();
new SearchHistoryList(sp);
await DataUtils.getWidgetTreeList().then((List json) {
List data =
WidgetTree.insertDevPagesToList(json, StandardPages().getLocalList());
Application.widgetTree = WidgetTree.buildWidgetTree(data);
print("Application.widgetTree>>>> ${Application.widgetTree}");
});
db = Provider.db;
runApp(new MyApp());
}