import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import 'package:proxibuy/presentation/providers/category/cubit/categories_children_cubit.dart'; import 'package:proxibuy/presentation/providers/category/cubit/categories_cubit.dart'; import 'package:proxibuy/presentation/ui/screens/auth/auth_page.dart'; import 'package:proxibuy/presentation/ui/screens/setting/edit_profile_page.dart'; import 'package:proxibuy/presentation/ui/screens/home/screens/categories_screen.dart'; import 'package:proxibuy/presentation/ui/screens/home/screens/explore_screen.dart'; import 'package:proxibuy/presentation/ui/screens/home/home_page.dart'; import 'package:proxibuy/presentation/ui/screens/home/screens/home_screen.dart'; import 'package:proxibuy/presentation/ui/screens/home/screens/setting_screen.dart'; import 'package:proxibuy/presentation/ui/screens/product/product_page.dart'; import 'package:proxibuy/presentation/ui/screens/setting/notifications_etting_page.dart'; import 'package:proxibuy/presentation/ui/theme/responsive.dart'; class AppRouter { static final initial = '/'; static final auth = '/auth'; static final explore = '/explore'; static final setting = '/settings'; static final editProfile = 'edit-profile'; static final notificationsSetting = 'notification'; static final product = '/product'; static final categories = '/categories'; static List home = [initial, explore, setting, categories]; static final GlobalKey navigatorKey = GlobalKey(); static GoRouter createRouter = GoRouter( navigatorKey: navigatorKey, initialLocation: initial, redirect: (context, state) { return null; }, routes: [ GoRoute( path: auth, builder: (context, state) { return AuthPage(); }, ), StatefulShellRoute.indexedStack( builder: (context, state, navigationShell) => home.contains(state.fullPath) ? HomePage(child: navigationShell) : navigationShell, branches: [ StatefulShellBranch(routes: [ GoRoute( path: initial, builder: (context, state) { return HomeScreen(); }, routes: [ GoRoute( path: product, builder: (BuildContext context, GoRouterState state) { return ProductPage(); }, ), ]), ]), StatefulShellBranch(routes: [ GoRoute( path: categories, redirect: (context, state) { if (Responsive(context).isDesktop()) { return initial; } return null; }, builder: (BuildContext context, GoRouterState state) { StreamSubscription? categoriesSubscription; String? id = state.uri.queryParameters['id']; if (id != null) { catId.value = id; context.read().resetPagination(); context .read() .getAllChildCategories(id); } else { categoriesSubscription = context.read().stream.listen( (state) { if (state is CategoriesLoaded) { if (state.categories.isNotEmpty) { id = state.categories.first.id; catId.value = id; // ignore: use_build_context_synchronously context .read() .getAllChildCategories(id ?? ''); categoriesSubscription?.cancel(); } } }, ); } return CategoriesScreen(); }, ), ]), StatefulShellBranch(routes: [ GoRoute( path: explore, builder: (context, state) => ExploreScreen(), ), ]), StatefulShellBranch(routes: [ GoRoute( path: setting, builder: (context, state) => SettingScreen(), routes: [ GoRoute( path: editProfile, name: editProfile, builder: (context, state) { return EditProfilePage(); }, ), GoRoute( path: notificationsSetting, name: notificationsSetting, builder: (context, state) { return NotificationsEttingPage(); }, ), ]), ]), ], ), ], ); }