mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
339 lines
12 KiB
Dart
339 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:shadcn_ui/shadcn_ui.dart';
|
|
import 'package:supplements/widgets/info_chip.dart';
|
|
|
|
import '../models/supplement.dart';
|
|
import '../providers/supplement_provider.dart';
|
|
|
|
class ArchivedSupplementsScreen extends StatefulWidget {
|
|
const ArchivedSupplementsScreen({super.key});
|
|
|
|
@override
|
|
State<ArchivedSupplementsScreen> createState() => _ArchivedSupplementsScreenState();
|
|
}
|
|
|
|
class _ArchivedSupplementsScreenState extends State<ArchivedSupplementsScreen> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<SupplementProvider>().loadArchivedSupplements();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Archived Supplements'),
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
),
|
|
body: Consumer<SupplementProvider>(
|
|
builder: (context, provider, child) {
|
|
if (provider.archivedSupplements.isEmpty) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.archive_outlined,
|
|
size: 64,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
'No archived supplements',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Archived supplements will appear here',
|
|
style: TextStyle(
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
return RefreshIndicator(
|
|
onRefresh: () async {
|
|
await provider.loadArchivedSupplements();
|
|
},
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: provider.archivedSupplements.length,
|
|
itemBuilder: (context, index) {
|
|
final supplement = provider.archivedSupplements[index];
|
|
return _ArchivedSupplementCard(
|
|
supplement: supplement,
|
|
onUnarchive: () => _unarchiveSupplement(context, supplement),
|
|
onDelete: () => _deleteSupplement(context, supplement),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
void _unarchiveSupplement(BuildContext context, Supplement supplement) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Unarchive Supplement'),
|
|
content: Text('Are you sure you want to unarchive ${supplement.name}?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
context.read<SupplementProvider>().unarchiveSupplement(supplement.id!);
|
|
Navigator.of(context).pop();
|
|
ShadSonner.of(context).show(
|
|
ShadToast(
|
|
title: Text('${supplement.name} unarchived'),
|
|
),
|
|
);
|
|
},
|
|
child: const Text('Unarchive'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
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 permanently delete ${supplement.name}? This action cannot be undone.',
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
context.read<SupplementProvider>().deleteArchivedSupplement(supplement.id!);
|
|
Navigator.of(context).pop();
|
|
ShadSonner.of(context).show(
|
|
ShadToast(
|
|
title: Text('${supplement.name} deleted permanently'),
|
|
),
|
|
);
|
|
},
|
|
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
|
child: const Text('Delete', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ArchivedSupplementCard extends StatelessWidget {
|
|
final Supplement supplement;
|
|
final VoidCallback onUnarchive;
|
|
final VoidCallback onDelete;
|
|
|
|
const _ArchivedSupplementCard({
|
|
required this.supplement,
|
|
required this.onUnarchive,
|
|
required this.onDelete,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
elevation: 2,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(16),
|
|
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
|
|
border: Border.all(
|
|
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.2),
|
|
width: 1,
|
|
),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.1),
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: Icon(
|
|
Icons.archive,
|
|
color: Theme.of(context).colorScheme.outline,
|
|
size: 20,
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
supplement.name,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
if (supplement.brand != null && supplement.brand!.isNotEmpty)
|
|
Text(
|
|
supplement.brand!,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Theme.of(context).colorScheme.outline,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
PopupMenuButton(
|
|
padding: EdgeInsets.zero,
|
|
icon: Icon(
|
|
Icons.more_vert,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
onSelected: (value) {
|
|
switch (value) {
|
|
case 'unarchive':
|
|
onUnarchive();
|
|
break;
|
|
case 'delete':
|
|
onDelete();
|
|
break;
|
|
}
|
|
},
|
|
itemBuilder: (context) => [
|
|
const PopupMenuItem(
|
|
value: 'unarchive',
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.unarchive, color: Colors.green),
|
|
SizedBox(width: 8),
|
|
Text('Unarchive'),
|
|
],
|
|
),
|
|
),
|
|
const PopupMenuItem(
|
|
value: 'delete',
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.delete_forever, color: Colors.red),
|
|
SizedBox(width: 8),
|
|
Text('Delete Permanently', style: TextStyle(color: Colors.red)),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Supplement details in a muted style
|
|
if (supplement.ingredients.isNotEmpty) ...[
|
|
Container(
|
|
padding: const EdgeInsets.all(12),
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Ingredients per ${supplement.unitType}:',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Wrap(
|
|
spacing: 6,
|
|
runSpacing: 4,
|
|
children: supplement.ingredients.map((ingredient) {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.1),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Text(
|
|
'${ingredient.name} ${ingredient.amount}${ingredient.unit}',
|
|
style: TextStyle(
|
|
fontSize: 10,
|
|
fontWeight: FontWeight.w500,
|
|
color: Theme.of(context).colorScheme.outline,
|
|
),
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
],
|
|
|
|
// Dosage info
|
|
Row(
|
|
children: [
|
|
InfoChip(
|
|
icon: Icons.schedule,
|
|
label: '${supplement.frequencyPerDay}x daily',
|
|
context: context,
|
|
),
|
|
const SizedBox(width: 8),
|
|
InfoChip(
|
|
icon: Icons.medication,
|
|
label: '${supplement.numberOfUnits} ${supplement.unitType}',
|
|
context: context,
|
|
),
|
|
],
|
|
),
|
|
|
|
if (supplement.reminderTimes.isNotEmpty) ...[
|
|
const SizedBox(height: 8),
|
|
InfoChip(
|
|
icon: Icons.notifications_off,
|
|
label: 'Was: ${supplement.reminderTimes.join(', ')}',
|
|
context: context,
|
|
fullWidth: true,
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|