From 6f839aea24412157ea23f0fb0213b29725643b93 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 30 Dec 2025 12:18:23 +0700 Subject: [PATCH] =?UTF-8?q?refactor(router):=20=D1=83=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D1=88=D0=B8=D1=82=D1=8C=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D1=83=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8=20=D1=80=D0=BE=D1=83=D1=82=D0=B5=D1=80=D0=B0=20refactor(?= =?UTF-8?q?app):=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D1=82=D1=8C=20AppR?= =?UTF-8?q?oot=20=D0=BD=D0=B0=20StatefulWidget=20=D0=B8=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BC=D0=B5=D1=81=D1=82=D0=B8=D1=82=D1=8C=20=D0=B8?= =?UTF-8?q?=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8E=20=D1=80=D0=BE=D1=83=D1=82=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/app/app_root.dart | 78 +++++++++++++++----------------------- lib/router/app_router.dart | 48 ++++++++++++++--------- 2 files changed, 60 insertions(+), 66 deletions(-) diff --git a/lib/app/app_root.dart b/lib/app/app_root.dart index 6a77673..0d646cf 100644 --- a/lib/app/app_root.dart +++ b/lib/app/app_root.dart @@ -14,8 +14,9 @@ import 'package:go_router/go_router.dart'; /// /// Отвечает за: /// - Настройку провайдеров для темы и локализации +/// - Инициализацию роутера приложения /// {@endtemplate} -class AppRoot extends StatelessWidget { +class AppRoot extends StatefulWidget { /// {@macro app_root} const AppRoot({required this.diContainer, super.key}); @@ -23,46 +24,10 @@ class AppRoot extends StatelessWidget { final DiContainer diContainer; @override - Widget build(BuildContext context) { - 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, - ), - ), - ); - }, - ), - ); - } + State createState() => _AppRootState(); } -class _Internal extends StatefulWidget { - 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> { +class _AppRootState extends State { /// Роутер приложения late final GoRouter router; @@ -79,13 +44,30 @@ class _InternalState extends State<_Internal> { } @override - Widget build(BuildContext context) => MaterialApp.router( - darkTheme: AppTheme.dark, - theme: AppTheme.light, - themeMode: widget.theme.themeMode, - locale: widget.localization.locale, - localizationsDelegates: AppLocalizations.localizationsDelegates, - supportedLocales: AppLocalizations.supportedLocales, - routerConfig: 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, + theme: AppTheme.light, + themeMode: themeContext.theme.themeMode, + locale: localizationContext.localization.locale, + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + routerConfig: router, + ), + ), + ); + }, + ), + ); + } } diff --git a/lib/router/app_router.dart b/lib/router/app_router.dart index 1be0562..8a98a63 100644 --- a/lib/router/app_router.dart +++ b/lib/router/app_router.dart @@ -23,23 +23,35 @@ class AppRouter { /// Метод для создания экземпляра GoRouter static GoRouter createRouter(IDebugService debugService) { - return 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(), - ], - ); + 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, + 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(), + ], + ); }