refactor(app): перенести роутер ближе к месту инициализации в AppRoot

This commit is contained in:
Artem
2025-12-30 12:13:52 +07:00
parent 75c0ac3285
commit 617563fb6c
2 changed files with 50 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ import 'package:friflex_starter/app/theme/theme_notifier.dart';
import 'package:friflex_starter/di/di_container.dart';
import 'package:friflex_starter/l10n/gen/app_localizations.dart';
import 'package:friflex_starter/l10n/localization_notifier.dart';
import 'package:friflex_starter/router/app_router.dart';
import 'package:go_router/go_router.dart';
/// {@template app}
@@ -16,14 +17,11 @@ import 'package:go_router/go_router.dart';
/// {@endtemplate}
class AppRoot extends StatelessWidget {
/// {@macro app_root}
const AppRoot({required this.diContainer, required this.router, super.key});
const AppRoot({required this.diContainer, super.key});
/// Контейнер зависимостей
final DiContainer diContainer;
/// Роутер приложения
final GoRouter router;
@override
Widget build(BuildContext context) {
return AppProviders(
@@ -36,14 +34,10 @@ class AppRoot extends StatelessWidget {
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,
child: _Internal(
diContainer: diContainer,
theme: themeContext.theme,
localization: localizationContext.localization,
),
),
);
@@ -52,3 +46,46 @@ class AppRoot extends StatelessWidget {
);
}
}
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> {
/// Роутер приложения
late final GoRouter router;
@override
void initState() {
super.initState();
router = AppRouter.createRouter(widget.diContainer.debugService);
}
@override
void dispose() {
router.dispose();
super.dispose();
}
@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,
);
}