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) {
return AppProviders(
diContainer: widget.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: MaterialApp.router(
darkTheme: AppTheme.dark, darkTheme: AppTheme.dark,
theme: AppTheme.light, theme: AppTheme.light,
themeMode: widget.theme.themeMode, themeMode: themeContext.theme.themeMode,
locale: widget.localization.locale, locale: localizationContext.localization.locale,
localizationsDelegates: AppLocalizations.localizationsDelegates, localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales, supportedLocales: AppLocalizations.supportedLocales,
routerConfig: router, routerConfig: router,
),
),
); );
},
),
);
}
} }

View File

@@ -23,7 +23,20 @@ class AppRouter {
/// Метод для создания экземпляра GoRouter /// Метод для создания экземпляра GoRouter
static GoRouter createRouter(IDebugService debugService) { static GoRouter createRouter(IDebugService debugService) {
return GoRouter( try {
return _init(debugService);
} on Object catch (error, stackTrace) {
debugService.logError(
'Ошибка при создании роутера',
error: error,
stackTrace: stackTrace,
);
throw StateError('Не удалось создать роутер: $error');
}
}
/// Внутренний метод для инициализации роутера
static GoRouter _init(IDebugService debugService) => GoRouter(
navigatorKey: rootNavigatorKey, navigatorKey: rootNavigatorKey,
initialLocation: initialLocation, initialLocation: initialLocation,
observers: [debugService.routeObserver], observers: [debugService.routeObserver],
@@ -41,5 +54,4 @@ class AppRouter {
UpdateRoutes.buildRoutes(), UpdateRoutes.buildRoutes(),
], ],
); );
}
} }