forked from CarGuo/gsy_github_app_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
166 lines (149 loc) · 5.09 KB
/
main.dart
File metadata and controls
166 lines (149 loc) · 5.09 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
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gsy_github_app_flutter/common/event/http_error_event.dart';
import 'package:gsy_github_app_flutter/common/event/index.dart';
import 'package:gsy_github_app_flutter/common/localization/gsy_localizations_delegate.dart';
import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
import 'package:gsy_github_app_flutter/common/model/User.dart';
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart';
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
import 'package:gsy_github_app_flutter/page/home_page.dart';
import 'package:gsy_github_app_flutter/page/login_page.dart';
import 'package:gsy_github_app_flutter/page/welcome_page.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:provider/provider.dart';
import 'package:redux/redux.dart';
import 'package:gsy_github_app_flutter/common/net/code.dart';
import 'common/event/index.dart';
import 'common/utils/navigator_utils.dart';
void main() {
runZoned(() {
runApp(FlutterReduxApp());
PaintingBinding.instance.imageCache.maximumSize = 100;
Provider.debugCheckInvalidValueType = null;
}, onError: (Object obj, StackTrace stack) {
print(obj);
print(stack);
});
}
class FlutterReduxApp extends StatelessWidget {
/// 创建Store,引用 GSYState 中的 appReducer 实现 Reducer 方法
/// initialState 初始化 State
final store = new Store<GSYState>(
appReducer,
middleware: middleware,
///初始化数据
initialState: new GSYState(
userInfo: User.empty(),
themeData: CommonUtils.getThemeData(GSYColors.primarySwatch),
locale: Locale('zh', 'CH')),
);
FlutterReduxApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
/// 通过 StoreProvider 应用 store
return new StoreProvider(
store: store,
child: new StoreBuilder<GSYState>(builder: (context, store) {
return new MaterialApp(
///多语言实现代理
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GSYLocalizationsDelegate.delegate,
],
locale: store.state.locale,
supportedLocales: [store.state.locale],
theme: store.state.themeData,
routes: {
WelcomePage.sName: (context) {
store.state.platformLocale = Localizations.localeOf(context);
return WelcomePage();
},
HomePage.sName: (context) {
///通过 Localizations.override 包裹一层,
return new GSYLocalizations(
child: NavigatorUtils.pageContainer(new HomePage()));
},
LoginPage.sName: (context) {
return new GSYLocalizations(
child: NavigatorUtils.pageContainer(new LoginPage()));
},
});
}),
);
}
}
class GSYLocalizations extends StatefulWidget {
final Widget child;
GSYLocalizations({Key key, this.child}) : super(key: key);
@override
State<GSYLocalizations> createState() {
return new _GSYLocalizations();
}
}
class _GSYLocalizations extends State<GSYLocalizations> {
StreamSubscription stream;
@override
Widget build(BuildContext context) {
return new StoreBuilder<GSYState>(builder: (context, store) {
///通过 StoreBuilder 和 Localizations 实现实时多语言切换
return new Localizations.override(
context: context,
locale: store.state.locale,
child: widget.child,
);
});
}
@override
void initState() {
super.initState();
///Stream演示event bus
stream = eventBus.on<HttpErrorEvent>().listen((event) {
errorHandleFunction(event.code, event.message);
});
}
@override
void dispose() {
super.dispose();
if (stream != null) {
stream.cancel();
stream = null;
}
}
///网络错误提醒
errorHandleFunction(int code, message) {
switch (code) {
case Code.NETWORK_ERROR:
Fluttertoast.showToast(
msg: CommonUtils.getLocale(context).network_error);
break;
case 401:
Fluttertoast.showToast(
msg: CommonUtils.getLocale(context).network_error_401);
break;
case 403:
Fluttertoast.showToast(
msg: CommonUtils.getLocale(context).network_error_403);
break;
case 404:
Fluttertoast.showToast(
msg: CommonUtils.getLocale(context).network_error_404);
break;
case Code.NETWORK_TIMEOUT:
//超时
Fluttertoast.showToast(
msg: CommonUtils.getLocale(context).network_error_timeout);
break;
default:
Fluttertoast.showToast(
msg: CommonUtils.getLocale(context).network_error_unknown +
" " +
message);
break;
}
}
}