app_router.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/material.dart';
  2. import 'package:go_router/go_router.dart';
  3. import '../pages/dashboard_page.dart';
  4. import '../pages/upload_page.dart';
  5. import '../pages/parse_compare_page.dart';
  6. import '../pages/ai_process_page.dart';
  7. import '../pages/traction_page.dart';
  8. import '../pages/result_page.dart';
  9. import '../utils/constants.dart';
  10. /// 应用路由配置
  11. class AppRouter {
  12. static const String _initialLocation = String.fromEnvironment(
  13. 'DEFAULT_ROUTE',
  14. defaultValue: AppRoutes.home,
  15. // defaultValue: '${AppRoutes.result}/1',
  16. );
  17. static final GoRouter router = GoRouter(
  18. initialLocation: _initialLocation,
  19. routes: [
  20. GoRoute(
  21. path: AppRoutes.home,
  22. name: 'home',
  23. builder: (context, state) => const DashboardPage(),
  24. ),
  25. GoRoute(
  26. path: AppRoutes.upload,
  27. name: 'upload',
  28. builder: (context, state) => const UploadPage(),
  29. ),
  30. GoRoute(
  31. path: '${AppRoutes.parse}/:id',
  32. name: 'parse',
  33. builder: (context, state) {
  34. final id = state.pathParameters['id']!;
  35. return ParseComparePage(documentId: id);
  36. },
  37. ),
  38. GoRoute(
  39. path: '${AppRoutes.process}/:id',
  40. name: 'process',
  41. builder: (context, state) {
  42. final id = state.pathParameters['id']!;
  43. return AIProcessPage(documentId: id);
  44. },
  45. ),
  46. GoRoute(
  47. path: '${AppRoutes.traction}/:id',
  48. name: 'traction',
  49. builder: (context, state) {
  50. final id = state.pathParameters['id']!;
  51. return TractionPage(documentId: id);
  52. },
  53. ),
  54. GoRoute(
  55. path: '${AppRoutes.result}/:id',
  56. name: 'result',
  57. builder: (context, state) {
  58. final id = state.pathParameters['id']!;
  59. return ResultPage(documentId: id);
  60. },
  61. ),
  62. ],
  63. errorBuilder: (context, state) => Scaffold(
  64. body: Center(
  65. child: Column(
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. children: [
  68. const Icon(Icons.error_outline, size: 64, color: Colors.red),
  69. const SizedBox(height: 16),
  70. Text('页面未找到: ${state.uri}'),
  71. const SizedBox(height: 16),
  72. ElevatedButton(
  73. onPressed: () => context.go(AppRoutes.home),
  74. child: const Text('返回首页'),
  75. ),
  76. ],
  77. ),
  78. ),
  79. ),
  80. );
  81. }