Skip to content

Commit c321888

Browse files
committed
增加新的状态管理 riverpod 展示
调整旧版本状态管理 调整部分代码
1 parent 2031bcd commit c321888

File tree

76 files changed

+1634
-1634
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1634
-1634
lines changed

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ include: package:flutter_lints/flutter.yaml
33
analyzer:
44
errors:
55
mixin_inherits_from_not_object: ignore
6+
plugins:
7+
- custom_lint
68

79
linter:
810
rules:

lib/app.dart

Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_localizations/flutter_localizations.dart';
55
import 'package:flutter_redux/flutter_redux.dart';
6+
import 'package:flutter_riverpod/flutter_riverpod.dart';
67
import 'package:fluttertoast/fluttertoast.dart';
78
import 'package:gsy_github_app_flutter/common/event/http_error_event.dart';
89
import 'package:gsy_github_app_flutter/common/event/index.dart';
910
import 'package:gsy_github_app_flutter/common/localization/default_localizations.dart';
1011
import 'package:gsy_github_app_flutter/common/localization/gsy_localizations_delegate.dart';
1112
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';
1413
import 'package:gsy_github_app_flutter/model/User.dart';
1514
import 'package:gsy_github_app_flutter/page/debug/debug_label.dart';
1615
import 'package:gsy_github_app_flutter/page/home/home_page.dart';
1716
import 'package:gsy_github_app_flutter/page/login/login_page.dart';
1817
import 'package:gsy_github_app_flutter/page/photoview_page.dart';
1918
import 'package:gsy_github_app_flutter/page/welcome_page.dart';
19+
import 'package:gsy_github_app_flutter/provider/app_state_provider.dart';
2020
import 'package:gsy_github_app_flutter/redux/gsy_state.dart';
2121
import 'package:redux/redux.dart';
2222

@@ -41,23 +41,13 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
4141

4242
///初始化数据
4343
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+
),
4847
);
4948

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-
5849
NavigatorObserver navigatorObserver = NavigatorObserver();
5950

60-
6151
@override
6252
void initState() {
6353
super.initState();
@@ -73,72 +63,84 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
7363

7464
@override
7565
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+
),
139142
);
140143
}
141-
142144
}
143145

144146
mixin HttpErrorListener on State<FlutterReduxApp> {
@@ -185,15 +187,16 @@ mixin HttpErrorListener on State<FlutterReduxApp> {
185187
showToast(GSYLocalizations.i18n(context)!.network_error_422);
186188
break;
187189
case Code.NETWORK_TIMEOUT:
188-
//超时
190+
//超时
189191
showToast(GSYLocalizations.i18n(context)!.network_error_timeout);
190192
break;
191193
case Code.GITHUB_API_REFUSED:
192-
//Github API 异常
194+
//Github API 异常
193195
showToast(GSYLocalizations.i18n(context)!.github_refused);
194196
break;
195197
default:
196-
showToast("${GSYLocalizations.i18n(context)!.network_error_unknown} $message");
198+
showToast(
199+
"${GSYLocalizations.i18n(context)!.network_error_unknown} $message");
197200
break;
198201
}
199202
}
@@ -205,4 +208,3 @@ mixin HttpErrorListener on State<FlutterReduxApp> {
205208
toastLength: Toast.LENGTH_LONG);
206209
}
207210
}
208-

lib/common/net/transformer.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/common/dao/event_dao.dart renamed to lib/common/repositories/event_repository.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import 'dart:convert';
22

33
import 'package:gsy_github_app_flutter/db/provider/event/received_event_db_provider.dart';
44
import 'package:gsy_github_app_flutter/db/provider/event/user_event_db_provider.dart';
5-
import 'package:gsy_github_app_flutter/common/dao/dao_result.dart';
5+
import 'package:gsy_github_app_flutter/common/repositories/data_result.dart';
66
import 'package:gsy_github_app_flutter/model/Event.dart';
77
import 'package:gsy_github_app_flutter/common/net/address.dart';
88
import 'package:gsy_github_app_flutter/common/net/api.dart';
99

10-
class EventDao {
10+
class EventRepository {
1111
static getEventReceived(String? userName,
1212
{page = 1, bool needDb = false}) async {
1313
if (userName == null) {
@@ -50,7 +50,7 @@ class EventDao {
5050
}
5151

5252
/// 用户行为事件
53-
static getEventDao(userName, {page = 0, bool needDb = false}) async {
53+
static getEventRequest(userName, {page = 0, bool needDb = false}) async {
5454
UserEventDbProvider provider = UserEventDbProvider();
5555
next() async {
5656
String url =

lib/common/dao/issue_dao.dart renamed to lib/common/repositories/issue_repository.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:dio/dio.dart';
44
import 'package:gsy_github_app_flutter/db/provider/issue/issue_comment_db_provider.dart';
55
import 'package:gsy_github_app_flutter/db/provider/issue/issue_detail_db_provider.dart';
66
import 'package:gsy_github_app_flutter/db/provider/repos/repository_issue_db_provider.dart';
7-
import 'package:gsy_github_app_flutter/common/dao/dao_result.dart';
7+
import 'package:gsy_github_app_flutter/common/repositories/data_result.dart';
88
import 'package:gsy_github_app_flutter/model/Issue.dart';
99
import 'package:gsy_github_app_flutter/common/net/address.dart';
1010
import 'package:gsy_github_app_flutter/common/net/api.dart';
@@ -13,15 +13,15 @@ import 'package:gsy_github_app_flutter/common/net/api.dart';
1313
/// Created by guoshuyu
1414
/// Date: 2018-07-19
1515
16-
class IssueDao {
16+
class IssueRepository {
1717
/// 获取仓库issue
1818
/// @param page
1919
/// @param userName
2020
/// @param repository
2121
/// @param state issue状态
2222
/// @param sort 排序类型 created updated等
2323
/// @param direction 正序或者倒序
24-
static getRepositoryIssueDao(userName, repository, state,
24+
static getRepositoryIssueRequest(userName, repository, state,
2525
{sort, direction, page = 0, needDb = false}) async {
2626
String? fullName = "$userName/$repository";
2727
String dbState = state ?? "*";
@@ -74,7 +74,7 @@ class IssueDao {
7474
/// @param reposName 仓库名
7575
/// @param page
7676
/// @param state 问题状态,all open closed
77-
static searchRepositoryIssue(q, name, reposName, state, {page = 1}) async {
77+
static searchRepositoryRequest(q, name, reposName, state, {page = 1}) async {
7878
String? qu;
7979
if (state == null || state == 'all') {
8080
qu = q + "+repo%3A$name%2F$reposName";
@@ -100,7 +100,7 @@ class IssueDao {
100100
}
101101

102102
/// issue的详请
103-
static getIssueInfoDao(userName, repository, number, {needDb = true}) async {
103+
static getIssueInfoRequest(userName, repository, number, {needDb = true}) async {
104104
String? fullName = "$userName/$repository";
105105

106106
IssueDetailDbProvider provider = IssueDetailDbProvider();
@@ -132,7 +132,7 @@ class IssueDao {
132132
}
133133

134134
/// issue的详请列表
135-
static getIssueCommentDao(userName, repository, number,
135+
static getIssueCommentRequest(userName, repository, number,
136136
{page = 0, needDb = false}) async {
137137
String? fullName = "$userName/$repository";
138138
IssueCommentDbProvider provider = IssueCommentDbProvider();
@@ -173,7 +173,7 @@ class IssueDao {
173173
}
174174

175175
/// 增加issue的回复
176-
static addIssueCommentDao(userName, repository, number, comment) async {
176+
static addIssueCommentRequest(userName, repository, number, comment) async {
177177
String url = Address.addIssueComment(userName, repository, number);
178178
var res = await httpManager.netFetch(
179179
url,
@@ -188,7 +188,7 @@ class IssueDao {
188188
}
189189

190190
/// 编辑issue
191-
static editIssueDao(userName, repository, number, issue) async {
191+
static editIssueRequest(userName, repository, number, issue) async {
192192
String url = Address.editIssue(userName, repository, number);
193193
var res = await httpManager.netFetch(
194194
url,
@@ -203,7 +203,7 @@ class IssueDao {
203203
}
204204

205205
/// 锁定issue
206-
static lockIssueDao(userName, repository, number, locked) async {
206+
static lockIssueRequest(userName, repository, number, locked) async {
207207
String url = Address.lockIssue(userName, repository, number);
208208
var res = await httpManager.netFetch(
209209
url,
@@ -219,7 +219,7 @@ class IssueDao {
219219
}
220220

221221
/// 创建issue
222-
static createIssueDao(userName, repository, issue) async {
222+
static createIssueRequest(userName, repository, issue) async {
223223
String url = Address.createIssue(userName, repository);
224224
var res = await httpManager.netFetch(
225225
url,
@@ -234,7 +234,7 @@ class IssueDao {
234234
}
235235

236236
/// 编辑issue回复
237-
static editCommentDao(
237+
static editCommentRequest(
238238
userName, repository, number, commentId, comment) async {
239239
String url = Address.editComment(userName, repository, commentId);
240240
var res = await httpManager.netFetch(
@@ -250,7 +250,7 @@ class IssueDao {
250250
}
251251

252252
/// 删除issue回复
253-
static deleteCommentDao(userName, repository, number, commentId) async {
253+
static deleteCommentRequest(userName, repository, number, commentId) async {
254254
String url = Address.editComment(userName, repository, commentId);
255255
var res = await httpManager
256256
.netFetch(url, null, null, Options(method: 'DELETE'), noTip: true);

0 commit comments

Comments
 (0)