Files
minhthu/lib/core/routing/app_router.dart
2025-09-16 23:14:35 +07:00

211 lines
5.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../../features/scanner/presentation/pages/home_page.dart';
import '../../features/scanner/presentation/pages/detail_page.dart';
/// Application router configuration using GoRouter
final GoRouter appRouter = GoRouter(
initialLocation: '/',
debugLogDiagnostics: true,
routes: [
// Home route - Main scanner screen
GoRoute(
path: '/',
name: 'home',
builder: (BuildContext context, GoRouterState state) {
return const HomePage();
},
),
// Detail route - Edit scan data
GoRoute(
path: '/detail/:barcode',
name: 'detail',
builder: (BuildContext context, GoRouterState state) {
final barcode = state.pathParameters['barcode']!;
return DetailPage(barcode: barcode);
},
redirect: (BuildContext context, GoRouterState state) {
final barcode = state.pathParameters['barcode'];
// Ensure barcode is not empty
if (barcode == null || barcode.trim().isEmpty) {
return '/';
}
return null; // No redirect needed
},
),
// Settings route (optional for future expansion)
GoRoute(
path: '/settings',
name: 'settings',
builder: (BuildContext context, GoRouterState state) {
return const SettingsPlaceholderPage();
},
),
// About route (optional for future expansion)
GoRoute(
path: '/about',
name: 'about',
builder: (BuildContext context, GoRouterState state) {
return const AboutPlaceholderPage();
},
),
],
// Error handling
errorBuilder: (context, state) {
return Scaffold(
appBar: AppBar(
title: const Text('Page Not Found'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 64,
color: Theme.of(context).colorScheme.error,
),
const SizedBox(height: 16),
Text(
'Page Not Found',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
'The page "${state.path}" does not exist.',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('Go Home'),
),
],
),
),
);
},
// Redirect handler for authentication or onboarding (optional)
redirect: (BuildContext context, GoRouterState state) {
// Add any global redirect logic here
// For example, redirect to onboarding or login if needed
return null; // No global redirect
},
);
/// Placeholder page for settings (for future implementation)
class SettingsPlaceholderPage extends StatelessWidget {
const SettingsPlaceholderPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => context.pop(),
),
),
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.settings,
size: 64,
color: Colors.grey,
),
SizedBox(height: 16),
Text(
'Settings',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'Settings page coming soon',
style: TextStyle(
color: Colors.grey,
),
),
],
),
),
);
}
}
/// Placeholder page for about (for future implementation)
class AboutPlaceholderPage extends StatelessWidget {
const AboutPlaceholderPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('About'),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => context.pop(),
),
),
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.info_outline,
size: 64,
color: Colors.grey,
),
SizedBox(height: 16),
Text(
'Barcode Scanner App',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'Version 1.0.0',
style: TextStyle(
color: Colors.grey,
),
),
],
),
),
);
}
}
/// Extension methods for easier navigation
extension AppRouterExtension on BuildContext {
/// Navigate to home page
void goHome() => go('/');
/// Navigate to detail page with barcode
void goToDetail(String barcode) => go('/detail/$barcode');
/// Navigate to settings
void goToSettings() => go('/settings');
/// Navigate to about page
void goToAbout() => go('/about');
}