feat: meal tracking, dish recognition UX improvements, English AI prompts

Backend:
- Translate all recognition prompts (receipt, products, dish) from Russian to English
- Add lang parameter to Recognizer interface and pass locale.FromContext in handlers
- DishResult type uses candidates array for multi-candidate responses

Client:
- Add meal tracking: diary provider, date selector, meal type model
- DishResult parser: backward-compatible with legacy flat format and new candidates format
- DishResultScreen: sticky bottom button, full-width portion/meal-type inputs,
  КБЖУ disclaimer moved under nutrition card, add date field to diary POST body
- Recognition prompts now return dish/product names in user's preferred language
- Onboarding, profile, home screen visual updates

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-17 14:29:36 +02:00
parent 2a95bcd53c
commit 87ef2097fc
16 changed files with 1269 additions and 350 deletions

View File

@@ -1,134 +1,396 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../features/menu/menu_provider.dart';
import '../../features/home/home_provider.dart';
import '../../shared/models/meal_type.dart';
import 'recognition_service.dart';
/// Shows the nutritional breakdown of a recognized dish.
class DishResultScreen extends StatelessWidget {
const DishResultScreen({super.key, required this.dish});
/// Shows the recognition candidates and lets the user confirm a dish entry
/// before adding it to the diary.
class DishResultScreen extends ConsumerStatefulWidget {
const DishResultScreen({
super.key,
required this.dish,
this.preselectedMealType,
});
final DishResult dish;
final String? preselectedMealType;
@override
ConsumerState<DishResultScreen> createState() => _DishResultScreenState();
}
class _DishResultScreenState extends ConsumerState<DishResultScreen> {
late int _selectedIndex;
late int _portionGrams;
late String _mealType;
bool _saving = false;
final TextEditingController _portionController = TextEditingController();
@override
void initState() {
super.initState();
_selectedIndex = 0;
_portionGrams = widget.dish.candidates.isNotEmpty
? widget.dish.candidates.first.weightGrams
: 300;
_mealType = widget.preselectedMealType ??
kAllMealTypes.first.id;
_portionController.text = '$_portionGrams';
}
@override
void dispose() {
_portionController.dispose();
super.dispose();
}
DishCandidate get _selected => widget.dish.candidates[_selectedIndex];
/// Scales nutrition linearly to the current portion weight.
double _scale(double baseValue) {
final baseWeight = _selected.weightGrams;
if (baseWeight <= 0) return baseValue;
return baseValue * _portionGrams / baseWeight;
}
void _selectCandidate(int index) {
setState(() {
_selectedIndex = index;
_portionGrams = widget.dish.candidates[index].weightGrams;
_portionController.text = '$_portionGrams';
});
}
void _adjustPortion(int delta) {
final newValue = (_portionGrams + delta).clamp(10, 9999);
setState(() {
_portionGrams = newValue;
_portionController.text = '$newValue';
});
}
void _onPortionEdited(String value) {
final parsed = int.tryParse(value);
if (parsed != null && parsed >= 10) {
setState(() => _portionGrams = parsed.clamp(10, 9999));
}
}
Future<void> _addToDiary() async {
if (_saving) return;
setState(() => _saving = true);
final selectedDate = ref.read(selectedDateProvider);
final dateString = formatDateForDiary(selectedDate);
final scaledCalories = _scale(_selected.calories);
final scaledProtein = _scale(_selected.proteinG);
final scaledFat = _scale(_selected.fatG);
final scaledCarbs = _scale(_selected.carbsG);
try {
await ref.read(diaryProvider(dateString).notifier).add({
'date': dateString,
'meal_type': _mealType,
'name': _selected.dishName,
'calories': scaledCalories,
'protein_g': scaledProtein,
'fat_g': scaledFat,
'carbs_g': scaledCarbs,
'portion_g': _portionGrams,
'source': 'recognition',
});
if (mounted) context.go('/home');
} catch (addError) {
debugPrint('Add to diary error: $addError');
if (mounted) {
setState(() => _saving = false);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Не удалось добавить. Попробуйте ещё раз.')),
);
}
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final confPct = (dish.confidence * 100).toInt();
final hasCandidates = widget.dish.candidates.isNotEmpty;
return Scaffold(
appBar: AppBar(title: const Text('Распознано блюдо')),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
// Dish name + confidence
Text(
dish.dishName,
style: theme.textTheme.headlineSmall
?.copyWith(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Row(
children: [
Icon(
Icons.info_outline,
size: 14,
color: theme.colorScheme.onSurfaceVariant,
),
const SizedBox(width: 4),
Text(
'Уверенность: $confPct%',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
bottomNavigationBar: hasCandidates
? SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 8, 20, 16),
child: FilledButton(
onPressed: _saving ? null : _addToDiary,
child: _saving
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Добавить в журнал'),
),
),
],
),
const SizedBox(height: 24),
// Nutrition card
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
'${dish.calories.toInt()} ккал',
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: theme.colorScheme.primary,
),
),
const Spacer(),
Tooltip(
message: 'Приблизительные значения на основе фото',
child: Text(
'',
style: theme.textTheme.titleLarge?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
],
)
: null,
body: hasCandidates
? ListView(
padding: const EdgeInsets.all(20),
children: [
_CandidatesSection(
candidates: widget.dish.candidates,
selectedIndex: _selectedIndex,
onSelect: _selectCandidate,
),
const SizedBox(height: 20),
_NutritionCard(
calories: _scale(_selected.calories),
proteinG: _scale(_selected.proteinG),
fatG: _scale(_selected.fatG),
carbsG: _scale(_selected.carbsG),
),
const SizedBox(height: 8),
Text(
'КБЖУ приблизительные — определены по фото.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_MacroChip(
label: 'Белки',
value: '${dish.proteinG.toStringAsFixed(1)} г',
color: Colors.blue,
),
_MacroChip(
label: 'Жиры',
value: '${dish.fatG.toStringAsFixed(1)} г',
color: Colors.orange,
),
_MacroChip(
label: 'Углеводы',
value: '${dish.carbsG.toStringAsFixed(1)} г',
color: Colors.green,
),
],
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
_PortionRow(
controller: _portionController,
onMinus: () => _adjustPortion(-10),
onPlus: () => _adjustPortion(10),
onChanged: _onPortionEdited,
),
const SizedBox(height: 20),
_MealTypeDropdown(
selected: _mealType,
onChanged: (value) {
if (value != null) setState(() => _mealType = value);
},
),
const SizedBox(height: 16),
],
)
: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Блюдо не распознано',
style: theme.textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
'Вес порции: ~${dish.weightGrams} г',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
FilledButton(
onPressed: () => context.pop(),
child: const Text('Попробовать снова'),
),
],
),
),
),
);
}
}
// Similar dishes
if (dish.similarDishes.isNotEmpty) ...[
const SizedBox(height: 20),
Text('Похожие блюда', style: theme.textTheme.titleSmall),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 4,
children: dish.similarDishes
.map((name) => Chip(label: Text(name)))
.toList(),
// ---------------------------------------------------------------------------
// Candidates selector
// ---------------------------------------------------------------------------
class _CandidatesSection extends StatelessWidget {
const _CandidatesSection({
required this.candidates,
required this.selectedIndex,
required this.onSelect,
});
final List<DishCandidate> candidates;
final int selectedIndex;
final ValueChanged<int> onSelect;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Выберите блюдо', style: theme.textTheme.titleMedium),
const SizedBox(height: 8),
...candidates.asMap().entries.map((entry) {
final index = entry.key;
final candidate = entry.value;
final confPct = (candidate.confidence * 100).toInt();
final isSelected = index == selectedIndex;
return Padding(
padding: const EdgeInsets.only(bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(12),
onTap: () => onSelect(index),
child: AnimatedContainer(
duration: const Duration(milliseconds: 150),
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isSelected
? theme.colorScheme.primary
: theme.colorScheme.outlineVariant,
width: isSelected ? 2 : 1,
),
color: isSelected
? theme.colorScheme.primaryContainer
.withValues(alpha: 0.3)
: null,
),
child: Row(
children: [
Icon(
isSelected
? Icons.radio_button_checked
: Icons.radio_button_unchecked,
color: isSelected
? theme.colorScheme.primary
: theme.colorScheme.onSurfaceVariant,
),
const SizedBox(width: 8),
Expanded(
child: Text(
candidate.dishName,
style: theme.textTheme.bodyLarge?.copyWith(
fontWeight: isSelected
? FontWeight.w600
: FontWeight.normal,
),
),
),
_ConfidenceBadge(confidence: confPct),
],
),
),
),
);
}),
],
);
}
}
class _ConfidenceBadge extends StatelessWidget {
const _ConfidenceBadge({required this.confidence});
final int confidence;
@override
Widget build(BuildContext context) {
final Color badgeColor;
if (confidence >= 80) {
badgeColor = Colors.green;
} else if (confidence >= 50) {
badgeColor = Colors.orange;
} else {
badgeColor = Colors.red;
}
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: badgeColor.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'$confidence%',
style: TextStyle(
color: badgeColor,
fontWeight: FontWeight.bold,
fontSize: 12,
),
),
);
}
}
// ---------------------------------------------------------------------------
// Nutrition card
// ---------------------------------------------------------------------------
class _NutritionCard extends StatelessWidget {
const _NutritionCard({
required this.calories,
required this.proteinG,
required this.fatG,
required this.carbsG,
});
final double calories;
final double proteinG;
final double fatG;
final double carbsG;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
'${calories.toInt()} ккал',
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: theme.colorScheme.primary,
),
),
const Spacer(),
Tooltip(
message: 'Приблизительные значения на основе фото',
child: Text(
'',
style: theme.textTheme.titleLarge?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
],
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_MacroChip(
label: 'Белки',
value: '${proteinG.toStringAsFixed(1)} г',
color: Colors.blue,
),
_MacroChip(
label: 'Жиры',
value: '${fatG.toStringAsFixed(1)} г',
color: Colors.orange,
),
_MacroChip(
label: 'Углеводы',
value: '${carbsG.toStringAsFixed(1)} г',
color: Colors.green,
),
],
),
],
const SizedBox(height: 32),
const Divider(),
const SizedBox(height: 8),
Text(
'КБЖУ приблизительные — определены по фото.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
],
),
),
);
}
@@ -157,9 +419,103 @@ class _MacroChip extends StatelessWidget {
color: color,
),
),
Text(
label,
style: Theme.of(context).textTheme.labelSmall,
Text(label, style: Theme.of(context).textTheme.labelSmall),
],
);
}
}
// ---------------------------------------------------------------------------
// Portion row
// ---------------------------------------------------------------------------
class _PortionRow extends StatelessWidget {
const _PortionRow({
required this.controller,
required this.onMinus,
required this.onPlus,
required this.onChanged,
});
final TextEditingController controller;
final VoidCallback onMinus;
final VoidCallback onPlus;
final ValueChanged<String> onChanged;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Порция', style: theme.textTheme.titleSmall),
const SizedBox(height: 8),
Row(
children: [
IconButton.outlined(
icon: const Icon(Icons.remove),
onPressed: onMinus,
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: controller,
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
onChanged: onChanged,
decoration: const InputDecoration(
suffixText: 'г',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 8),
IconButton.outlined(
icon: const Icon(Icons.add),
onPressed: onPlus,
),
],
),
],
);
}
}
// ---------------------------------------------------------------------------
// Meal type dropdown
// ---------------------------------------------------------------------------
class _MealTypeDropdown extends StatelessWidget {
const _MealTypeDropdown({
required this.selected,
required this.onChanged,
});
final String selected;
final ValueChanged<String?> onChanged;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Приём пищи', style: theme.textTheme.titleSmall),
const SizedBox(height: 8),
DropdownButtonFormField<String>(
initialValue: selected,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
items: kAllMealTypes
.map((mealTypeOption) => DropdownMenuItem(
value: mealTypeOption.id,
child: Text(
'${mealTypeOption.emoji} ${mealTypeOption.label}'),
))
.toList(),
onChanged: onChanged,
),
],
);

View File

@@ -61,7 +61,8 @@ class ReceiptResult {
const ReceiptResult({required this.items, required this.unrecognized});
}
class DishResult {
/// A single dish recognition candidate with estimated nutrition for the portion in the photo.
class DishCandidate {
final String dishName;
final int weightGrams;
final double calories;
@@ -69,9 +70,8 @@ class DishResult {
final double fatG;
final double carbsG;
final double confidence;
final List<String> similarDishes;
const DishResult({
const DishCandidate({
required this.dishName,
required this.weightGrams,
required this.calories,
@@ -79,11 +79,10 @@ class DishResult {
required this.fatG,
required this.carbsG,
required this.confidence,
required this.similarDishes,
});
factory DishResult.fromJson(Map<String, dynamic> json) {
return DishResult(
factory DishCandidate.fromJson(Map<String, dynamic> json) {
return DishCandidate(
dishName: json['dish_name'] as String? ?? '',
weightGrams: json['weight_grams'] as int? ?? 0,
calories: (json['calories'] as num?)?.toDouble() ?? 0,
@@ -91,14 +90,46 @@ class DishResult {
fatG: (json['fat_g'] as num?)?.toDouble() ?? 0,
carbsG: (json['carbs_g'] as num?)?.toDouble() ?? 0,
confidence: (json['confidence'] as num?)?.toDouble() ?? 0,
similarDishes: (json['similar_dishes'] as List<dynamic>?)
?.map((e) => e as String)
.toList() ??
[],
);
}
}
/// Result of dish recognition: ordered list of candidates (best match first).
class DishResult {
final List<DishCandidate> candidates;
const DishResult({required this.candidates});
/// The best matching candidate.
DishCandidate get best => candidates.first;
// Convenience getters delegating to the best candidate.
String get dishName => best.dishName;
int get weightGrams => best.weightGrams;
double get calories => best.calories;
double get proteinG => best.proteinG;
double get fatG => best.fatG;
double get carbsG => best.carbsG;
double get confidence => best.confidence;
factory DishResult.fromJson(Map<String, dynamic> json) {
// New format: {"candidates": [...]}
if (json['candidates'] is List) {
final candidatesList = (json['candidates'] as List<dynamic>)
.map((element) => DishCandidate.fromJson(element as Map<String, dynamic>))
.toList();
return DishResult(candidates: candidatesList);
}
// Legacy flat format: {"dish_name": "...", "calories": ..., ...}
if (json['dish_name'] != null) {
return DishResult(candidates: [DishCandidate.fromJson(json)]);
}
return const DishResult(candidates: []);
}
}
// ---------------------------------------------------------------------------
// Service
// ---------------------------------------------------------------------------

View File

@@ -12,11 +12,36 @@ final _recognitionServiceProvider = Provider<RecognitionService>((ref) {
});
/// Entry screen — lets the user choose how to add products.
class ScanScreen extends ConsumerWidget {
/// If [GoRouterState.extra] is a non-null String, it is treated as a meal type ID
/// and the screen immediately opens the camera for dish recognition.
class ScanScreen extends ConsumerStatefulWidget {
const ScanScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
ConsumerState<ScanScreen> createState() => _ScanScreenState();
}
class _ScanScreenState extends ConsumerState<ScanScreen> {
bool _autoStarted = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_autoStarted) return;
final mealType = GoRouterState.of(context).extra as String?;
if (mealType != null && mealType.isNotEmpty) {
_autoStarted = true;
// Defer to avoid calling context navigation during build.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
_pickAndRecognize(context, _Mode.dish, mealType: mealType);
}
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Добавить продукты')),
body: ListView(
@@ -33,21 +58,21 @@ class ScanScreen extends ConsumerWidget {
emoji: '🧾',
title: 'Сфотографировать чек',
subtitle: 'Распознаем все продукты из чека',
onTap: () => _pickAndRecognize(context, ref, _Mode.receipt),
onTap: () => _pickAndRecognize(context, _Mode.receipt),
),
const SizedBox(height: 16),
_ModeCard(
emoji: '🥦',
title: 'Сфотографировать продукты',
subtitle: 'Холодильник, стол, полка — до 3 фото',
onTap: () => _pickAndRecognize(context, ref, _Mode.products),
onTap: () => _pickAndRecognize(context, _Mode.products),
),
const SizedBox(height: 16),
_ModeCard(
emoji: '🍽️',
title: 'Определить блюдо',
subtitle: 'КБЖУ≈ по фото готового блюда',
onTap: () => _pickAndRecognize(context, ref, _Mode.dish),
onTap: () => _pickAndRecognize(context, _Mode.dish),
),
const SizedBox(height: 16),
_ModeCard(
@@ -63,9 +88,9 @@ class ScanScreen extends ConsumerWidget {
Future<void> _pickAndRecognize(
BuildContext context,
WidgetRef ref,
_Mode mode,
) async {
_Mode mode, {
String? mealType,
}) async {
final picker = ImagePicker();
List<XFile> files = [];
@@ -120,11 +145,11 @@ class ScanScreen extends ConsumerWidget {
final dish = await service.recognizeDish(files.first);
if (context.mounted) {
Navigator.pop(context);
context.push('/scan/dish', extra: dish);
context.push('/scan/dish', extra: {'dish': dish, 'meal_type': mealType});
}
}
} catch (e, s) {
debugPrint('Recognition error: $e\n$s');
} catch (recognitionError) {
debugPrint('Recognition error: $recognitionError');
if (context.mounted) {
Navigator.pop(context); // close loading
ScaffoldMessenger.of(context).showSnackBar(