mirror of
https://github.com/smmarty/friflex_flutter_starter.git
synced 2026-02-05 03:32:18 +00:00
refactor(router): улучшить обработку ошибок при создании роутера
refactor(app): изменить AppRoot на StatefulWidget и переместить инициализацию роутера
This commit is contained in:
@@ -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<AppRoot> 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<AppRoot> {
|
||||
/// Роутер приложения
|
||||
late final GoRouter router;
|
||||
|
||||
@@ -79,13 +44,30 @@ class _InternalState extends State<_Internal> {
|
||||
}
|
||||
|
||||
@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,
|
||||
theme: AppTheme.light,
|
||||
themeMode: widget.theme.themeMode,
|
||||
locale: widget.localization.locale,
|
||||
themeMode: themeContext.theme.themeMode,
|
||||
locale: localizationContext.localization.locale,
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
routerConfig: router,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,20 @@ class AppRouter {
|
||||
|
||||
/// Метод для создания экземпляра GoRouter
|
||||
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,
|
||||
initialLocation: initialLocation,
|
||||
observers: [debugService.routeObserver],
|
||||
@@ -41,5 +54,4 @@ class AppRouter {
|
||||
UpdateRoutes.buildRoutes(),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user