152 lines
5.4 KiB
Dart
152 lines
5.4 KiB
Dart
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/providers/user_info_cubit.dart';
|
|
import 'package:proxibuy/presentation/ui/screens/auth/auth_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/theme/responsive.dart';
|
|
|
|
class AppRouter {
|
|
static final initial = '/';
|
|
static final explore = '/explore';
|
|
static final setting = '/settings';
|
|
static final product = '/product';
|
|
static final categories = '/categories';
|
|
|
|
static List<String> home = [initial, explore, setting, categories];
|
|
|
|
static final GlobalKey<NavigatorState> navigatorKey =
|
|
GlobalKey<NavigatorState>();
|
|
|
|
static GoRouter createRouter = GoRouter(
|
|
navigatorKey: navigatorKey,
|
|
initialLocation: initial,
|
|
routes: [
|
|
// GoRoute(
|
|
// path: initial,
|
|
// builder: (BuildContext context, GoRouterState state) {
|
|
// return BlocConsumer<UserInfoCubit, UserInfoState>(
|
|
// listener: (context, state) {
|
|
// if (state is UserInfoSuccess || state is UserInfoFail) {
|
|
// FlutterNativeSplash.remove();
|
|
// }
|
|
// },
|
|
// builder: (context, state) {
|
|
// if (state is UserInfoSuccess) {
|
|
// return const HomePage();
|
|
// } else {
|
|
// return const AuthPage();
|
|
// }
|
|
// },
|
|
// );
|
|
// },
|
|
// routes: <RouteBase>[
|
|
// GoRoute(
|
|
// path: product,
|
|
// builder: (BuildContext context, GoRouterState state) {
|
|
// return ProductPage();
|
|
// },
|
|
// routes: <RouteBase>[],
|
|
// ),
|
|
// ],
|
|
// ),
|
|
|
|
StatefulShellRoute.indexedStack(
|
|
builder: (context, state, navigationShell) =>
|
|
BlocBuilder<UserInfoCubit, UserInfoState>(
|
|
builder: (context, userState) {
|
|
if (userState is UserInfoSuccess) {
|
|
if (home.contains(state.fullPath)) {
|
|
return HomePage(child: navigationShell);
|
|
} else {
|
|
return navigationShell;
|
|
}
|
|
} else {
|
|
return AuthPage();
|
|
}
|
|
},
|
|
),
|
|
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<CategoriesChildrenCubit>().resetPagination();
|
|
context
|
|
.read<CategoriesChildrenCubit>()
|
|
.getAllChildCategories(id);
|
|
} else {
|
|
categoriesSubscription =
|
|
context.read<CategoriesCubit>().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<CategoriesChildrenCubit>()
|
|
.getAllChildCategories(id ?? '');
|
|
categoriesSubscription?.cancel();
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|
|
return CategoriesScreen();
|
|
},
|
|
),
|
|
]),
|
|
StatefulShellBranch(routes: [
|
|
GoRoute(
|
|
path: explore,
|
|
builder: (context, state) => ExploreScreen(),
|
|
),
|
|
]),
|
|
StatefulShellBranch(routes: [
|
|
GoRoute(
|
|
path: setting,
|
|
builder: (context, state) => SettingScreen(),
|
|
),
|
|
]),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|