refactor(router): улучшить обработку ошибок при создании роутера

refactor(app): изменить AppRoot на StatefulWidget и переместить инициализацию роутера
This commit is contained in:
Artem
2025-12-30 12:18:23 +07:00
parent 617563fb6c
commit 6f839aea24
2 changed files with 60 additions and 66 deletions

View File

@@ -14,8 +14,9 @@ import 'package:go_router/go_router.dart';
/// ///
/// Отвечает за: /// Отвечает за:
/// - Настройку провайдеров для темы и локализации /// - Настройку провайдеров для темы и локализации
/// - Инициализацию роутера приложения
/// {@endtemplate} /// {@endtemplate}
class AppRoot extends StatelessWidget { class AppRoot extends StatefulWidget {
/// {@macro app_root} /// {@macro app_root}
const AppRoot({required this.diContainer, super.key}); const AppRoot({required this.diContainer, super.key});
@@ -23,46 +24,10 @@ class AppRoot extends StatelessWidget {
final DiContainer diContainer; final DiContainer diContainer;
@override @override
Widget build(BuildContext context) { State<AppRoot> createState() => _AppRootState();
return AppProviders(
diContainer: diContainer,
child: LocalizationConsumer(
builder: (localizationContext) {
return ThemeConsumer(
builder: (themeContext) => MediaQuery(
key: const ValueKey('prevent_rebuild'),
data: MediaQuery.of(
themeContext,
).copyWith(textScaler: TextScaler.noScaling, boldText: false),
child: _Internal(
diContainer: diContainer,
theme: themeContext.theme,
localization: localizationContext.localization,
),
),
);
},
),
);
}
} }
class _Internal extends StatefulWidget { class _AppRootState extends State<AppRoot> {
const _Internal({
required this.theme,
required this.diContainer,
required this.localization,
});
final ThemeNotifier theme;
final DiContainer diContainer;
final LocalizationNotifier localization;
@override
State<_Internal> createState() => _InternalState();
}
class _InternalState extends State<_Internal> {
/// Роутер приложения /// Роутер приложения
late final GoRouter router; late final GoRouter router;
@@ -79,13 +44,30 @@ class _InternalState extends State<_Internal> {
} }
@override @override
Widget build(BuildContext context) => MaterialApp.router( Widget build(BuildContext context) {
darkTheme: AppTheme.dark, return AppProviders(
theme: AppTheme.light, diContainer: widget.diContainer,
themeMode: widget.theme.themeMode, child: LocalizationConsumer(
locale: widget.localization.locale, builder: (localizationContext) {
localizationsDelegates: AppLocalizations.localizationsDelegates, return ThemeConsumer(
supportedLocales: AppLocalizations.supportedLocales, builder: (themeContext) => MediaQuery(
routerConfig: router, key: const ValueKey('prevent_rebuild'),
); data: MediaQuery.of(
themeContext,
).copyWith(textScaler: TextScaler.noScaling, boldText: false),
child: MaterialApp.router(
darkTheme: AppTheme.dark,
theme: AppTheme.light,
themeMode: themeContext.theme.themeMode,
locale: localizationContext.localization.locale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
routerConfig: router,
),
),
);
},
),
);
}
} }

View File

@@ -23,23 +23,35 @@ class AppRouter {
/// Метод для создания экземпляра GoRouter /// Метод для создания экземпляра GoRouter
static GoRouter createRouter(IDebugService debugService) { static GoRouter createRouter(IDebugService debugService) {
return GoRouter( try {
navigatorKey: rootNavigatorKey, return _init(debugService);
initialLocation: initialLocation, } on Object catch (error, stackTrace) {
observers: [debugService.routeObserver], debugService.logError(
routes: [ 'Ошибка при создании роутера',
StatefulShellRoute.indexedStack( error: error,
parentNavigatorKey: rootNavigatorKey, stackTrace: stackTrace,
builder: (context, state, navigationShell) => );
RootScreen(navigationShell: navigationShell), throw StateError('Не удалось создать роутер: $error');
branches: [ }
MainRoutes.buildShellBranch(),
ProfileRoutes.buildShellBranch(),
],
),
DebugRoutes.buildRoutes(),
UpdateRoutes.buildRoutes(),
],
);
} }
/// Внутренний метод для инициализации роутера
static GoRouter _init(IDebugService debugService) => GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: initialLocation,
observers: [debugService.routeObserver],
routes: [
StatefulShellRoute.indexedStack(
parentNavigatorKey: rootNavigatorKey,
builder: (context, state, navigationShell) =>
RootScreen(navigationShell: navigationShell),
branches: [
MainRoutes.buildShellBranch(),
ProfileRoutes.buildShellBranch(),
],
),
DebugRoutes.buildRoutes(),
UpdateRoutes.buildRoutes(),
],
);
} }