mirror of
https://github.com/smmarty/friflex_flutter_starter.git
synced 2026-02-05 03:32:18 +00:00
refactor(app): перенести роутер ближе к месту инициализации в AppRoot
This commit is contained in:
@@ -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/di/di_container.dart';
|
||||||
import 'package:friflex_starter/l10n/gen/app_localizations.dart';
|
import 'package:friflex_starter/l10n/gen/app_localizations.dart';
|
||||||
import 'package:friflex_starter/l10n/localization_notifier.dart';
|
import 'package:friflex_starter/l10n/localization_notifier.dart';
|
||||||
|
import 'package:friflex_starter/router/app_router.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
/// {@template app}
|
/// {@template app}
|
||||||
@@ -16,14 +17,11 @@ import 'package:go_router/go_router.dart';
|
|||||||
/// {@endtemplate}
|
/// {@endtemplate}
|
||||||
class AppRoot extends StatelessWidget {
|
class AppRoot extends StatelessWidget {
|
||||||
/// {@macro app_root}
|
/// {@macro app_root}
|
||||||
const AppRoot({required this.diContainer, required this.router, super.key});
|
const AppRoot({required this.diContainer, super.key});
|
||||||
|
|
||||||
/// Контейнер зависимостей
|
/// Контейнер зависимостей
|
||||||
final DiContainer diContainer;
|
final DiContainer diContainer;
|
||||||
|
|
||||||
/// Роутер приложения
|
|
||||||
final GoRouter router;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return AppProviders(
|
return AppProviders(
|
||||||
@@ -36,14 +34,10 @@ class AppRoot extends StatelessWidget {
|
|||||||
data: MediaQuery.of(
|
data: MediaQuery.of(
|
||||||
themeContext,
|
themeContext,
|
||||||
).copyWith(textScaler: TextScaler.noScaling, boldText: false),
|
).copyWith(textScaler: TextScaler.noScaling, boldText: false),
|
||||||
child: MaterialApp.router(
|
child: _Internal(
|
||||||
darkTheme: AppTheme.dark,
|
diContainer: diContainer,
|
||||||
theme: AppTheme.light,
|
theme: themeContext.theme,
|
||||||
themeMode: themeContext.theme.themeMode,
|
localization: localizationContext.localization,
|
||||||
locale: localizationContext.localization.locale,
|
|
||||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
||||||
supportedLocales: AppLocalizations.supportedLocales,
|
|
||||||
routerConfig: router,
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class AppRunner {
|
|||||||
);
|
);
|
||||||
// Инициализация метода обработки ошибок
|
// Инициализация метода обработки ошибок
|
||||||
_initErrorHandlers(_debugService);
|
_initErrorHandlers(_debugService);
|
||||||
runApp(AppRoot(diContainer: diContainer, router: router));
|
runApp(AppRoot(diContainer: diContainer));
|
||||||
await _onAppLoaded();
|
await _onAppLoaded();
|
||||||
} on Object catch (e, stackTrace) {
|
} on Object catch (e, stackTrace) {
|
||||||
await _onAppLoaded();
|
await _onAppLoaded();
|
||||||
|
|||||||
Reference in New Issue
Block a user