import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../../../../core/theme/app_theme.dart'; import '../../data/models/route_model.dart'; import '../providers/route_provider.dart'; import '../widgets/route_card.dart'; import 'route_details_page.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { @override void initState() { super.initState(); // Load routes for demo driver // In production, use actual driver ID from authentication Future.microtask(() { context.read().loadRoutes('driver_1'); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('My Routes'), actions: [ IconButton( icon: const Icon(Icons.refresh), onPressed: () { context.read().loadRoutes('driver_1'); }, ), IconButton( icon: const Icon(Icons.person), onPressed: () { // Navigate to profile page }, ), ], ), body: Consumer( builder: (context, routeProvider, child) { if (routeProvider.isLoading) { return const Center( child: CircularProgressIndicator(), ); } if (routeProvider.error != null) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.error_outline, size: 64, color: AppTheme.errorColor, ), const SizedBox(height: 16), Text( 'Error loading routes', style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: 8), Padding( padding: const EdgeInsets.symmetric(horizontal: 32), child: Text( routeProvider.error!, textAlign: TextAlign.center, style: Theme.of(context).textTheme.bodyMedium, ), ), const SizedBox(height: 24), ElevatedButton.icon( onPressed: () { routeProvider.loadRoutes('driver_1'); }, icon: const Icon(Icons.refresh), label: const Text('Retry'), ), ], ), ); } if (routeProvider.routes.isEmpty) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.route, size: 64, color: Colors.grey, ), const SizedBox(height: 16), Text( 'No routes available', style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: 8), Text( 'Check back later for new routes', style: Theme.of(context).textTheme.bodyMedium, ), ], ), ); } return RefreshIndicator( onRefresh: () async { await routeProvider.loadRoutes('driver_1'); }, child: ListView( padding: const EdgeInsets.symmetric(vertical: 8), children: [ _buildSummaryCard(context, routeProvider.routes), const SizedBox(height: 8), ...routeProvider.routes.map((route) { return RouteCard( route: route, onTap: () { routeProvider.setCurrentRoute(route); Navigator.push( context, MaterialPageRoute( builder: (context) => const RouteDetailsPage(), ), ); }, ); }), ], ), ); }, ), ); } Widget _buildSummaryCard(BuildContext context, List routes) { final todayRoutes = routes.where((route) { return route.date.day == DateTime.now().day && route.date.month == DateTime.now().month && route.date.year == DateTime.now().year; }).toList(); final completedRoutes = todayRoutes.where((r) => r.status == RouteStatus.completed).length; return Card( margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Today\'s Summary', style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: 16), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ _buildSummaryItem( context, 'Total Routes', todayRoutes.length.toString(), Icons.route, AppTheme.primaryColor, ), _buildSummaryItem( context, 'Completed', completedRoutes.toString(), Icons.check_circle, AppTheme.completedColor, ), _buildSummaryItem( context, 'Pending', (todayRoutes.length - completedRoutes).toString(), Icons.pending, AppTheme.warningColor, ), ], ), ], ), ), ); } Widget _buildSummaryItem( BuildContext context, String label, String value, IconData icon, Color color, ) { return Column( children: [ Icon(icon, color: color, size: 32), const SizedBox(height: 8), Text( value, style: Theme.of(context).textTheme.displaySmall?.copyWith( color: color, fontWeight: FontWeight.bold, ), ), Text( label, style: Theme.of(context).textTheme.bodySmall, ), ], ); } }