diet-guard/app/lib/widgets/food_bank/fb_add_entry_dialog.dart
Krzysztof kuhy Rudnicki 7cb09a5b85 Split food_bank_screen; share the filter widgets with history
food_bank_screen.dart (712) carried a near-clone of the filter sheet batch 2
had just factored out of history: the same name-search field, the same slider
captions, and four more copies of the same min/max macro range block. Splitting
it in isolation would have baked that duplication in permanently, so the shared
widgets moved to a neutral `lib/widgets/filters/` first and both screens now
import them.

  food_bank_screen.dart      712 -> 238
  lib/widgets/food_bank/
    fb_filter.dart            156  FbFilter, FbSortField, applyFbFilter
    fb_filter_sheet.dart      244  the sheet itself
    fb_add_entry_dialog.dart  103  the manual-entry dialog
  lib/widgets/history/history_filter_fields.dart
                        -> lib/widgets/filters/filter_fields.dart

FilterRangeRow gained `showEndpointLabels`, defaulting to true. History showed
endpoint captions and a selected-range readout; the food-bank sheet never did,
and switching them on there would have been a UI change smuggled in under a
refactor. The food-bank call sites pass false, so both screens render exactly
what they rendered before.

FbSortField and HistorySortField stay separate on purpose -- they are not the
same enum (`name` vs `date`/`description`), so the sort dropdown is not shared.

The test file mirrors the source split, three files under test/screens/
food_bank/ plus fb_test_support.dart for the setUp/tearDown/settle prologue,
same shape as batch 2's history_test_support.dart.

Verified: 555 flutter tests (unchanged count -- no test lost, and food bank's
own RangeSlider/sort-toggle tests still pass against the shared widget),
flutter analyze clean over lib/ and test/, live ~/.local/share/diet_guard
byte-identical. Over-cap files: 40 -> 38.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
2026-08-15 19:43:08 +02:00

104 lines
2.9 KiB
Dart

/// The "add a manual food bank entry" dialog.
///
/// Split out of `food_bank_screen.dart` for the repo's 250-line cap.
library;
import 'package:diet_guard_app/models/food_bank_record.dart';
import 'package:flutter/material.dart';
/// Prompts for a new manually-curated food bank entry.
///
/// Pops with the new [FoodBankRecord], or null when cancelled.
class AddEntryDialog extends StatefulWidget {
/// Creates an [AddEntryDialog] with empty fields.
const AddEntryDialog({super.key});
@override
State<AddEntryDialog> createState() => AddEntryDialogState();
}
/// State for [AddEntryDialog], owning its text controllers.
class AddEntryDialogState extends State<AddEntryDialog> {
final _name = TextEditingController();
final _grams = TextEditingController(text: '100');
final _kcal = TextEditingController();
final _protein = TextEditingController();
final _carbs = TextEditingController();
final _fat = TextEditingController();
@override
void dispose() {
_name.dispose();
_grams.dispose();
_kcal.dispose();
_protein.dispose();
_carbs.dispose();
_fat.dispose();
super.dispose();
}
void _save() {
final name = _name.text.trim();
if (name.isEmpty) return;
Navigator.of(context).pop(
FoodBankRecord(
desc: name,
kcal: double.tryParse(_kcal.text) ?? 0,
proteinG: double.tryParse(_protein.text) ?? 0,
carbsG: double.tryParse(_carbs.text) ?? 0,
fatG: double.tryParse(_fat.text) ?? 0,
grams: double.tryParse(_grams.text) ?? 100,
count: 0,
),
);
}
Widget _field(String label, TextEditingController ctrl) => Padding(
padding: const EdgeInsets.only(bottom: 8),
child: TextField(
controller: ctrl,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(labelText: label, isDense: true),
),
);
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Add to food bank'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: TextField(
controller: _name,
decoration: const InputDecoration(
labelText: 'Name',
isDense: true,
),
),
),
_field('Reference grams', _grams),
_field('Kcal', _kcal),
_field('Protein (g)', _protein),
_field('Carbs (g)', _carbs),
_field('Fat (g)', _fat),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: _save,
child: const Text('Save to bank'),
),
],
);
}
}