Files
momo/lib/material/router.dart

74 lines
2.4 KiB
Dart
Raw Normal View History

2023-02-06 23:45:56 +08:00
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
2023-02-08 23:48:25 +08:00
import 'package:momo/material/detail.dart';
2023-02-08 17:20:16 +08:00
import 'package:momo/material/gallery.dart';
2023-02-06 23:45:56 +08:00
import 'package:momo/material/home.dart';
2023-02-07 17:28:01 +08:00
import 'package:momo/material/login.dart';
2023-02-08 17:20:16 +08:00
import 'package:momo/material/profile.dart';
2023-02-06 23:45:56 +08:00
class MyMaterialRouterConfig {
2023-02-07 17:28:01 +08:00
late GoRouter router;
2023-02-10 17:33:51 +08:00
2023-02-10 00:07:41 +08:00
final GlobalKey<NavigatorState> _rootNavigatorKey =
GlobalKey<NavigatorState>();
2023-02-07 17:28:01 +08:00
2023-02-11 22:29:33 +08:00
MyMaterialRouterConfig(String? token) {
2023-02-07 17:28:01 +08:00
router = GoRouter(
2023-02-10 00:07:41 +08:00
navigatorKey: _rootNavigatorKey,
2023-02-09 17:07:14 +08:00
initialLocation: "/",
2023-02-07 17:28:01 +08:00
routes: <RouteBase>[
2023-02-08 17:20:16 +08:00
ShellRoute(
builder: (BuildContext context, GoRouterState state, Widget child) {
return HomePage(
content: child,
);
},
2023-02-10 00:07:41 +08:00
routes: <RouteBase>[
2023-02-08 17:20:16 +08:00
GoRoute(
path: "/",
2023-02-10 00:07:41 +08:00
name: "相册",
2023-02-13 00:36:25 +08:00
pageBuilder: (BuildContext context, GoRouterState state) =>
const NoTransitionPage(child: Gallery()),
2023-02-08 17:20:16 +08:00
redirect: (BuildContext context, GoRouterState state) {
if (token == null || token.isEmpty) {
return '/login';
}
return null;
2023-02-10 00:07:41 +08:00
},
2023-02-19 23:10:09 +08:00
// routes: <RouteBase>[
// GoRoute(
// path: "detail",
// name: "图片详情",
// builder: (BuildContext context, GoRouterState state) =>
// const ImageDetail())
// ]
),
GoRoute(
path: "/detail",
name: "图片详情",
builder: (BuildContext context, GoRouterState state) =>
const ImageDetail()),
2023-02-08 17:20:16 +08:00
GoRoute(
path: "/profile",
2023-02-10 00:07:41 +08:00
name: "个人资料",
2023-02-13 00:36:25 +08:00
pageBuilder: (BuildContext context, GoRouterState state) =>
const NoTransitionPage(child: Profile())),
2023-02-08 17:20:16 +08:00
],
),
2023-02-07 17:28:01 +08:00
GoRoute(
2023-02-08 17:20:16 +08:00
path: "/login",
2023-02-10 00:07:41 +08:00
name: "登录",
2023-02-07 17:28:01 +08:00
builder: (BuildContext context, GoRouterState state) {
2023-02-08 17:20:16 +08:00
return const LoginPage();
2023-02-07 17:28:01 +08:00
},
redirect: (BuildContext context, GoRouterState state) {
2023-02-08 17:20:16 +08:00
if (token != null && token.isNotEmpty) {
return '/';
2023-02-07 17:28:01 +08:00
}
return null;
}),
],
);
}
2023-02-06 23:45:56 +08:00
}