import 'dart:async'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:intl/intl.dart'; import '../models/schedule_item.dart'; import '../services/schedule_service.dart'; class ScheduleListWidget extends StatefulWidget { const ScheduleListWidget({super.key}); @override State createState() => _ScheduleListWidgetState(); } class _ScheduleListWidgetState extends State { Timer? _timer; List _schedules = []; bool _isLoading = true; String? _error; @override void initState() { super.initState(); _fetchSchedules(); _startAutoRefresh(); } void _startAutoRefresh() { _timer = Timer.periodic(const Duration(seconds: 30), (timer) { _fetchSchedules(); }); } Future _fetchSchedules() async { try { final data = await Provider.of( context, listen: false, ).fetchWeeklySchedules(); if (mounted) { setState(() { _schedules = data; _isLoading = false; _error = null; }); } } catch (e) { if (mounted) { setState(() { _error = 'Failed to load schedules'; _isLoading = false; }); } } } @override void dispose() { _timer?.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return Container( decoration: BoxDecoration( color: Theme.of(context).cardTheme.color, borderRadius: BorderRadius.circular(16), ), padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Weekly Schedule', style: Theme.of(context).textTheme.titleLarge?.copyWith( color: Colors.white, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 12), Expanded( child: _buildContent(), ), ], ), ); } Widget _buildContent() { if (_isLoading && _schedules.isEmpty) { return const Center(child: CircularProgressIndicator()); } if (_error != null && _schedules.isEmpty) { return Center( child: Text( _error!, style: const TextStyle(color: Colors.white54), ), ); } if (_schedules.isEmpty) { return const Center( child: Text( 'No schedules this week', style: TextStyle(color: Colors.white54), ), ); } // Sort by date final sortedSchedules = List.from(_schedules); sortedSchedules.sort((a, b) => a.startDate.compareTo(b.startDate)); return ListView.separated( itemCount: sortedSchedules.length, separatorBuilder: (context, index) => const Divider(color: Colors.white10), itemBuilder: (context, index) { final item = sortedSchedules[index]; final timeStr = item.isAllDay ? 'All Day' : DateFormat('HH:mm').format(item.startDate); return ListTile( contentPadding: EdgeInsets.zero, leading: Container( padding: const EdgeInsets.symmetric( horizontal: 12, vertical: 8, ), decoration: BoxDecoration( color: Colors.white10, borderRadius: BorderRadius.circular(8), ), child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ Text( DateFormat('d').format(item.startDate), style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18, ), ), Text( DateFormat('E').format(item.startDate), style: const TextStyle( color: Colors.white70, fontSize: 12, ), ), ], ), ), title: Text( item.title, style: const TextStyle( color: Colors.white, fontWeight: FontWeight.w500, ), ), subtitle: Text( timeStr, style: const TextStyle(color: Colors.white54), ), ); }, ); } }