/// App Router Configuration /// /// Centralized routing configuration using go_router. /// Defines all routes, navigation guards, and deep linking. library; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:worker/features/cart/presentation/pages/cart_page.dart'; import 'package:worker/features/favorites/presentation/pages/favorites_page.dart'; import 'package:worker/features/loyalty/presentation/pages/rewards_page.dart'; import 'package:worker/features/main/presentation/pages/main_scaffold.dart'; import 'package:worker/features/orders/presentation/pages/order_detail_page.dart'; import 'package:worker/features/orders/presentation/pages/orders_page.dart'; import 'package:worker/features/orders/presentation/pages/payment_detail_page.dart'; import 'package:worker/features/orders/presentation/pages/payments_page.dart'; import 'package:worker/features/products/presentation/pages/product_detail_page.dart'; import 'package:worker/features/products/presentation/pages/products_page.dart'; import 'package:worker/features/promotions/presentation/pages/promotion_detail_page.dart'; /// App Router /// /// Handles navigation throughout the app using declarative routing. /// Features: /// - Named routes for type-safe navigation /// - Authentication guards (TODO: implement when auth is ready) /// - Deep linking support /// - Transition animations class AppRouter { /// Router configuration static final GoRouter router = GoRouter( // Initial route initialLocation: RouteNames.home, // Route definitions routes: [ // Main Route (with bottom navigation) GoRoute( path: RouteNames.home, name: RouteNames.home, pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const MainScaffold(), ), ), // Products Route (full screen, no bottom nav) GoRoute( path: RouteNames.products, name: RouteNames.products, pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const ProductsPage(), ), ), // Product Detail Route GoRoute( path: RouteNames.productDetail, name: RouteNames.productDetail, pageBuilder: (context, state) { final productId = state.pathParameters['id']; return MaterialPage( key: state.pageKey, child: ProductDetailPage(productId: productId ?? ''), ); }, ), // Promotion Detail Route GoRoute( path: RouteNames.promotionDetail, name: RouteNames.promotionDetail, pageBuilder: (context, state) { final promotionId = state.pathParameters['id']; return MaterialPage( key: state.pageKey, child: PromotionDetailPage(promotionId: promotionId), ); }, ), // Cart Route GoRoute( path: RouteNames.cart, name: RouteNames.cart, pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const CartPage(), ), ), // Favorites Route GoRoute( path: RouteNames.favorites, name: RouteNames.favorites, pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const FavoritesPage(), ), ), // Loyalty Rewards Route GoRoute( path: '/loyalty/rewards', name: 'loyalty_rewards', pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const RewardsPage(), ), ), // Orders Route GoRoute( path: RouteNames.orders, name: RouteNames.orders, pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const OrdersPage(), ), ), // Order Detail Route GoRoute( path: RouteNames.orderDetail, name: RouteNames.orderDetail, pageBuilder: (context, state) { final orderId = state.pathParameters['id']; return MaterialPage( key: state.pageKey, child: OrderDetailPage(orderId: orderId ?? ''), ); }, ), // Payments Route GoRoute( path: RouteNames.payments, name: RouteNames.payments, pageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: const PaymentsPage(), ), ), // Payment Detail Route GoRoute( path: RouteNames.paymentDetail, name: RouteNames.paymentDetail, pageBuilder: (context, state) { final invoiceId = state.pathParameters['id']; return MaterialPage( key: state.pageKey, child: PaymentDetailPage(invoiceId: invoiceId ?? ''), ); }, ), // TODO: Add more routes as features are implemented ], // Error page for unknown routes errorPageBuilder: (context, state) => MaterialPage( key: state.pageKey, child: Scaffold( appBar: AppBar( title: const Text('Không tìm thấy trang'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.error_outline, size: 64, color: Colors.red, ), const SizedBox(height: 16), const Text( 'Trang không tồn tại', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), ), const SizedBox(height: 8), Text( state.uri.toString(), style: const TextStyle(color: Colors.grey), ), const SizedBox(height: 24), ElevatedButton.icon( onPressed: () => context.go(RouteNames.home), icon: const Icon(Icons.home), label: const Text('Về trang chủ'), ), ], ), ), ), ), // Redirect logic for authentication (TODO: implement when auth is ready) // redirect: (context, state) { // final isLoggedIn = false; // TODO: Get from auth provider // final isOnLoginPage = state.matchedLocation == RouteNames.login; // // if (!isLoggedIn && !isOnLoginPage) { // return RouteNames.login; // } // // if (isLoggedIn && isOnLoginPage) { // return RouteNames.home; // } // // return null; // }, // Debug logging (disable in production) debugLogDiagnostics: true, ); } /// Route Names /// /// Centralized route name constants for type-safe navigation. /// Use these constants instead of hardcoded strings. /// /// Example: /// ```dart /// context.go(RouteNames.home); /// context.push(RouteNames.products); /// ``` class RouteNames { // Private constructor to prevent instantiation RouteNames._(); // Main Routes static const String home = '/'; static const String products = '/products'; static const String productDetail = '/products/:id'; static const String cart = '/cart'; static const String favorites = '/favorites'; static const String checkout = '/checkout'; static const String orderSuccess = '/order-success'; // Loyalty Routes static const String loyalty = '/loyalty'; static const String rewards = '/loyalty/rewards'; static const String pointsHistory = '/loyalty/points-history'; static const String myGifts = '/loyalty/gifts'; static const String referral = '/loyalty/referral'; // Orders & Payments Routes static const String orders = '/orders'; static const String orderDetail = '/orders/:id'; static const String payments = '/payments'; static const String paymentDetail = '/payments/:id'; // Projects & Quotes Routes static const String projects = '/projects'; static const String projectDetail = '/projects/:id'; static const String projectCreate = '/projects/create'; static const String quotes = '/quotes'; static const String quoteDetail = '/quotes/:id'; static const String quoteCreate = '/quotes/create'; // Account Routes static const String account = '/account'; static const String profile = '/account/profile'; static const String addresses = '/account/addresses'; static const String addressForm = '/account/addresses/form'; static const String changePassword = '/account/change-password'; static const String settings = '/account/settings'; // Promotions & Notifications Routes static const String promotions = '/promotions'; static const String promotionDetail = '/promotions/:id'; static const String notifications = '/notifications'; // Chat Route static const String chat = '/chat'; // Authentication Routes (TODO: implement when auth feature is ready) static const String login = '/login'; static const String otpVerification = '/otp-verification'; static const String register = '/register'; } /// Route Extensions /// /// Helper extensions for common navigation patterns. extension GoRouterExtension on BuildContext { /// Navigate to home page void goHome() => go(RouteNames.home); /// Navigate to products page void goProducts() => go(RouteNames.products); /// Navigate to cart page void goCart() => go(RouteNames.cart); /// Navigate to favorites page void goFavorites() => go(RouteNames.favorites); /// Navigate to loyalty page void goLoyalty() => go(RouteNames.loyalty); /// Navigate to orders page void goOrders() => go(RouteNames.orders); /// Navigate to projects page void goProjects() => go(RouteNames.projects); /// Navigate to account page void goAccount() => go(RouteNames.account); /// Navigate to promotions page void goPromotions() => go(RouteNames.promotions); /// Navigate to notifications page void goNotifications() => go(RouteNames.notifications); /// Navigate to chat page void goChat() => go(RouteNames.chat); }