Skip to content

Commit

Permalink
增加新的状态管理 riverpod 展示
Browse files Browse the repository at this point in the history
调整旧版本状态管理
调整部分代码
  • Loading branch information
CarGuo committed Feb 26, 2025
1 parent 2031bcd commit c321888
Show file tree
Hide file tree
Showing 76 changed files with 1,634 additions and 1,634 deletions.
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ include: package:flutter_lints/flutter.yaml
analyzer:
errors:
mixin_inherits_from_not_object: ignore
plugins:
- custom_lint

linter:
rules:
Expand Down
168 changes: 85 additions & 83 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_riverpod/flutter_riverpod.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/default_localizations.dart';
import 'package:gsy_github_app_flutter/common/localization/gsy_localizations_delegate.dart';
import 'package:gsy_github_app_flutter/common/net/code.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/model/User.dart';
import 'package:gsy_github_app_flutter/page/debug/debug_label.dart';
import 'package:gsy_github_app_flutter/page/home/home_page.dart';
import 'package:gsy_github_app_flutter/page/login/login_page.dart';
import 'package:gsy_github_app_flutter/page/photoview_page.dart';
import 'package:gsy_github_app_flutter/page/welcome_page.dart';
import 'package:gsy_github_app_flutter/provider/app_state_provider.dart';
import 'package:gsy_github_app_flutter/redux/gsy_state.dart';
import 'package:redux/redux.dart';

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

///初始化数据
initialState: GSYState(
userInfo: User.empty(),
login: false,
themeData: CommonUtils.getThemeData(GSYColors.primarySwatch),
locale: const Locale('en', 'US')),
userInfo: User.empty(),
login: false,
),
);

ColorFilter greyscale = const ColorFilter.matrix(<double>[
0.2126, 0.7152, 0.0722, 0, 0,
0.2126, 0.7152, 0.0722, 0, 0,
0.2126, 0.7152, 0.0722, 0, 0,
0, 0, 0, 1, 0,
]);


NavigatorObserver navigatorObserver = NavigatorObserver();


@override
void initState() {
super.initState();
Expand All @@ -73,72 +63,84 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>

@override
Widget build(BuildContext context) {
/// 使用 flutter_redux 做全局状态共享
/// 通过 StoreProvider 应用 store
return StoreProvider(
store: store,
child: StoreBuilder<GSYState>(builder: (context, store) {
///使用 StoreBuilder 获取 store 中的 theme 、locale
store.state.platformLocale = WidgetsBinding.instance.platformDispatcher.locale;
Widget app = MaterialApp(
navigatorKey: navKey,
///多语言实现代理
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GSYLocalizationsDelegate.delegate,
],
supportedLocales: [
store.state.locale ?? store.state.platformLocale ?? const Locale('en', 'US')
],
locale: store.state.locale,
theme: store.state.themeData,
navigatorObservers: [navigatorObserver],

///命名式路由
/// "/" 和 MaterialApp 的 home 参数一个效果
///⚠️ 这里的 name调用,里面 pageContainer 方法有一个 MediaQuery.of(context).copyWith(textScaleFactor: 1),
///⚠️ 而这里的 context 用的是 WidgetBuilder 的 context ~
///⚠️ 所以 MediaQuery.of(context) 这个 InheritedWidget 就把这个 context “登记”到了 Element 的内部静态 _map 里。
///⚠️ 所以键盘弹出来的时候,触发了顶层的 MediaQueryData 发生变化,自然就触发了“登记”过的 context 的变化
///⚠️ 比如 LoginPage 、HomePage ····
///⚠️ 所以比如你在 搜索页面 键盘弹出时,下面的 HomePage.sName 对应的 WidgetBuilder 会被触发
///⚠️ 这个是我故意的,如果不需要,可以去掉 pageContainer 或者不要用这里的 context
routes: {
WelcomePage.sName: (context) {
DebugLabel.showDebugLabel(context);
return const WelcomePage();
},
HomePage.sName: (context) {
return NavigatorUtils.pageContainer(const HomePage(), context);
},
LoginPage.sName: (context) {
return NavigatorUtils.pageContainer(const LoginPage(), context);
},

///使用 ModalRoute.of(context).settings.arguments; 获取参数
PhotoViewPage.sName: (context) {
return const PhotoViewPage();
},
});

if (store.state.grey) {
///mode one
app = ColorFiltered(
colorFilter: const ColorFilter.mode(Colors.grey, BlendMode.saturation),
child: app);
///mode tow
// app = ColorFiltered(
// colorFilter: greyscale,
// child: app);
}

return app;
}),
/// 使用 riverpod 做部分状态共享
/// 这里是为了展示使用 riverpod 的能力所以使用了多种状态管理
return UncontrolledProviderScope(
container: globalContainer,
child: Consumer(
builder: (BuildContext context, WidgetRef ref, Widget? child) {
final (greyApp, appLocale, themeData) = ref.watch(appStateProvider);

/// 使用 flutter_redux 做部分状态共享
/// 通过 StoreProvider 应用 store
/// 这里是为了展示使用 flutter_redux 的能力所以使用了多种状态管理
return StoreProvider(
store: store,
child: StoreBuilder<GSYState>(builder: (context, store) {
Widget app = MaterialApp(
navigatorKey: navKey,

///多语言实现代理
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GSYLocalizationsDelegate.delegate,
],
supportedLocales: [appLocale],
locale: appLocale,
theme: themeData,
navigatorObservers: [navigatorObserver],

///命名式路由
/// "/" 和 MaterialApp 的 home 参数一个效果
///⚠️ 这里的 name调用,里面 pageContainer 方法有一个 MediaQuery.of(context).copyWith(textScaleFactor: 1),
///⚠️ 而这里的 context 用的是 WidgetBuilder 的 context ~
///⚠️ 所以 MediaQuery.of(context) 这个 InheritedWidget 就把这个 context “登记”到了 Element 的内部静态 _map 里。
///⚠️ 所以键盘弹出来的时候,触发了顶层的 MediaQueryData 发生变化,自然就触发了“登记”过的 context 的变化
///⚠️ 比如 LoginPage 、HomePage ····
///⚠️ 所以比如你在 搜索页面 键盘弹出时,下面的 HomePage.sName 对应的 WidgetBuilder 会被触发
///⚠️ 这个是我故意的,如果不需要,可以去掉 pageContainer 或者不要用这里的 context
routes: {
WelcomePage.sName: (context) {
DebugLabel.showDebugLabel(context);
return const WelcomePage();
},
HomePage.sName: (context) {
return NavigatorUtils.pageContainer(
const HomePage(), context);
},
LoginPage.sName: (context) {
return NavigatorUtils.pageContainer(
const LoginPage(), context);
},

///使用 ModalRoute.of(context).settings.arguments; 获取参数
PhotoViewPage.sName: (context) {
return const PhotoViewPage();
},
});

if (greyApp) {
///mode one
app = ColorFiltered(
colorFilter: const ColorFilter.mode(
Colors.grey, BlendMode.saturation),
child: app);

///mode two
// app = ColorFiltered(
// colorFilter: greyscale,
// child: app);
}

return app;
}),
);
},
),
);
}

}

mixin HttpErrorListener on State<FlutterReduxApp> {
Expand Down Expand Up @@ -185,15 +187,16 @@ mixin HttpErrorListener on State<FlutterReduxApp> {
showToast(GSYLocalizations.i18n(context)!.network_error_422);
break;
case Code.NETWORK_TIMEOUT:
//超时
//超时
showToast(GSYLocalizations.i18n(context)!.network_error_timeout);
break;
case Code.GITHUB_API_REFUSED:
//Github API 异常
//Github API 异常
showToast(GSYLocalizations.i18n(context)!.github_refused);
break;
default:
showToast("${GSYLocalizations.i18n(context)!.network_error_unknown} $message");
showToast(
"${GSYLocalizations.i18n(context)!.network_error_unknown} $message");
break;
}
}
Expand All @@ -205,4 +208,3 @@ mixin HttpErrorListener on State<FlutterReduxApp> {
toastLength: Toast.LENGTH_LONG);
}
}

2 changes: 1 addition & 1 deletion lib/common/net/transformer.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import 'dart:convert';

import 'package:gsy_github_app_flutter/db/provider/event/received_event_db_provider.dart';
import 'package:gsy_github_app_flutter/db/provider/event/user_event_db_provider.dart';
import 'package:gsy_github_app_flutter/common/dao/dao_result.dart';
import 'package:gsy_github_app_flutter/common/repositories/data_result.dart';
import 'package:gsy_github_app_flutter/model/Event.dart';
import 'package:gsy_github_app_flutter/common/net/address.dart';
import 'package:gsy_github_app_flutter/common/net/api.dart';

class EventDao {
class EventRepository {
static getEventReceived(String? userName,
{page = 1, bool needDb = false}) async {
if (userName == null) {
Expand Down Expand Up @@ -50,7 +50,7 @@ class EventDao {
}

/// 用户行为事件
static getEventDao(userName, {page = 0, bool needDb = false}) async {
static getEventRequest(userName, {page = 0, bool needDb = false}) async {
UserEventDbProvider provider = UserEventDbProvider();
next() async {
String url =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:dio/dio.dart';
import 'package:gsy_github_app_flutter/db/provider/issue/issue_comment_db_provider.dart';
import 'package:gsy_github_app_flutter/db/provider/issue/issue_detail_db_provider.dart';
import 'package:gsy_github_app_flutter/db/provider/repos/repository_issue_db_provider.dart';
import 'package:gsy_github_app_flutter/common/dao/dao_result.dart';
import 'package:gsy_github_app_flutter/common/repositories/data_result.dart';
import 'package:gsy_github_app_flutter/model/Issue.dart';
import 'package:gsy_github_app_flutter/common/net/address.dart';
import 'package:gsy_github_app_flutter/common/net/api.dart';
Expand All @@ -13,15 +13,15 @@ import 'package:gsy_github_app_flutter/common/net/api.dart';
/// Created by guoshuyu
/// Date: 2018-07-19
class IssueDao {
class IssueRepository {
/// 获取仓库issue
/// @param page
/// @param userName
/// @param repository
/// @param state issue状态
/// @param sort 排序类型 created updated等
/// @param direction 正序或者倒序
static getRepositoryIssueDao(userName, repository, state,
static getRepositoryIssueRequest(userName, repository, state,
{sort, direction, page = 0, needDb = false}) async {
String? fullName = "$userName/$repository";
String dbState = state ?? "*";
Expand Down Expand Up @@ -74,7 +74,7 @@ class IssueDao {
/// @param reposName 仓库名
/// @param page
/// @param state 问题状态,all open closed
static searchRepositoryIssue(q, name, reposName, state, {page = 1}) async {
static searchRepositoryRequest(q, name, reposName, state, {page = 1}) async {
String? qu;
if (state == null || state == 'all') {
qu = q + "+repo%3A$name%2F$reposName";
Expand All @@ -100,7 +100,7 @@ class IssueDao {
}

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

IssueDetailDbProvider provider = IssueDetailDbProvider();
Expand Down Expand Up @@ -132,7 +132,7 @@ class IssueDao {
}

/// issue的详请列表
static getIssueCommentDao(userName, repository, number,
static getIssueCommentRequest(userName, repository, number,
{page = 0, needDb = false}) async {
String? fullName = "$userName/$repository";
IssueCommentDbProvider provider = IssueCommentDbProvider();
Expand Down Expand Up @@ -173,7 +173,7 @@ class IssueDao {
}

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

/// 编辑issue
static editIssueDao(userName, repository, number, issue) async {
static editIssueRequest(userName, repository, number, issue) async {
String url = Address.editIssue(userName, repository, number);
var res = await httpManager.netFetch(
url,
Expand All @@ -203,7 +203,7 @@ class IssueDao {
}

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

/// 创建issue
static createIssueDao(userName, repository, issue) async {
static createIssueRequest(userName, repository, issue) async {
String url = Address.createIssue(userName, repository);
var res = await httpManager.netFetch(
url,
Expand All @@ -234,7 +234,7 @@ class IssueDao {
}

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

/// 删除issue回复
static deleteCommentDao(userName, repository, number, commentId) async {
static deleteCommentRequest(userName, repository, number, commentId) async {
String url = Address.editComment(userName, repository, commentId);
var res = await httpManager
.netFetch(url, null, null, Options(method: 'DELETE'), noTip: true);
Expand Down
Loading

0 comments on commit c321888

Please sign in to comment.