1+ import 'dart:async' ;
2+
13import 'package:flutter/foundation.dart' ;
24import 'package:flutter/material.dart' ;
5+ import 'package:event_bus/event_bus.dart' ;
36import 'package:flutter_localizations/flutter_localizations.dart' ;
7+ import 'package:fluttertoast/fluttertoast.dart' ;
8+ import 'package:gsy_github_app_flutter/common/event/HttpErrorEvent.dart' ;
49import 'package:gsy_github_app_flutter/common/localization/GSYLocalizationsDelegate.dart' ;
510import 'package:gsy_github_app_flutter/common/redux/GSYState.dart' ;
611import 'package:gsy_github_app_flutter/common/model/User.dart' ;
712import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart' ;
13+ import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart' ;
814import 'package:gsy_github_app_flutter/page/HomePage.dart' ;
915import 'package:gsy_github_app_flutter/page/LoginPage.dart' ;
1016import 'package:gsy_github_app_flutter/page/WelcomePage.dart' ;
1117import 'package:flutter_redux/flutter_redux.dart' ;
1218import 'package:redux/redux.dart' ;
19+ import 'package:gsy_github_app_flutter/common/net/Code.dart' ;
1320
1421void main () {
1522 runApp (new FlutterReduxApp ());
@@ -45,9 +52,7 @@ class FlutterReduxApp extends StatelessWidget {
4552 GSYLocalizationsDelegate .delegate,
4653 ],
4754 locale: store.state.locale,
48- supportedLocales: [
49- store.state.locale
50- ],
55+ supportedLocales: [store.state.locale],
5156 theme: store.state.themeData,
5257 routes: {
5358 WelcomePage .sName: (context) {
@@ -82,6 +87,10 @@ class GSYLocalizations extends StatefulWidget {
8287}
8388
8489class _GSYLocalizations extends State <GSYLocalizations > {
90+
91+
92+ StreamSubscription stream;
93+
8594 @override
8695 Widget build (BuildContext context) {
8796 return new StoreBuilder <GSYState >(builder: (context, store) {
@@ -92,4 +101,45 @@ class _GSYLocalizations extends State<GSYLocalizations> {
92101 );
93102 });
94103 }
104+
105+ @override
106+ void initState () {
107+ super .initState ();
108+ stream = Code .eventBus.on < HttpErrorEvent > ().listen ((event) {
109+ errorHandleFunction (event.code, event.message);
110+ });
111+ }
112+
113+ @override
114+ void dispose () {
115+ super .dispose ();
116+ if (stream != null ) {
117+ stream.cancel ();
118+ stream = null ;
119+ }
120+ }
121+
122+ errorHandleFunction (int code, message) {
123+ switch (code) {
124+ case Code .NETWORK_ERROR :
125+ Fluttertoast .showToast (msg: CommonUtils .getLocale (context).network_error);
126+ break ;
127+ case 401 :
128+ Fluttertoast .showToast (msg: CommonUtils .getLocale (context).network_error_401);
129+ break ;
130+ case 403 :
131+ Fluttertoast .showToast (msg: CommonUtils .getLocale (context).network_error_403);
132+ break ;
133+ case 404 :
134+ Fluttertoast .showToast (msg: CommonUtils .getLocale (context).network_error_404);
135+ break ;
136+ case Code .NETWORK_TIMEOUT :
137+ //超时
138+ Fluttertoast .showToast (msg: CommonUtils .getLocale (context).network_error_timeout);
139+ break ;
140+ default :
141+ Fluttertoast .showToast (msg: CommonUtils .getLocale (context).network_error_unknown + " " + message);
142+ break ;
143+ }
144+ }
95145}
0 commit comments