@@ -3,20 +3,20 @@ import 'dart:async';
3
3
import 'package:flutter/material.dart' ;
4
4
import 'package:flutter_localizations/flutter_localizations.dart' ;
5
5
import 'package:flutter_redux/flutter_redux.dart' ;
6
+ import 'package:flutter_riverpod/flutter_riverpod.dart' ;
6
7
import 'package:fluttertoast/fluttertoast.dart' ;
7
8
import 'package:gsy_github_app_flutter/common/event/http_error_event.dart' ;
8
9
import 'package:gsy_github_app_flutter/common/event/index.dart' ;
9
10
import 'package:gsy_github_app_flutter/common/localization/default_localizations.dart' ;
10
11
import 'package:gsy_github_app_flutter/common/localization/gsy_localizations_delegate.dart' ;
11
12
import 'package:gsy_github_app_flutter/common/net/code.dart' ;
12
- import 'package:gsy_github_app_flutter/common/style/gsy_style.dart' ;
13
- import 'package:gsy_github_app_flutter/common/utils/common_utils.dart' ;
14
13
import 'package:gsy_github_app_flutter/model/User.dart' ;
15
14
import 'package:gsy_github_app_flutter/page/debug/debug_label.dart' ;
16
15
import 'package:gsy_github_app_flutter/page/home/home_page.dart' ;
17
16
import 'package:gsy_github_app_flutter/page/login/login_page.dart' ;
18
17
import 'package:gsy_github_app_flutter/page/photoview_page.dart' ;
19
18
import 'package:gsy_github_app_flutter/page/welcome_page.dart' ;
19
+ import 'package:gsy_github_app_flutter/provider/app_state_provider.dart' ;
20
20
import 'package:gsy_github_app_flutter/redux/gsy_state.dart' ;
21
21
import 'package:redux/redux.dart' ;
22
22
@@ -41,23 +41,13 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
41
41
42
42
///初始化数据
43
43
initialState: GSYState (
44
- userInfo: User .empty (),
45
- login: false ,
46
- themeData: CommonUtils .getThemeData (GSYColors .primarySwatch),
47
- locale: const Locale ('en' , 'US' )),
44
+ userInfo: User .empty (),
45
+ login: false ,
46
+ ),
48
47
);
49
48
50
- ColorFilter greyscale = const ColorFilter .matrix (< double > [
51
- 0.2126 , 0.7152 , 0.0722 , 0 , 0 ,
52
- 0.2126 , 0.7152 , 0.0722 , 0 , 0 ,
53
- 0.2126 , 0.7152 , 0.0722 , 0 , 0 ,
54
- 0 , 0 , 0 , 1 , 0 ,
55
- ]);
56
-
57
-
58
49
NavigatorObserver navigatorObserver = NavigatorObserver ();
59
50
60
-
61
51
@override
62
52
void initState () {
63
53
super .initState ();
@@ -73,72 +63,84 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
73
63
74
64
@override
75
65
Widget build (BuildContext context) {
76
- /// 使用 flutter_redux 做全局状态共享
77
- /// 通过 StoreProvider 应用 store
78
- return StoreProvider (
79
- store: store,
80
- child: StoreBuilder <GSYState >(builder: (context, store) {
81
- ///使用 StoreBuilder 获取 store 中的 theme 、locale
82
- store.state.platformLocale = WidgetsBinding .instance.platformDispatcher.locale;
83
- Widget app = MaterialApp (
84
- navigatorKey: navKey,
85
- ///多语言实现代理
86
- localizationsDelegates: [
87
- GlobalMaterialLocalizations .delegate,
88
- GlobalCupertinoLocalizations .delegate,
89
- GlobalWidgetsLocalizations .delegate,
90
- GSYLocalizationsDelegate .delegate,
91
- ],
92
- supportedLocales: [
93
- store.state.locale ?? store.state.platformLocale ?? const Locale ('en' , 'US' )
94
- ],
95
- locale: store.state.locale,
96
- theme: store.state.themeData,
97
- navigatorObservers: [navigatorObserver],
98
-
99
- ///命名式路由
100
- /// "/" 和 MaterialApp 的 home 参数一个效果
101
- ///⚠️ 这里的 name调用,里面 pageContainer 方法有一个 MediaQuery.of(context).copyWith(textScaleFactor: 1),
102
- ///⚠️ 而这里的 context 用的是 WidgetBuilder 的 context ~
103
- ///⚠️ 所以 MediaQuery.of(context) 这个 InheritedWidget 就把这个 context “登记”到了 Element 的内部静态 _map 里。
104
- ///⚠️ 所以键盘弹出来的时候,触发了顶层的 MediaQueryData 发生变化,自然就触发了“登记”过的 context 的变化
105
- ///⚠️ 比如 LoginPage 、HomePage ····
106
- ///⚠️ 所以比如你在 搜索页面 键盘弹出时,下面的 HomePage.sName 对应的 WidgetBuilder 会被触发
107
- ///⚠️ 这个是我故意的,如果不需要,可以去掉 pageContainer 或者不要用这里的 context
108
- routes: {
109
- WelcomePage .sName: (context) {
110
- DebugLabel .showDebugLabel (context);
111
- return const WelcomePage ();
112
- },
113
- HomePage .sName: (context) {
114
- return NavigatorUtils .pageContainer (const HomePage (), context);
115
- },
116
- LoginPage .sName: (context) {
117
- return NavigatorUtils .pageContainer (const LoginPage (), context);
118
- },
119
-
120
- ///使用 ModalRoute.of(context).settings.arguments; 获取参数
121
- PhotoViewPage .sName: (context) {
122
- return const PhotoViewPage ();
123
- },
124
- });
125
-
126
- if (store.state.grey) {
127
- ///mode one
128
- app = ColorFiltered (
129
- colorFilter: const ColorFilter .mode (Colors .grey, BlendMode .saturation),
130
- child: app);
131
- ///mode tow
132
- // app = ColorFiltered(
133
- // colorFilter: greyscale,
134
- // child: app);
135
- }
136
-
137
- return app;
138
- }),
66
+ /// 使用 riverpod 做部分状态共享
67
+ /// 这里是为了展示使用 riverpod 的能力所以使用了多种状态管理
68
+ return UncontrolledProviderScope (
69
+ container: globalContainer,
70
+ child: Consumer (
71
+ builder: (BuildContext context, WidgetRef ref, Widget ? child) {
72
+ final (greyApp, appLocale, themeData) = ref.watch (appStateProvider);
73
+
74
+ /// 使用 flutter_redux 做部分状态共享
75
+ /// 通过 StoreProvider 应用 store
76
+ /// 这里是为了展示使用 flutter_redux 的能力所以使用了多种状态管理
77
+ return StoreProvider (
78
+ store: store,
79
+ child: StoreBuilder <GSYState >(builder: (context, store) {
80
+ Widget app = MaterialApp (
81
+ navigatorKey: navKey,
82
+
83
+ ///多语言实现代理
84
+ localizationsDelegates: [
85
+ GlobalMaterialLocalizations .delegate,
86
+ GlobalCupertinoLocalizations .delegate,
87
+ GlobalWidgetsLocalizations .delegate,
88
+ GSYLocalizationsDelegate .delegate,
89
+ ],
90
+ supportedLocales: [appLocale],
91
+ locale: appLocale,
92
+ theme: themeData,
93
+ navigatorObservers: [navigatorObserver],
94
+
95
+ ///命名式路由
96
+ /// "/" 和 MaterialApp 的 home 参数一个效果
97
+ ///⚠️ 这里的 name调用,里面 pageContainer 方法有一个 MediaQuery.of(context).copyWith(textScaleFactor: 1),
98
+ ///⚠️ 而这里的 context 用的是 WidgetBuilder 的 context ~
99
+ ///⚠️ 所以 MediaQuery.of(context) 这个 InheritedWidget 就把这个 context “登记”到了 Element 的内部静态 _map 里。
100
+ ///⚠️ 所以键盘弹出来的时候,触发了顶层的 MediaQueryData 发生变化,自然就触发了“登记”过的 context 的变化
101
+ ///⚠️ 比如 LoginPage 、HomePage ····
102
+ ///⚠️ 所以比如你在 搜索页面 键盘弹出时,下面的 HomePage.sName 对应的 WidgetBuilder 会被触发
103
+ ///⚠️ 这个是我故意的,如果不需要,可以去掉 pageContainer 或者不要用这里的 context
104
+ routes: {
105
+ WelcomePage .sName: (context) {
106
+ DebugLabel .showDebugLabel (context);
107
+ return const WelcomePage ();
108
+ },
109
+ HomePage .sName: (context) {
110
+ return NavigatorUtils .pageContainer (
111
+ const HomePage (), context);
112
+ },
113
+ LoginPage .sName: (context) {
114
+ return NavigatorUtils .pageContainer (
115
+ const LoginPage (), context);
116
+ },
117
+
118
+ ///使用 ModalRoute.of(context).settings.arguments; 获取参数
119
+ PhotoViewPage .sName: (context) {
120
+ return const PhotoViewPage ();
121
+ },
122
+ });
123
+
124
+ if (greyApp) {
125
+ ///mode one
126
+ app = ColorFiltered (
127
+ colorFilter: const ColorFilter .mode (
128
+ Colors .grey, BlendMode .saturation),
129
+ child: app);
130
+
131
+ ///mode two
132
+ // app = ColorFiltered(
133
+ // colorFilter: greyscale,
134
+ // child: app);
135
+ }
136
+
137
+ return app;
138
+ }),
139
+ );
140
+ },
141
+ ),
139
142
);
140
143
}
141
-
142
144
}
143
145
144
146
mixin HttpErrorListener on State <FlutterReduxApp > {
@@ -185,15 +187,16 @@ mixin HttpErrorListener on State<FlutterReduxApp> {
185
187
showToast (GSYLocalizations .i18n (context)! .network_error_422);
186
188
break ;
187
189
case Code .NETWORK_TIMEOUT :
188
- //超时
190
+ //超时
189
191
showToast (GSYLocalizations .i18n (context)! .network_error_timeout);
190
192
break ;
191
193
case Code .GITHUB_API_REFUSED :
192
- //Github API 异常
194
+ //Github API 异常
193
195
showToast (GSYLocalizations .i18n (context)! .github_refused);
194
196
break ;
195
197
default :
196
- showToast ("${GSYLocalizations .i18n (context )!.network_error_unknown } $message " );
198
+ showToast (
199
+ "${GSYLocalizations .i18n (context )!.network_error_unknown } $message " );
197
200
break ;
198
201
}
199
202
}
@@ -205,4 +208,3 @@ mixin HttpErrorListener on State<FlutterReduxApp> {
205
208
toastLength: Toast .LENGTH_LONG );
206
209
}
207
210
}
208
-
0 commit comments