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
151 lines (136 loc) · 4.72 KB
/
main.dart
File metadata and controls
151 lines (136 loc) · 4.72 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
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gsy_github_app_flutter/common/event/HttpErrorEvent.dart';
import 'package:gsy_github_app_flutter/common/localization/GSYLocalizationsDelegate.dart';
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
import 'package:gsy_github_app_flutter/common/model/User.dart';
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
import 'package:gsy_github_app_flutter/page/HomePage.dart';
import 'package:gsy_github_app_flutter/page/LoginPage.dart';
import 'package:gsy_github_app_flutter/page/WelcomePage.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:gsy_github_app_flutter/common/net/Code.dart';
void main() {
runApp(new FlutterReduxApp());
PaintingBinding.instance.imageCache.maximumSize = 100;
}
class FlutterReduxApp extends StatelessWidget {
/// 创建Store,引用 GSYState 中的 appReducer 实现 Reducer 方法
/// initialState 初始化 State
final store = new Store<GSYState>(
appReducer,
///初始化数据
initialState: new GSYState(
userInfo: User.empty(),
eventList: new List(),
trendList: new List(),
themeData: new ThemeData(
primarySwatch: GSYColors.primarySwatch,
platform: TargetPlatform.iOS//滑动返回
),
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: new HomePage(),
);
},
LoginPage.sName: (context) {
return new GSYLocalizations(
child: 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 = Code.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;
}
}
}