import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:intl/intl.dart'; import '../providers/supplement_provider.dart'; import '../models/supplement.dart'; import '../widgets/supplement_card.dart'; import 'add_supplement_screen.dart'; class SupplementsListScreen extends StatelessWidget { const SupplementsListScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('My Supplements'), backgroundColor: Theme.of(context).colorScheme.inversePrimary, ), body: Consumer( builder: (context, provider, child) { if (provider.isLoading) { return const Center(child: CircularProgressIndicator()); } if (provider.supplements.isEmpty) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.medication_outlined, size: 64, color: Theme.of(context).colorScheme.onSurfaceVariant, ), const SizedBox(height: 16), Text( 'No supplements added yet', style: TextStyle( fontSize: 18, color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), const SizedBox(height: 8), Text( 'Tap the + button to add your first supplement', style: TextStyle( color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ], ), ); } return RefreshIndicator( onRefresh: () async { await provider.loadSupplements(); }, child: Column( children: [ // Today's Intakes Section if (provider.todayIntakes.isNotEmpty) ...[ Container( width: double.infinity, margin: const EdgeInsets.all(16), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Theme.of(context).colorScheme.primaryContainer, borderRadius: BorderRadius.circular(12), border: Border.all(color: Theme.of(context).colorScheme.outline), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon(Icons.check_circle, color: Theme.of(context).colorScheme.primary), const SizedBox(width: 8), Text( 'Today\'s Intakes', style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Theme.of(context).colorScheme.onPrimaryContainer, ), ), ], ), const SizedBox(height: 8), ...provider.todayIntakes.map((intake) { final takenAt = DateTime.parse(intake['takenAt']); return Padding( padding: const EdgeInsets.only(bottom: 4), child: Text( '${intake['supplementName']} - ${intake['dosageTaken']} ${intake['supplementUnit']} at ${DateFormat('HH:mm').format(takenAt)}', style: TextStyle(color: Theme.of(context).colorScheme.onPrimaryContainer), ), ); }), ], ), ), ], // Supplements List Expanded( child: ListView.builder( padding: const EdgeInsets.all(16), itemCount: provider.supplements.length, itemBuilder: (context, index) { final supplement = provider.supplements[index]; return SupplementCard( supplement: supplement, onTake: () => _showTakeDialog(context, supplement), onEdit: () => _editSupplement(context, supplement), onDelete: () => _deleteSupplement(context, supplement), ); }, ), ), ], ), ); }, ), ); } void _showTakeDialog(BuildContext context, Supplement supplement) { final unitsController = TextEditingController(text: supplement.numberOfUnits.toString()); final notesController = TextEditingController(); showDialog( context: context, builder: (context) => StatefulBuilder( builder: (context, setState) { final units = int.tryParse(unitsController.text) ?? supplement.numberOfUnits; final totalDosage = supplement.dosageAmount * units; return AlertDialog( title: Text('Take ${supplement.name}'), content: Column( mainAxisSize: MainAxisSize.min, children: [ Row( children: [ Expanded( child: TextField( controller: unitsController, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: 'Number of ${supplement.unitType}', border: const OutlineInputBorder(), suffixText: supplement.unitType, ), onChanged: (value) => setState(() {}), ), ), ], ), const SizedBox(height: 8), Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceVariant, borderRadius: BorderRadius.circular(4), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Total dosage:', style: TextStyle( fontSize: 12, color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), Text( '${totalDosage.toStringAsFixed(1)} ${supplement.unit}', style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, color: Theme.of(context).colorScheme.onSurface, ), ), ], ), ), const SizedBox(height: 16), TextField( controller: notesController, decoration: const InputDecoration( labelText: 'Notes (optional)', border: OutlineInputBorder(), ), maxLines: 2, ), ], ), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Cancel'), ), ElevatedButton( onPressed: () { final unitsTaken = int.tryParse(unitsController.text) ?? supplement.numberOfUnits; final totalDosageTaken = supplement.dosageAmount * unitsTaken; context.read().recordIntake( supplement.id!, totalDosageTaken, unitsTaken: unitsTaken, notes: notesController.text.isNotEmpty ? notesController.text : null, ); Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${supplement.name} recorded!'), backgroundColor: Colors.green, ), ); }, child: const Text('Take'), ), ], ); }, ), ); } void _editSupplement(BuildContext context, Supplement supplement) { Navigator.of(context).push( MaterialPageRoute( builder: (context) => AddSupplementScreen(supplement: supplement), ), ); } void _deleteSupplement(BuildContext context, Supplement supplement) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Delete Supplement'), content: Text('Are you sure you want to delete ${supplement.name}?'), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Cancel'), ), ElevatedButton( onPressed: () { context.read().deleteSupplement(supplement.id!); Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${supplement.name} deleted'), backgroundColor: Colors.red, ), ); }, style: ElevatedButton.styleFrom(backgroundColor: Colors.red), child: const Text('Delete'), ), ], ), ); } }