notification overhaul

This commit is contained in:
2025-08-30 00:12:29 +02:00
parent 9ae2bb5654
commit 6dccac6124
25 changed files with 1313 additions and 3947 deletions

View File

@@ -0,0 +1,206 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../models/supplement.dart';
import '../../providers/supplement_provider.dart';
/// Shows a bulk "Take supplements" dialog for a list of supplements.
/// - No time selection (records as now)
/// - Allows editing units per supplement
/// - Optional shared notes (applies to all)
Future<void> showBulkTakeDialog(
BuildContext context,
List<Supplement> supplements,
) async {
if (supplements.isEmpty) {
return;
}
// Controllers for each supplement's units
final Map<int, TextEditingController> unitControllers = {
for (final s in supplements.where((s) => s.id != null))
s.id!: TextEditingController(text: s.numberOfUnits.toString()),
};
final notesController = TextEditingController();
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Take ${supplements.length} supplements'),
content: SizedBox(
width: double.maxFinite,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 500),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// List of supplements with editable units
// Use a scroll view with explicit max height to avoid intrinsic dimension issues.
ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 320),
child: SingleChildScrollView(
child: Column(
children: [
for (int index = 0; index < supplements.length; index++) ...[
() {
final s = supplements[index];
final controller = unitControllers[s.id] ??
TextEditingController(text: s.numberOfUnits.toString());
return Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.5),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withOpacity(0.3),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Name and per-unit info
Row(
children: [
Expanded(
child: Text(
s.name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(width: 8),
Text(
s.unitType,
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
const SizedBox(height: 8),
// Units editor
Row(
children: [
Expanded(
child: TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Units',
border: const OutlineInputBorder(),
suffixText: s.unitType,
),
),
),
],
),
const SizedBox(height: 8),
// Dosage line
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(6),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withOpacity(0.3),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Per unit:',
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
Text(
s.ingredientsDisplay,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurface,
),
),
],
),
),
],
),
);
}(),
if (index != supplements.length - 1) const SizedBox(height: 8),
],
],
),
),
),
const SizedBox(height: 12),
// Shared notes
TextField(
controller: notesController,
decoration: const InputDecoration(
labelText: 'Notes for all (optional)',
border: OutlineInputBorder(),
),
maxLines: 2,
),
],
),
)),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () {
final provider = context.read<SupplementProvider>();
int recorded = 0;
for (final s in supplements) {
if (s.id == null) continue;
final controller = unitControllers[s.id]!;
final units = double.tryParse(controller.text) ?? s.numberOfUnits.toDouble();
// totalDosageTaken stays 0.0 for now (ingredients-based tracking later)
provider.recordIntake(
s.id!,
0.0,
unitsTaken: units,
notes: notesController.text.isNotEmpty ? notesController.text : null,
takenAt: null, // "now"
);
recorded++;
}
Navigator.of(context).pop();
if (recorded > 0) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Recorded $recorded supplement${recorded == 1 ? '' : 's'}'),
backgroundColor: Colors.green,
),
);
}
},
child: const Text('Record All'),
),
],
);
},
);
// Dispose controllers
for (final c in unitControllers.values) {
c.dispose();
}
notesController.dispose();
}

View File

@@ -0,0 +1,277 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../models/supplement.dart';
import '../../providers/supplement_provider.dart';
/// Shows the "Take supplement" dialog.
/// - If [hideTime] is true, the time selection UI is hidden and intake is recorded as "now".
Future<void> showTakeSupplementDialog(
BuildContext context,
Supplement supplement, {
bool hideTime = false,
}) async {
final unitsController = TextEditingController(text: supplement.numberOfUnits.toString());
final notesController = TextEditingController();
DateTime selectedDateTime = DateTime.now();
bool useCustomTime = false;
await showDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text('Take ${supplement.name}'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Expanded(
child: TextField(
controller: unitsController,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
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(
supplement.ingredientsDisplay,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurface,
),
),
],
),
),
const SizedBox(height: 16),
if (!hideTime) ...[
// Time selection section
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer.withOpacity(0.3),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.access_time,
size: 16,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 6),
Text(
'When did you take it?',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.primary,
),
),
],
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: RadioListTile<bool>(
dense: true,
contentPadding: EdgeInsets.zero,
title: const Text('Just now', style: TextStyle(fontSize: 12)),
value: false,
groupValue: useCustomTime,
onChanged: (value) => setState(() => useCustomTime = value!),
),
),
Expanded(
child: RadioListTile<bool>(
dense: true,
contentPadding: EdgeInsets.zero,
title: const Text('Custom time', style: TextStyle(fontSize: 12)),
value: true,
groupValue: useCustomTime,
onChanged: (value) => setState(() => useCustomTime = value!),
),
),
],
),
if (useCustomTime) ...[
const SizedBox(height: 8),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(6),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withOpacity(0.5),
),
),
child: Column(
children: [
// Date picker
Row(
children: [
Icon(
Icons.calendar_today,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Date: ${selectedDateTime.day}/${selectedDateTime.month}/${selectedDateTime.year}',
style: const TextStyle(fontSize: 12),
),
),
TextButton(
onPressed: () async {
final date = await showDatePicker(
context: context,
initialDate: selectedDateTime,
firstDate: DateTime.now().subtract(const Duration(days: 7)),
lastDate: DateTime.now(),
);
if (date != null) {
setState(() {
selectedDateTime = DateTime(
date.year,
date.month,
date.day,
selectedDateTime.hour,
selectedDateTime.minute,
);
});
}
},
child: const Text('Change', style: TextStyle(fontSize: 10)),
),
],
),
const SizedBox(height: 4),
// Time picker
Row(
children: [
Icon(
Icons.access_time,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Time: ${selectedDateTime.hour.toString().padLeft(2, '0')}:${selectedDateTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 12),
),
),
TextButton(
onPressed: () async {
final time = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(selectedDateTime),
);
if (time != null) {
setState(() {
selectedDateTime = DateTime(
selectedDateTime.year,
selectedDateTime.month,
selectedDateTime.day,
time.hour,
time.minute,
);
});
}
},
child: const Text('Change', style: TextStyle(fontSize: 10)),
),
],
),
],
),
),
],
],
),
),
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 = double.tryParse(unitsController.text) ?? supplement.numberOfUnits.toDouble();
// For now, we'll record 0 as total dosage since we're transitioning to ingredients
// This will be properly implemented when we add the full ingredient tracking
final totalDosageTaken = 0.0;
context.read<SupplementProvider>().recordIntake(
supplement.id!,
totalDosageTaken,
unitsTaken: unitsTaken,
notes: notesController.text.isNotEmpty ? notesController.text : null,
takenAt: hideTime
? null
: (useCustomTime ? selectedDateTime : null),
);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${supplement.name} recorded!'),
backgroundColor: Colors.green,
),
);
},
child: const Text('Record'),
),
],
);
},
),
);
}