157 lines
4.8 KiB
Dart
157 lines
4.8 KiB
Dart
// 功能描述:配置工作人员端路由、鉴权守卫及登录后安全回跳。
|
||
// 版本:1.1.0
|
||
import 'package:flutter/material.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
|
||
import '../ui/features/auth/login_page.dart';
|
||
import '../ui/features/evidence/evidence_page.dart';
|
||
import '../ui/features/preflight/preflight_page.dart';
|
||
import '../ui/features/profile/profile_page.dart';
|
||
import '../ui/features/work/work_detail_page.dart';
|
||
import '../ui/features/work/work_list_page.dart';
|
||
import 'auth_navigation.dart';
|
||
import 'dependencies.dart';
|
||
|
||
GoRouter createRouter(
|
||
AppDependencies dependencies, {
|
||
String initialLocation = '/preflight',
|
||
}) => GoRouter(
|
||
initialLocation: initialLocation,
|
||
refreshListenable: dependencies.session,
|
||
redirect: (context, state) {
|
||
final login = state.matchedLocation == '/login';
|
||
if (!dependencies.session.isAuthenticated && !login) {
|
||
return buildAuthLocation(
|
||
redirectTarget: state.uri.toString(),
|
||
sessionExpired: dependencies.session.hasExpired,
|
||
);
|
||
}
|
||
if (dependencies.session.isAuthenticated && login) {
|
||
return sanitizeRedirectTarget(state.uri.queryParameters['redirect']) ?? '/preflight';
|
||
}
|
||
return null;
|
||
},
|
||
routes: [
|
||
GoRoute(
|
||
path: '/login',
|
||
builder: (context, state) => LoginPage(
|
||
session: dependencies.session,
|
||
showSessionExpiredMessage: state.uri.queryParameters['reason'] == 'expired',
|
||
),
|
||
),
|
||
GoRoute(
|
||
path: '/preflight',
|
||
builder: (context, state) => PreflightPage(
|
||
session: dependencies.session,
|
||
repository: dependencies.repository,
|
||
),
|
||
),
|
||
GoRoute(
|
||
path: '/tasks/:identity',
|
||
builder: (context, state) => WorkDetailPage(
|
||
session: dependencies.session,
|
||
repository: dependencies.repository,
|
||
drafts: dependencies.drafts,
|
||
identity: state.pathParameters['identity']!,
|
||
),
|
||
routes: [
|
||
GoRoute(
|
||
path: 'evidence',
|
||
builder: (context, state) => EvidencePage(
|
||
session: dependencies.session,
|
||
repository: dependencies.repository,
|
||
drafts: dependencies.drafts,
|
||
taskIdentity: state.pathParameters['identity']!,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
StatefulShellRoute.indexedStack(
|
||
builder: (context, state, shell) => _ServiceShell(
|
||
navigationShell: shell,
|
||
roleCode: dependencies.session.roleCode,
|
||
),
|
||
branches: [
|
||
StatefulShellBranch(
|
||
routes: [
|
||
GoRoute(
|
||
path: '/work',
|
||
builder: (context, state) => WorkListPage(
|
||
session: dependencies.session,
|
||
repository: dependencies.repository,
|
||
completed: false,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
StatefulShellBranch(
|
||
routes: [
|
||
GoRoute(
|
||
path: '/records',
|
||
builder: (context, state) => WorkListPage(
|
||
session: dependencies.session,
|
||
repository: dependencies.repository,
|
||
completed: true,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
StatefulShellBranch(
|
||
routes: [
|
||
GoRoute(
|
||
path: '/me',
|
||
builder: (context, state) => ProfilePage(
|
||
session: dependencies.session,
|
||
repository: dependencies.repository,
|
||
drafts: dependencies.drafts,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
],
|
||
);
|
||
|
||
class _ServiceShell extends StatelessWidget {
|
||
const _ServiceShell({required this.navigationShell, required this.roleCode});
|
||
|
||
final StatefulNavigationShell navigationShell;
|
||
final String roleCode;
|
||
|
||
@override
|
||
Widget build(BuildContext context) => Scaffold(
|
||
body: navigationShell,
|
||
bottomNavigationBar: NavigationBar(
|
||
selectedIndex: navigationShell.currentIndex,
|
||
onDestinationSelected: (index) => navigationShell.goBranch(
|
||
index,
|
||
initialLocation: index == navigationShell.currentIndex,
|
||
),
|
||
destinations: [
|
||
NavigationDestination(
|
||
icon: Icon(
|
||
roleCode == 'delivery' ? Icons.local_shipping_outlined : Icons.assignment_outlined,
|
||
),
|
||
selectedIcon: Icon(roleCode == 'delivery' ? Icons.local_shipping : Icons.assignment),
|
||
label: roleCode == 'delivery'
|
||
? '配送'
|
||
: roleCode == 'operations'
|
||
? '安检'
|
||
: '工单',
|
||
),
|
||
const NavigationDestination(
|
||
icon: Icon(Icons.history),
|
||
selectedIcon: Icon(Icons.history_toggle_off),
|
||
label: '记录',
|
||
),
|
||
const NavigationDestination(
|
||
icon: Icon(Icons.person_outline),
|
||
selectedIcon: Icon(Icons.person),
|
||
label: '我的',
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|