2023-06-21 17:44:28 +08:00
|
|
|
import 'package:flutter/widgets.dart';
|
2023-07-11 23:39:36 +08:00
|
|
|
|
2023-06-21 17:44:28 +08:00
|
|
|
import 'package:go_router/go_router.dart';
|
2023-07-11 23:39:36 +08:00
|
|
|
|
2023-07-17 01:00:16 +08:00
|
|
|
import 'router_key.dart';
|
|
|
|
import 'chat_router.dart';
|
|
|
|
import 'contact_router.dart';
|
|
|
|
import 'more_router.dart';
|
|
|
|
import 'package:together_mobile/models/init_get_it.dart';
|
|
|
|
import 'package:together_mobile/models/token_model.dart';
|
|
|
|
import 'package:together_mobile/models/user_model.dart';
|
|
|
|
import 'package:together_mobile/request/signup_signin.dart';
|
|
|
|
|
2023-06-21 17:44:28 +08:00
|
|
|
import 'package:together_mobile/screens/signin_signup/signup_screen.dart';
|
|
|
|
import 'package:together_mobile/screens/signin_signup/signin_screen.dart';
|
|
|
|
import 'package:together_mobile/screens/welcome/welcome_screen.dart';
|
|
|
|
import 'package:together_mobile/screens/home/home_screen.dart';
|
|
|
|
|
|
|
|
final GoRouter router = GoRouter(
|
|
|
|
initialLocation: '/welcome',
|
|
|
|
navigatorKey: rootNavigatorKey,
|
|
|
|
debugLogDiagnostics: true,
|
|
|
|
routes: <RouteBase>[
|
|
|
|
GoRoute(
|
|
|
|
path: '/welcome',
|
|
|
|
name: 'Welcome',
|
|
|
|
builder: (BuildContext context, GoRouterState state) =>
|
|
|
|
const WelcomeScreen(),
|
2023-07-17 01:00:16 +08:00
|
|
|
redirect: (context, state) async {
|
2023-07-27 18:17:52 +08:00
|
|
|
await getIt.get<Token>().init();
|
2023-07-17 01:00:16 +08:00
|
|
|
if (getIt.get<Token>().token.isNotEmpty) {
|
|
|
|
Map<String, dynamic> res = await signinByToken();
|
|
|
|
if (res['code'] == 10200) {
|
|
|
|
await getIt.get<Token>().updateToken(res['token']);
|
2023-07-27 18:17:52 +08:00
|
|
|
getIt.get<UserAccount>().init(res['data']);
|
2023-07-17 01:00:16 +08:00
|
|
|
return '/chat';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2023-06-21 17:44:28 +08:00
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: '/signup',
|
|
|
|
name: 'SignUp',
|
|
|
|
pageBuilder: (context, state) {
|
|
|
|
return CustomTransitionPage(
|
|
|
|
key: state.pageKey,
|
|
|
|
child: const SignupScreen(),
|
|
|
|
transitionDuration: const Duration(milliseconds: 200),
|
|
|
|
reverseTransitionDuration: const Duration(milliseconds: 200),
|
|
|
|
transitionsBuilder: (BuildContext context,
|
|
|
|
Animation<double> animation,
|
|
|
|
Animation<double> secondaryAnimation,
|
|
|
|
Widget child) {
|
|
|
|
const begin = Offset(1.0, 0.0);
|
|
|
|
const end = Offset.zero;
|
|
|
|
final tween = Tween(begin: begin, end: end);
|
|
|
|
final offsetAnimation = animation.drive(tween);
|
|
|
|
return SlideTransition(
|
|
|
|
position: offsetAnimation,
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: '/signin',
|
|
|
|
name: 'SignIn',
|
|
|
|
pageBuilder: (context, state) {
|
|
|
|
return CustomTransitionPage(
|
|
|
|
key: state.pageKey,
|
|
|
|
child: const SigninScreen(),
|
|
|
|
transitionDuration: const Duration(milliseconds: 200),
|
|
|
|
reverseTransitionDuration: const Duration(milliseconds: 200),
|
|
|
|
transitionsBuilder: (
|
|
|
|
BuildContext context,
|
|
|
|
Animation<double> animation,
|
|
|
|
Animation<double> secondaryAnimation,
|
|
|
|
Widget child,
|
|
|
|
) {
|
|
|
|
const begin = Offset(1.0, 0.0);
|
|
|
|
const end = Offset.zero;
|
|
|
|
final tween = Tween(begin: begin, end: end);
|
|
|
|
final offsetAnimation = animation.drive(tween);
|
|
|
|
return SlideTransition(
|
|
|
|
position: offsetAnimation,
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ShellRoute(
|
|
|
|
navigatorKey: shellNavigatorKey,
|
|
|
|
builder: (BuildContext context, GoRouterState state, Widget child) {
|
|
|
|
int initIndex = 0;
|
|
|
|
if (state.fullPath!.startsWith('/chat')) {
|
|
|
|
initIndex = 0;
|
|
|
|
} else if (state.fullPath!.startsWith('/contact')) {
|
|
|
|
initIndex = 1;
|
|
|
|
} else if (state.fullPath!.startsWith('/more')) {
|
|
|
|
initIndex = 2;
|
|
|
|
}
|
|
|
|
return HomeScreenWithNavBar(initIndex: initIndex, child: child);
|
|
|
|
},
|
|
|
|
routes: <RouteBase>[
|
2023-07-17 01:00:16 +08:00
|
|
|
chatRouter,
|
|
|
|
contactRouter,
|
|
|
|
moreRouter,
|
2023-06-21 17:44:28 +08:00
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|