Files
supplements/lib/screens/history_screen.dart
Menno van Leeuwen 666008f05d Refactor and enhance UI components across multiple screens
- Updated date and time formatting in debug notifications screen for clarity.
- Wrapped context-dependent state updates in post-frame callbacks in history screen to ensure proper context usage.
- Improved layout and styling in settings screen by reordering radio list tiles.
- Enhanced logging in auto sync service for better error tracking.
- Added context mounted checks in notification router to prevent errors during navigation.
- Updated bulk take dialog to use new UI components from shadcn_ui package.
- Refactored take supplement dialog to utilize shadcn_ui for a more modern look and feel.
- Adjusted info chip and supplement card widgets to use updated color schemes and layouts.
- Updated pubspec.yaml and pubspec.lock to include new dependencies and versions.
2025-08-31 19:15:32 +02:00

795 lines
33 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import '../providers/settings_provider.dart';
import '../providers/supplement_provider.dart';
class HistoryScreen extends StatefulWidget {
const HistoryScreen({super.key});
@override
State<HistoryScreen> createState() => _HistoryScreenState();
}
class _HistoryScreenState extends State<HistoryScreen> {
int _selectedMonth = DateTime.now().month;
int _selectedYear = DateTime.now().year;
DateTime? _selectedDay;
@override
void initState() {
super.initState();
_selectedDay = DateTime.now(); // Set today as the default selected day
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Intake History'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: _buildCalendarView(),
);
}
Widget _buildCalendarView() {
return Column(
children: [
Container(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {
setState(() {
if (_selectedMonth == 1) {
_selectedMonth = 12;
_selectedYear--;
} else {
_selectedMonth--;
}
});
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
},
icon: const Icon(Icons.chevron_left),
),
InkWell(
onTap: _showMonthPicker,
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text(
DateFormat('MMMM yyyy').format(DateTime(_selectedYear, _selectedMonth)),
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
),
IconButton(
onPressed: () {
final now = DateTime.now();
if (_selectedYear < now.year || (_selectedYear == now.year && _selectedMonth < now.month)) {
setState(() {
if (_selectedMonth == 12) {
_selectedMonth = 1;
_selectedYear++;
} else {
_selectedMonth++;
}
});
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
}
},
icon: const Icon(Icons.chevron_right),
),
],
),
),
Expanded(
child: Consumer<SupplementProvider>(
builder: (context, provider, child) {
// Group intakes by date
final groupedIntakes = <String, List<Map<String, dynamic>>>{};
for (final intake in provider.monthlyIntakes) {
final date = DateTime.parse(intake['takenAt']);
final dateKey = DateFormat('yyyy-MM-dd').format(date);
groupedIntakes.putIfAbsent(dateKey, () => []);
groupedIntakes[dateKey]!.add(intake);
}
return LayoutBuilder(
builder: (context, constraints) {
final isWideScreen = constraints.maxWidth > 800;
if (isWideScreen) {
// Desktop/tablet layout: side-by-side
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Calendar on the left
Expanded(
flex: 2,
child: Container(
margin: const EdgeInsets.all(16),
child: _buildCalendar(groupedIntakes),
),
),
// Selected day details on the right
Expanded(
flex: 3,
child: Container(
// add a bit more horizontal spacing between calendar and card
margin: const EdgeInsets.fromLTRB(8, 16, 16, 16),
child: SingleChildScrollView(
child: _buildSelectedDayDetails(groupedIntakes),
),
),
),
],
);
} else {
// Mobile layout: vertical stack
return SingleChildScrollView(
child: Column(
children: [
// Calendar
Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: _buildCalendar(groupedIntakes),
),
const SizedBox(height: 16),
// Selected day details
Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: _buildSelectedDayDetails(groupedIntakes),
),
],
),
);
}
},
);
},
),
),
],
);
}
void _showMonthPicker() async {
final now = DateTime.now();
final picked = await showDatePicker(
context: context,
initialDate: DateTime(_selectedYear, _selectedMonth),
firstDate: DateTime(2020),
lastDate: now,
initialDatePickerMode: DatePickerMode.year,
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
dialogTheme: DialogThemeData(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16)),
),
),
),
child: child!,
);
},
);
if (picked != null) {
if (!context.mounted) return;
setState(() {
_selectedMonth = picked.month;
_selectedYear = picked.year;
_selectedDay = picked; // Set the selected day to the picked date
});
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
}
}
void _deleteIntake(BuildContext context, int intakeId, String supplementName) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Delete Intake'),
content: Text('Are you sure you want to delete this $supplementName intake?'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () async {
await context.read<SupplementProvider>().deleteIntake(intakeId);
if (!context.mounted) return;
Navigator.of(context).pop();
// Force refresh of the UI
setState(() {});
// Force refresh of the current view data
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('$supplementName intake deleted'),
backgroundColor: Colors.red,
),
);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
child: const Text('Delete'),
),
],
),
);
}
Widget _buildCalendar(Map<String, List<Map<String, dynamic>>> groupedIntakes) {
final firstDayOfMonth = DateTime(_selectedYear, _selectedMonth, 1);
final lastDayOfMonth = DateTime(_selectedYear, _selectedMonth + 1, 0);
final firstWeekday = firstDayOfMonth.weekday;
final daysInMonth = lastDayOfMonth.day;
// Calculate how many cells we need (including empty ones for alignment)
final totalCells = ((daysInMonth + firstWeekday - 1) / 7).ceil() * 7;
final weeks = (totalCells / 7).ceil();
return LayoutBuilder(
builder: (context, constraints) {
final isWideScreen = constraints.maxWidth > 800;
// Calculate calendar height based on number of weeks needed
final cellHeight = isWideScreen ? 56.0 : 48.0;
final calendarContentHeight = (weeks * cellHeight) + 60; // +60 for headers and padding
final calendarHeight = isWideScreen ? 400.0 : calendarContentHeight;
return Card(
child: SizedBox(
height: calendarHeight,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Calendar header (weekdays)
Row(
children: [
Expanded(
child: Center(
child: Text(
'Mon',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
SizedBox(width: 4),
Expanded(
child: Center(
child: Text(
'Tue',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
SizedBox(width: 4),
Expanded(
child: Center(
child: Text(
'Wed',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
SizedBox(width: 4),
Expanded(
child: Center(
child: Text(
'Thu',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
SizedBox(width: 4),
Expanded(
child: Center(
child: Text(
'Fri',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
SizedBox(width: 4),
Expanded(
child: Center(
child: Text(
'Sat',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
SizedBox(width: 4),
Expanded(
child: Center(
child: Text(
'Sun',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
),
],
),
const SizedBox(height: 8),
// Calendar grid
Expanded(
child: GridView.builder(
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7,
childAspectRatio: 1.0,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
),
itemCount: totalCells,
itemBuilder: (context, index) {
final dayNumber = index - firstWeekday + 2;
if (dayNumber < 1 || dayNumber > daysInMonth) {
return const SizedBox.shrink(); // Empty cell
}
final date = DateTime(_selectedYear, _selectedMonth, dayNumber);
final dateKey = DateFormat('yyyy-MM-dd').format(date);
final hasIntakes = groupedIntakes.containsKey(dateKey);
final intakeCount = hasIntakes ? groupedIntakes[dateKey]!.length : 0;
final isSelected = _selectedDay != null &&
DateFormat('yyyy-MM-dd').format(_selectedDay!) == dateKey;
final isToday = DateFormat('yyyy-MM-dd').format(DateTime.now()) == dateKey;
return GestureDetector(
onTap: () {
setState(() {
_selectedDay = date;
});
},
child: Container(
margin: const EdgeInsets.all(1),
decoration: BoxDecoration(
color: isSelected
? Theme.of(context).colorScheme.primary
: hasIntakes
? Theme.of(context).colorScheme.primaryContainer
: null,
border: isToday
? Border.all(color: Theme.of(context).colorScheme.secondary, width: 2)
: null,
borderRadius: BorderRadius.circular(8),
),
child: Stack(
children: [
Center(
child: Text(
'$dayNumber',
style: TextStyle(
color: isSelected
? Theme.of(context).colorScheme.onPrimary
: hasIntakes
? Theme.of(context).colorScheme.onPrimaryContainer
: Theme.of(context).colorScheme.onSurface,
fontWeight: isToday ? FontWeight.bold : FontWeight.normal,
fontSize: isWideScreen ? 16 : 14,
),
),
),
if (hasIntakes)
Positioned(
top: 2,
right: 2,
child: Container(
padding: EdgeInsets.all(isWideScreen ? 3 : 2),
decoration: BoxDecoration(
color: isSelected
? Theme.of(context).colorScheme.onPrimary
: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(8),
),
constraints: BoxConstraints(
minWidth: isWideScreen ? 18 : 16,
minHeight: isWideScreen ? 18 : 16,
),
child: Text(
'$intakeCount',
style: TextStyle(
color: isSelected
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.onPrimary,
fontSize: isWideScreen ? 11 : 10,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
),
),
],
),
),
);
},
),
),
],
),
),
),
);
},
);
}
Widget _buildSelectedDayDetails(Map<String, List<Map<String, dynamic>>> groupedIntakes) {
return LayoutBuilder(
builder: (context, constraints) {
final isWideScreen = constraints.maxWidth > 600;
if (_selectedDay == null) {
return Card(
child: Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.touch_app,
size: isWideScreen ? 64 : 48,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
'Tap a date on the calendar to see details',
style: TextStyle(
fontSize: isWideScreen ? 18 : 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
final dateKey = DateFormat('yyyy-MM-dd').format(_selectedDay!);
final dayIntakes = groupedIntakes[dateKey] ?? [];
if (dayIntakes.isEmpty) {
return Card(
child: Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.event_available,
size: isWideScreen ? 64 : 48,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
'No supplements taken on ${DateFormat('MMM d, yyyy').format(_selectedDay!)}',
style: TextStyle(
fontSize: isWideScreen ? 18 : 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: double.infinity,
padding: EdgeInsets.all(isWideScreen ? 20 : 16),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
DateFormat('EEEE, MMM d, yyyy').format(_selectedDay!),
style: TextStyle(
fontSize: isWideScreen ? 20 : 18,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onPrimaryContainer,
),
),
const SizedBox(height: 4),
Text(
'${dayIntakes.length} supplement${dayIntakes.length != 1 ? 's' : ''} taken',
style: TextStyle(
fontSize: isWideScreen ? 16 : 14,
color: Theme.of(context).colorScheme.onPrimaryContainer.withValues(alpha: 0.8),
),
),
],
),
),
Padding(
padding: EdgeInsets.all(isWideScreen ? 20 : 16),
child: Builder(
builder: (context) {
final settingsProvider = Provider.of<SettingsProvider>(context, listen: false);
// Sort once per render
final sortedDayIntakes = List<Map<String, dynamic>>.from(dayIntakes)
..sort((a, b) => DateTime.parse(a['takenAt']).compareTo(DateTime.parse(b['takenAt'])));
// Helpers
String timeCategory(DateTime dt) {
final h = dt.hour;
if (h >= settingsProvider.morningStart && h <= settingsProvider.morningEnd) return 'morning';
if (h >= settingsProvider.afternoonStart && h <= settingsProvider.afternoonEnd) return 'afternoon';
if (h >= settingsProvider.eveningStart && h <= settingsProvider.eveningEnd) return 'evening';
final ns = settingsProvider.nightStart;
final ne = settingsProvider.nightEnd;
final inNight = ns <= ne ? (h >= ns && h <= ne) : (h >= ns || h <= ne);
return inNight ? 'night' : 'anytime';
}
String? sectionRange(String cat) {
switch (cat) {
case 'morning':
return settingsProvider.morningRange;
case 'afternoon':
return settingsProvider.afternoonRange;
case 'evening':
return settingsProvider.eveningRange;
case 'night':
return settingsProvider.nightRange;
default:
return null;
}
}
Widget headerFor(String cat) {
late final IconData icon;
late final Color color;
late final String title;
switch (cat) {
case 'morning':
icon = Icons.wb_sunny;
color = Colors.orange;
title = 'Morning';
break;
case 'afternoon':
icon = Icons.light_mode;
color = Colors.blue;
title = 'Afternoon';
break;
case 'evening':
icon = Icons.nightlight_round;
color = Colors.indigo;
title = 'Evening';
break;
case 'night':
icon = Icons.bedtime;
color = Colors.purple;
title = 'Night';
break;
default:
icon = Icons.schedule;
color = Colors.grey;
title = 'Anytime';
}
final range = sectionRange(cat);
return Container(
margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: color.withValues(alpha: 0.3),
width: 1,
),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.2),
shape: BoxShape.circle,
),
child: Icon(
icon,
size: 20,
color: color,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: color,
),
),
if (range != null) ...[
Text(
'($range)',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: color.withValues(alpha: 0.8),
),
),
],
],
),
),
],
),
);
}
// Build a non-scrollable list so the card auto-expands to fit content
final List<Widget> children = [];
for (int index = 0; index < sortedDayIntakes.length; index++) {
final intake = sortedDayIntakes[index];
final takenAt = DateTime.parse(intake['takenAt']);
final units = (intake['unitsTaken'] as num?)?.toDouble() ?? 1.0;
final currentCategory = timeCategory(takenAt);
final needsHeader = index == 0
? true
: currentCategory != timeCategory(DateTime.parse(sortedDayIntakes[index - 1]['takenAt']));
if (needsHeader) {
children.add(headerFor(currentCategory));
}
children.add(
Card(
margin: const EdgeInsets.only(bottom: 12),
elevation: 2,
child: Padding(
padding: EdgeInsets.all(isWideScreen ? 16 : 12),
child: Row(
children: [
CircleAvatar(
backgroundColor: Theme.of(context).colorScheme.primary,
radius: isWideScreen ? 24 : 20,
child: Icon(
Icons.medication,
color: Theme.of(context).colorScheme.onPrimary,
size: isWideScreen ? 24 : 20,
),
),
SizedBox(width: isWideScreen ? 16 : 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
intake['supplementName'],
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: isWideScreen ? 16 : 14,
),
),
const SizedBox(height: 4),
Text(
'${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${intake['supplementUnitType'] ?? 'units'} at ${DateFormat('HH:mm').format(takenAt)}',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w500,
fontSize: isWideScreen ? 14 : 12,
),
),
if (intake['notes'] != null && intake['notes'].toString().isNotEmpty) ...[
const SizedBox(height: 4),
Text(
intake['notes'],
style: TextStyle(
fontSize: isWideScreen ? 13 : 12,
fontStyle: FontStyle.italic,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
],
),
),
IconButton(
icon: Icon(
Icons.delete_outline,
color: Colors.red.shade400,
size: isWideScreen ? 24 : 20,
),
onPressed: () => _deleteIntake(context, intake['id'], intake['supplementName']),
tooltip: 'Delete intake',
),
],
),
),
),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
);
},
),
),
],
),
);
},
);
}
@override
void dispose() {
super.dispose();
}
}