feat/перенести роутер в место инициализации

This commit is contained in:
Artem Luzin m
2025-11-24 16:18:36 +07:00
parent fcf5048038
commit 84e5f5e869
2 changed files with 26 additions and 13 deletions

View File

@@ -12,6 +12,7 @@ import 'package:friflex_starter/features/update/domain/state/cubit/update_cubit.
import 'package:friflex_starter/features/update/update_routes.dart'; import 'package:friflex_starter/features/update/update_routes.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}
@@ -26,10 +27,7 @@ import 'package:go_router/go_router.dart';
/// {@endtemplate} /// {@endtemplate}
class App extends StatefulWidget { class App extends StatefulWidget {
/// {@macro app} /// {@macro app}
const App({required this.router, required this.initDependencies, super.key}); const App({required this.initDependencies, super.key});
/// Роутер приложения для навигации между экранами
final GoRouter router;
/// Функция для инициализации зависимостей приложения /// Функция для инициализации зависимостей приложения
/// Возвращает Future с контейнером зависимостей /// Возвращает Future с контейнером зависимостей
@@ -78,7 +76,7 @@ class _AppState extends State<App> {
// Если данные получены и не равны null, то отображаем внутренний виджет приложения // Если данные получены и не равны null, то отображаем внутренний виджет приложения
// Иначе отображаем экран ошибки // Иначе отображаем экран ошибки
(snapshot.hasData && snapshot.data != null) (snapshot.hasData && snapshot.data != null)
? _App(router: widget.router, diContainer: snapshot.data!) ? _AppInternal(diContainer: snapshot.data!)
: ErrorScreen( : ErrorScreen(
error: snapshot.error, error: snapshot.error,
stackTrace: snapshot.stackTrace, stackTrace: snapshot.stackTrace,
@@ -106,20 +104,39 @@ class _AppState extends State<App> {
/// ///
/// Настраивает MaterialApp с роутером, темами и локализацией. /// Настраивает MaterialApp с роутером, темами и локализацией.
/// {@endtemplate} /// {@endtemplate}
class _App extends StatelessWidget { class _AppInternal extends StatefulWidget {
/// {@macro app_internal} /// {@macro app_internal}
const _App({required this.router, required this.diContainer}); const _AppInternal({required this.diContainer});
/// Роутер приложения для навигации /// Роутер приложения для навигации
final GoRouter router;
/// Контейнер зависимостей /// Контейнер зависимостей
final DiContainer diContainer; final DiContainer diContainer;
@override
State<_AppInternal> createState() => _AppInternalState();
}
class _AppInternalState extends State<_AppInternal> {
/// Роутер приложения для навигации
late final GoRouter router;
@override
void initState() {
super.initState();
router = AppRouter.createRouter(widget.diContainer.debugService);
}
@override
void dispose() {
router.dispose();
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return DependsProviders( return DependsProviders(
diContainer: diContainer, diContainer: widget.diContainer,
child: BlocConsumer<UpdateCubit, UpdateState>( child: BlocConsumer<UpdateCubit, UpdateState>(
listener: (context, state) { listener: (context, state) {
if (state is UpdateSuccessState && if (state is UpdateSuccessState &&

View File

@@ -65,14 +65,10 @@ class AppRunner {
// Инициализация метода обработки ошибок // Инициализация метода обработки ошибок
_initErrorHandlers(_debugService); _initErrorHandlers(_debugService);
// Инициализация роутера
router = AppRouter.createRouter(_debugService);
// throw Exception('Test error'); // throw Exception('Test error');
runApp( runApp(
App( App(
router: router,
initDependencies: () { initDependencies: () {
return _initDependencies( return _initDependencies(
debugService: _debugService, debugService: _debugService,