Food: logged entries follow their food; barcodes for My foods

- Saving one of My foods or a meal works every entry logged from it out again
  for that entry's grams, on any day. Only the entries that change are
  written, so the rest aren't synced again (db._follow, app _follow).
- My foods can have a barcode (8-14 digits, one food per barcode). A scanned
  or typed barcode finds My foods first (leading zeros ignored), then the food
  database and kept products; when nothing matches, "New food with barcode"
  opens the food editor with it filled in.
- App 1.1.3 and 1.1.4 changelog entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-26 14:08:32 +02:00
parent fb3c01cdd7
commit ad4b6aa22f
12 changed files with 238 additions and 30 deletions

View file

@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import '../app/sync_service.dart';
import '../app/theme.dart';
import 'food_archive.dart' show scanBarcode;
import 'food_model.dart';
import 'meal_builder.dart';
@ -82,20 +83,21 @@ class MyFoodsPage extends StatelessWidget {
);
}
/// The editor as a bottom sheet; → the saved food's id (null = cancelled / deleted).
Future<String?> openFoodEditor(BuildContext context, SyncService sync, {Map<String, dynamic>? row, String name = ''}) =>
/// The editor as a bottom sheet; → the saved food's id (null = cancelled / deleted). [barcode]: a new food's,
/// from a scan / typed barcode nothing was found for.
Future<String?> openFoodEditor(BuildContext context, SyncService sync, {Map<String, dynamic>? row, String name = '', String barcode = ''}) =>
showModalBottomSheet<String>(
context: context,
isScrollControlled: true,
backgroundColor: card,
builder: (_) => FoodEditor(sync: sync, row: row, name: name),
builder: (_) => FoodEditor(sync: sync, row: row, name: name, barcode: barcode),
);
class FoodEditor extends StatefulWidget {
const FoodEditor({super.key, required this.sync, this.row, this.name = ''});
const FoodEditor({super.key, required this.sync, this.row, this.name = '', this.barcode = ''});
final SyncService sync;
final Map<String, dynamic>? row;
final String name;
final String name, barcode;
@override
State<FoodEditor> createState() => _FoodEditorState();
}
@ -103,6 +105,7 @@ class FoodEditor extends StatefulWidget {
class _FoodEditorState extends State<FoodEditor> {
late final name = TextEditingController(text: widget.row?['name'] as String? ?? widget.name);
late final note = TextEditingController(text: widget.row?['note'] as String? ?? '');
late final barcode = TextEditingController(text: widget.row?['barcode'] as String? ?? widget.barcode);
late String sugarKind = widget.row?['sugar_kind'] as String? ?? ''; // '' = estimated (sugar.dart)
late final values = {
for (final k in foodFields.keys)
@ -127,7 +130,7 @@ class _FoodEditorState extends State<FoodEditor> {
}
final (uid, err) = saveMyFood(widget.sync.store, uid: widget.row?['_u'] as String?, name: name.text,
per100: isMeal ? {for (final k in foodFields.keys) k: (widget.row![k] as num?)?.toDouble()} : {for (final e in values.entries) e.key: _n(e.value)},
portions: ps, note: note.text, sugarKind: sugarKind);
portions: ps, note: note.text, sugarKind: sugarKind, barcode: barcode.text);
if (err != null) return setState(() => error = err);
widget.sync.changed();
Navigator.pop(context, uid);
@ -142,6 +145,23 @@ class _FoodEditorState extends State<FoodEditor> {
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: accent)),
TextField(controller: name, autofocus: widget.row == null && widget.name.isEmpty,
decoration: const InputDecoration(labelText: 'Name')),
if (!isMeal)
TextField(
controller: barcode,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Barcode',
hintText: 'optional — scanning it finds this food',
suffixIcon: IconButton(
tooltip: 'Scan a barcode',
icon: const Icon(Icons.qr_code_scanner),
onPressed: () async {
final code = await scanBarcode(context);
if (code != null && mounted) setState(() => barcode.text = code);
},
),
),
),
const SizedBox(height: 10),
Text(isMeal ? 'Per 100 g — from its ingredients (change them with the meal builder)' : 'Per 100 g (from the label)',
style: const TextStyle(color: muted, fontSize: 13)),