refactor(apps): 主题系统迁移至 ColorScheme + 扩展架构并支持 Dark Mode
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'app_route_observer.dart';
|
||||
import '../di/injection.dart';
|
||||
import '../../../features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import '../../../features/auth/presentation/bloc/auth_state.dart';
|
||||
import '../../../features/chat/presentation/bloc/chat_bloc.dart';
|
||||
import 'app_routes.dart';
|
||||
import 'go_router_refresh_stream.dart';
|
||||
import '../../../features/auth/presentation/screens/auth_boot_screen.dart';
|
||||
@@ -32,7 +36,6 @@ import '../../../features/settings/presentation/screens/work_memory_detail_scree
|
||||
import '../../../features/settings/presentation/screens/edit_profile_screen.dart';
|
||||
|
||||
final _homeSecondLevelRoutes = [
|
||||
AppRoutes.shellHomeBranch,
|
||||
AppRoutes.shellCalendarBranch,
|
||||
AppRoutes.calendarMonth,
|
||||
AppRoutes.shellTodoBranch,
|
||||
@@ -54,37 +57,53 @@ final _protectedRoutes = [
|
||||
AppRoutes.messageInviteList,
|
||||
];
|
||||
|
||||
String? resolveAuthRedirect({
|
||||
required AuthState authState,
|
||||
required String matchedLocation,
|
||||
}) {
|
||||
final isAuthenticated = authState is AuthAuthenticated;
|
||||
final isAuthChecking = authState is AuthInitial || authState is AuthLoading;
|
||||
final isBootRoute = matchedLocation == AppRoutes.authBoot;
|
||||
final isAuthRoute =
|
||||
matchedLocation == AppRoutes.authLogin ||
|
||||
matchedLocation.startsWith('/login');
|
||||
final isHomeRoute = matchedLocation == AppRoutes.homeMain;
|
||||
final isProtected =
|
||||
isHomeRoute ||
|
||||
_protectedRoutes.any((route) => matchedLocation.startsWith(route));
|
||||
|
||||
if (isAuthChecking && !isBootRoute) {
|
||||
return AppRoutes.authBoot;
|
||||
}
|
||||
if (!isAuthChecking && isBootRoute) {
|
||||
return isAuthenticated ? AppRoutes.homeMain : AppRoutes.authLogin;
|
||||
}
|
||||
if (!isAuthenticated && isProtected) {
|
||||
return AppRoutes.authLogin;
|
||||
}
|
||||
if (isAuthenticated && isAuthRoute) {
|
||||
return AppRoutes.homeMain;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Widget buildHomeRouteScreen() {
|
||||
return BlocProvider<ChatBloc>.value(
|
||||
value: sl<ChatBloc>(),
|
||||
child: const HomeScreen(),
|
||||
);
|
||||
}
|
||||
|
||||
GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
return GoRouter(
|
||||
initialLocation: AppRoutes.authBoot,
|
||||
observers: [appRouteObserver],
|
||||
refreshListenable: GoRouterRefreshStream(authBloc.stream),
|
||||
redirect: (context, state) {
|
||||
final authState = authBloc.state;
|
||||
final isAuthenticated = authState is AuthAuthenticated;
|
||||
final isAuthChecking =
|
||||
authState is AuthInitial || authState is AuthLoading;
|
||||
final isBootRoute = state.matchedLocation == AppRoutes.authBoot;
|
||||
final isAuthRoute =
|
||||
state.matchedLocation == AppRoutes.authLogin ||
|
||||
state.matchedLocation.startsWith('/login');
|
||||
final isProtected = _protectedRoutes.any(
|
||||
(route) => state.matchedLocation.startsWith(route),
|
||||
return resolveAuthRedirect(
|
||||
authState: authBloc.state,
|
||||
matchedLocation: state.matchedLocation,
|
||||
);
|
||||
|
||||
if (isAuthChecking && !isBootRoute) {
|
||||
return AppRoutes.authBoot;
|
||||
}
|
||||
if (!isAuthChecking && isBootRoute) {
|
||||
return isAuthenticated ? AppRoutes.homeMain : AppRoutes.authLogin;
|
||||
}
|
||||
if (!isAuthenticated && isProtected) {
|
||||
return AppRoutes.authLogin;
|
||||
}
|
||||
if (isAuthenticated && isAuthRoute) {
|
||||
return AppRoutes.homeMain;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
@@ -118,7 +137,7 @@ GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.homeMain,
|
||||
builder: (context, state) => const HomeScreen(),
|
||||
builder: (context, state) => buildHomeRouteScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.messageInviteList,
|
||||
|
||||
@@ -2,10 +2,9 @@ class AppRoutes {
|
||||
AppRoutes._();
|
||||
|
||||
static const authBoot = '/boot';
|
||||
static const authLogin = '/';
|
||||
static const authLogin = '/login';
|
||||
|
||||
static const homeMain = '/home';
|
||||
static const shellHomeBranch = homeMain;
|
||||
static const homeMain = '/';
|
||||
static const shellCalendarBranch = calendarDayWeek;
|
||||
static const shellTodoBranch = todoList;
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'app_routes.dart';
|
||||
|
||||
enum HomeReturnAction { pop, goHome, goHomeForDock }
|
||||
|
||||
HomeReturnAction resolveHomeReturnAction({
|
||||
required bool canPop,
|
||||
required bool isAuthEntry,
|
||||
bool forceGoHome = false,
|
||||
}) {
|
||||
if (forceGoHome) {
|
||||
return HomeReturnAction.goHome;
|
||||
}
|
||||
if (isAuthEntry) {
|
||||
return HomeReturnAction.goHome;
|
||||
}
|
||||
if (canPop) {
|
||||
return HomeReturnAction.goHomeForDock;
|
||||
}
|
||||
return HomeReturnAction.goHome;
|
||||
}
|
||||
|
||||
void returnToHomePreserveState(
|
||||
BuildContext context, {
|
||||
bool isAuthEntry = false,
|
||||
bool forceGoHome = false,
|
||||
}) {
|
||||
final action = resolveHomeReturnAction(
|
||||
canPop: context.canPop(),
|
||||
isAuthEntry: isAuthEntry,
|
||||
forceGoHome: forceGoHome,
|
||||
);
|
||||
switch (action) {
|
||||
case HomeReturnAction.pop:
|
||||
context.pop();
|
||||
return;
|
||||
case HomeReturnAction.goHome:
|
||||
context.go(AppRoutes.homeMain);
|
||||
return;
|
||||
case HomeReturnAction.goHomeForDock:
|
||||
if (context.canPop()) {
|
||||
context.pop();
|
||||
return;
|
||||
}
|
||||
context.go(AppRoutes.homeMain);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user