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

@@ -3,6 +3,19 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../shared/models/home_summary.dart';
import 'home_service.dart';
// ── Selected date (persists while app is open) ────────────────
/// The date currently viewed on the home screen.
/// Defaults to today; can be changed via the date selector.
final selectedDateProvider = StateProvider<DateTime>((ref) => DateTime.now());
/// Formats a [DateTime] to the 'YYYY-MM-DD' string expected by the diary API.
String formatDateForDiary(DateTime date) =>
'${date.year}-${date.month.toString().padLeft(2, '0')}-'
'${date.day.toString().padLeft(2, '0')}';
// ── Home summary ──────────────────────────────────────────────
class HomeNotifier extends StateNotifier<AsyncValue<HomeSummary>> {
final HomeService _service;

View File

@@ -6,60 +6,97 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../core/theme/app_colors.dart';
import '../../shared/models/diary_entry.dart';
import '../../shared/models/home_summary.dart';
import '../../shared/models/meal_type.dart';
import '../menu/menu_provider.dart';
import '../profile/profile_provider.dart';
import 'home_provider.dart';
// ── Root screen ───────────────────────────────────────────────
class HomeScreen extends ConsumerWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(homeProvider);
final userName = ref.watch(profileProvider).valueOrNull?.name;
final homeSummaryState = ref.watch(homeProvider);
final profile = ref.watch(profileProvider).valueOrNull;
final userName = profile?.name;
final goalType = profile?.goal;
final dailyGoal = profile?.dailyCalories ?? 0;
final userMealTypes = profile?.mealTypes ?? const ['breakfast', 'lunch', 'dinner'];
final goalType = ref.watch(
profileProvider.select((asyncUser) => asyncUser.valueOrNull?.goal));
final selectedDate = ref.watch(selectedDateProvider);
final dateString = formatDateForDiary(selectedDate);
final diaryState = ref.watch(diaryProvider(dateString));
final entries = diaryState.valueOrNull ?? [];
final loggedCalories = entries.fold<double>(
0.0, (sum, entry) => sum + (entry.calories ?? 0));
final loggedProtein = entries.fold<double>(
0.0, (sum, entry) => sum + (entry.proteinG ?? 0));
final loggedFat = entries.fold<double>(
0.0, (sum, entry) => sum + (entry.fatG ?? 0));
final loggedCarbs = entries.fold<double>(
0.0, (sum, entry) => sum + (entry.carbsG ?? 0));
final expiringSoon = homeSummaryState.valueOrNull?.expiringSoon ?? [];
final recommendations = homeSummaryState.valueOrNull?.recommendations ?? [];
return Scaffold(
body: state.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (_, __) => Center(
child: FilledButton(
onPressed: () => ref.read(homeProvider.notifier).load(),
child: const Text('Повторить'),
),
),
data: (summary) => RefreshIndicator(
onRefresh: () => ref.read(homeProvider.notifier).load(),
child: CustomScrollView(
slivers: [
_AppBar(summary: summary, userName: userName),
SliverPadding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 100),
sliver: SliverList(
delegate: SliverChildListDelegate([
body: RefreshIndicator(
onRefresh: () async {
ref.read(homeProvider.notifier).load();
ref.invalidate(diaryProvider(dateString));
},
child: CustomScrollView(
slivers: [
_AppBar(userName: userName),
SliverPadding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 100),
sliver: SliverList(
delegate: SliverChildListDelegate([
const SizedBox(height: 12),
_DateSelector(
selectedDate: selectedDate,
onDateSelected: (date) =>
ref.read(selectedDateProvider.notifier).state = date,
),
const SizedBox(height: 16),
_CaloriesCard(
loggedCalories: loggedCalories,
dailyGoal: dailyGoal,
goalType: goalType,
),
const SizedBox(height: 12),
_MacrosRow(
proteinG: loggedProtein,
fatG: loggedFat,
carbsG: loggedCarbs,
),
const SizedBox(height: 16),
_DailyMealsSection(
mealTypeIds: userMealTypes,
entries: entries,
dateString: dateString,
),
if (expiringSoon.isNotEmpty) ...[
const SizedBox(height: 16),
_CaloriesCard(today: summary.today, goalType: goalType),
const SizedBox(height: 16),
_TodayMealsCard(plan: summary.today.plan),
if (summary.expiringSoon.isNotEmpty) ...[
const SizedBox(height: 16),
_ExpiringBanner(items: summary.expiringSoon),
],
const SizedBox(height: 16),
_QuickActionsRow(date: summary.today.date),
if (summary.recommendations.isNotEmpty) ...[
const SizedBox(height: 20),
_SectionTitle('Рекомендуем приготовить'),
const SizedBox(height: 12),
_RecommendationsRow(recipes: summary.recommendations),
],
]),
),
_ExpiringBanner(items: expiringSoon),
],
const SizedBox(height: 16),
_QuickActionsRow(date: dateString),
if (recommendations.isNotEmpty) ...[
const SizedBox(height: 20),
_SectionTitle('Рекомендуем приготовить'),
const SizedBox(height: 12),
_RecommendationsRow(recipes: recommendations),
],
]),
),
],
),
),
],
),
),
);
@@ -69,9 +106,8 @@ class HomeScreen extends ConsumerWidget {
// ── App bar ───────────────────────────────────────────────────
class _AppBar extends StatelessWidget {
final HomeSummary summary;
final String? userName;
const _AppBar({required this.summary, this.userName});
const _AppBar({this.userName});
String get _greetingBase {
final hour = DateTime.now().hour;
@@ -86,31 +122,89 @@ class _AppBar extends StatelessWidget {
return _greetingBase;
}
String get _dateLabel {
final now = DateTime.now();
const months = [
'января', 'февраля', 'марта', 'апреля', 'мая', 'июня',
'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря',
];
return '${now.day} ${months[now.month - 1]}';
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return SliverAppBar(
pinned: false,
floating: true,
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(_greeting, style: theme.textTheme.titleMedium),
Text(
_dateLabel,
style: theme.textTheme.bodySmall
?.copyWith(color: theme.colorScheme.onSurfaceVariant),
),
],
title: Text(_greeting, style: theme.textTheme.titleMedium),
);
}
}
// ── Date selector ─────────────────────────────────────────────
class _DateSelector extends StatelessWidget {
final DateTime selectedDate;
final ValueChanged<DateTime> onDateSelected;
const _DateSelector({
required this.selectedDate,
required this.onDateSelected,
});
static const _weekDayShort = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final today = DateTime.now();
final todayNormalized = DateTime(today.year, today.month, today.day);
final selectedNormalized = DateTime(
selectedDate.year, selectedDate.month, selectedDate.day);
return SizedBox(
height: 64,
child: ListView.separated(
scrollDirection: Axis.horizontal,
itemCount: 7,
separatorBuilder: (_, __) => const SizedBox(width: 8),
itemBuilder: (context, index) {
final date = todayNormalized.subtract(Duration(days: 6 - index));
final isSelected = date == selectedNormalized;
final isToday = date == todayNormalized;
return GestureDetector(
onTap: () => onDateSelected(date),
child: AnimatedContainer(
duration: const Duration(milliseconds: 150),
width: 44,
decoration: BoxDecoration(
color: isSelected
? theme.colorScheme.primary
: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(12),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_weekDayShort[date.weekday - 1],
style: theme.textTheme.labelSmall?.copyWith(
color: isSelected
? theme.colorScheme.onPrimary
: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 2),
Text(
'${date.day}',
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight:
isSelected || isToday ? FontWeight.w700 : FontWeight.normal,
color: isSelected
? theme.colorScheme.onPrimary
: isToday
? theme.colorScheme.primary
: null,
),
),
],
),
),
);
},
),
);
}
@@ -119,31 +213,34 @@ class _AppBar extends StatelessWidget {
// ── Calories card ─────────────────────────────────────────────
class _CaloriesCard extends StatelessWidget {
final TodaySummary today;
final double loggedCalories;
final int dailyGoal;
final String? goalType;
const _CaloriesCard({required this.today, required this.goalType});
const _CaloriesCard({
required this.loggedCalories,
required this.dailyGoal,
required this.goalType,
});
@override
Widget build(BuildContext context) {
if (today.dailyGoal == 0) return const SizedBox.shrink();
if (dailyGoal == 0) return const SizedBox.shrink();
final theme = Theme.of(context);
final logged = today.loggedCalories.toInt();
final goal = today.dailyGoal;
final rawProgress =
goal > 0 ? today.loggedCalories / goal : 0.0;
final logged = loggedCalories.toInt();
final rawProgress = dailyGoal > 0 ? loggedCalories / dailyGoal : 0.0;
final isOverGoal = rawProgress > 1.0;
final ringColor = _ringColorFor(rawProgress, goalType);
final String secondaryLabel;
final Color secondaryColor;
if (isOverGoal) {
final overBy = (today.loggedCalories - goal).toInt();
final overBy = (loggedCalories - dailyGoal).toInt();
secondaryLabel = '+$overBy перебор';
secondaryColor = AppColors.error;
} else {
final remaining = today.remainingCalories.toInt();
final remaining = (dailyGoal - loggedCalories).toInt();
secondaryLabel = 'осталось $remaining';
secondaryColor = AppColors.textSecondary;
}
@@ -184,7 +281,7 @@ class _CaloriesCard extends StatelessWidget {
),
const SizedBox(height: 4),
Text(
'цель: $goal',
'цель: $dailyGoal',
style: theme.textTheme.labelSmall?.copyWith(
color: AppColors.textSecondary,
),
@@ -213,7 +310,7 @@ class _CaloriesCard extends StatelessWidget {
const SizedBox(height: 12),
_CalorieStat(
label: 'Цель',
value: '$goal ккал',
value: '$dailyGoal ккал',
valueColor: AppColors.textPrimary,
),
],
@@ -245,8 +342,8 @@ class _CalorieStat extends StatelessWidget {
children: [
Text(
label,
style: theme.textTheme.labelSmall
?.copyWith(color: AppColors.textSecondary),
style:
theme.textTheme.labelSmall?.copyWith(color: AppColors.textSecondary),
),
const SizedBox(height: 2),
Text(
@@ -261,21 +358,104 @@ class _CalorieStat extends StatelessWidget {
}
}
// ── Macros row ────────────────────────────────────────────────
class _MacrosRow extends StatelessWidget {
final double proteinG;
final double fatG;
final double carbsG;
const _MacrosRow({
required this.proteinG,
required this.fatG,
required this.carbsG,
});
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: _MacroChip(
label: 'Белки',
value: '${proteinG.toStringAsFixed(1)} г',
color: Colors.blue,
),
),
const SizedBox(width: 8),
Expanded(
child: _MacroChip(
label: 'Жиры',
value: '${fatG.toStringAsFixed(1)} г',
color: Colors.orange,
),
),
const SizedBox(width: 8),
Expanded(
child: _MacroChip(
label: 'Углеводы',
value: '${carbsG.toStringAsFixed(1)} г',
color: Colors.green,
),
),
],
);
}
}
class _MacroChip extends StatelessWidget {
final String label;
final String value;
final Color color;
const _MacroChip({
required this.label,
required this.value,
required this.color,
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Container(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(10),
),
child: Column(
children: [
Text(
value,
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w700,
color: color,
),
),
const SizedBox(height: 2),
Text(
label,
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
);
}
}
// ── Ring colour logic ──────────────────────────────────────────
// Returns the ring stroke colour based on rawProgress and goal type.
// See docs/calorie_ring_color_spec.md for full specification.
Color _ringColorFor(double rawProgress, String? goalType) {
switch (goalType) {
case 'lose':
// Ceiling semantics: over goal is bad
if (rawProgress >= 1.10) return AppColors.error;
if (rawProgress >= 0.95) return AppColors.warning;
if (rawProgress >= 0.75) return AppColors.success;
return AppColors.primary;
case 'maintain':
// Bidirectional target: closeness in either direction is good
if (rawProgress >= 1.25) return AppColors.error;
if (rawProgress >= 1.11) return AppColors.warning;
if (rawProgress >= 0.90) return AppColors.success;
@@ -283,7 +463,6 @@ Color _ringColorFor(double rawProgress, String? goalType) {
return AppColors.primary;
case 'gain':
// Floor semantics: under goal is bad, over is neutral
if (rawProgress < 0.60) return AppColors.error;
if (rawProgress < 0.85) return AppColors.warning;
if (rawProgress <= 1.15) return AppColors.success;
@@ -308,13 +487,11 @@ class _CalorieRingPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = (size.width - 20) / 2; // 10 px inset on each side
final radius = (size.width - 20) / 2;
const strokeWidth = 10.0;
const overflowStrokeWidth = 6.0;
// Arc starts at 12 o'clock (−π/2) and goes clockwise
const startAngle = -math.pi / 2;
// Background track — full circle
final trackPaint = Paint()
..color = AppColors.separator.withValues(alpha: 0.5)
..style = PaintingStyle.stroke
@@ -322,7 +499,6 @@ class _CalorieRingPainter extends CustomPainter {
..strokeCap = StrokeCap.round;
canvas.drawCircle(center, radius, trackPaint);
// Primary arc — clamp sweep to max one full circle
final clampedProgress = rawProgress.clamp(0.0, 1.0);
if (clampedProgress > 0) {
final arcPaint = Paint()
@@ -339,7 +515,6 @@ class _CalorieRingPainter extends CustomPainter {
);
}
// Overflow arc — second lap when rawProgress > 1.0
if (rawProgress > 1.0) {
final overflowProgress = rawProgress - 1.0;
final overflowPaint = Paint()
@@ -363,72 +538,158 @@ class _CalorieRingPainter extends CustomPainter {
oldDelegate.ringColor != ringColor;
}
// ── Today meals card ──────────────────────────────────────────
// ── Daily meals section ───────────────────────────────────────
class _TodayMealsCard extends StatelessWidget {
final List<TodayMealPlan> plan;
const _TodayMealsCard({required this.plan});
class _DailyMealsSection extends ConsumerWidget {
final List<String> mealTypeIds;
final List<DiaryEntry> entries;
final String dateString;
const _DailyMealsSection({
required this.mealTypeIds,
required this.entries,
required this.dateString,
});
@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text('Приёмы пищи сегодня',
style: theme.textTheme.titleSmall),
),
Card(
child: Column(
children: plan.asMap().entries.map((entry) {
final i = entry.key;
final meal = entry.value;
return Column(
children: [
_MealRow(meal: meal),
if (i < plan.length - 1)
const Divider(height: 1, indent: 16),
],
);
}).toList(),
),
),
Text('Приёмы пищи', style: theme.textTheme.titleSmall),
const SizedBox(height: 8),
...mealTypeIds.map((mealTypeId) {
final mealTypeOption = mealTypeById(mealTypeId);
if (mealTypeOption == null) return const SizedBox.shrink();
final mealEntries = entries
.where((entry) => entry.mealType == mealTypeId)
.toList();
return Padding(
padding: const EdgeInsets.only(bottom: 8),
child: _MealCard(
mealTypeOption: mealTypeOption,
entries: mealEntries,
dateString: dateString,
),
);
}),
],
);
}
}
class _MealRow extends StatelessWidget {
final TodayMealPlan meal;
const _MealRow({required this.meal});
class _MealCard extends ConsumerWidget {
final MealTypeOption mealTypeOption;
final List<DiaryEntry> entries;
final String dateString;
const _MealCard({
required this.mealTypeOption,
required this.entries,
required this.dateString,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
final totalCalories = entries.fold<double>(
0.0, (sum, entry) => sum + (entry.calories ?? 0));
return Card(
child: Column(
children: [
// Header row
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 8, 8),
child: Row(
children: [
Text(mealTypeOption.emoji,
style: const TextStyle(fontSize: 20)),
const SizedBox(width: 8),
Text(mealTypeOption.label,
style: theme.textTheme.titleSmall),
const Spacer(),
if (totalCalories > 0)
Text(
'${totalCalories.toInt()} ккал',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
IconButton(
icon: const Icon(Icons.add, size: 20),
visualDensity: VisualDensity.compact,
tooltip: 'Добавить блюдо',
onPressed: () =>
context.push('/scan', extra: mealTypeOption.id),
),
],
),
),
// Diary entries
if (entries.isNotEmpty) ...[
const Divider(height: 1, indent: 16),
...entries.map((entry) => _DiaryEntryTile(
entry: entry,
onDelete: () => ref
.read(diaryProvider(dateString).notifier)
.remove(entry.id),
)),
],
],
),
);
}
}
class _DiaryEntryTile extends StatelessWidget {
final DiaryEntry entry;
final VoidCallback onDelete;
const _DiaryEntryTile({required this.entry, required this.onDelete});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final calories = entry.calories?.toInt();
final hasProtein = (entry.proteinG ?? 0) > 0;
final hasFat = (entry.fatG ?? 0) > 0;
final hasCarbs = (entry.carbsG ?? 0) > 0;
return ListTile(
leading: Text(meal.mealEmoji, style: const TextStyle(fontSize: 24)),
title: Text(meal.mealLabel, style: theme.textTheme.labelMedium),
subtitle: meal.hasRecipe
? Text(meal.recipeTitle!, style: theme.textTheme.bodyMedium)
: Text(
'Не запланировано',
style: theme.textTheme.bodyMedium?.copyWith(
dense: true,
title: Text(entry.name, style: theme.textTheme.bodyMedium),
subtitle: (hasProtein || hasFat || hasCarbs)
? Text(
[
if (hasProtein) 'Б ${entry.proteinG!.toStringAsFixed(1)}',
if (hasFat) 'Ж ${entry.fatG!.toStringAsFixed(1)}',
if (hasCarbs) 'У ${entry.carbsG!.toStringAsFixed(1)}',
].join(' '),
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
)
: null,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (calories != null)
Text(
'$calories ккал',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
fontStyle: FontStyle.italic,
),
),
trailing: meal.calories != null
? Text(
'${meal.calories!.toInt()} ккал',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant),
)
: const Icon(Icons.chevron_right),
onTap: () => context.push('/menu'),
IconButton(
icon: Icon(Icons.delete_outline,
size: 18, color: theme.colorScheme.error),
visualDensity: VisualDensity.compact,
onPressed: onDelete,
),
],
),
);
}
}
@@ -463,15 +724,17 @@ class _ExpiringBanner extends StatelessWidget {
children: [
Text(
'Истекает срок годности',
style: theme.textTheme.labelMedium
?.copyWith(color: onColor, fontWeight: FontWeight.w600),
style: theme.textTheme.labelMedium?.copyWith(
color: onColor, fontWeight: FontWeight.w600),
),
Text(
items
.take(3)
.map((e) => '${e.name}${e.expiryLabel}')
.map((expiringSoonItem) =>
'${expiringSoonItem.name}${expiringSoonItem.expiryLabel}')
.join(', '),
style: theme.textTheme.bodySmall?.copyWith(color: onColor),
style:
theme.textTheme.bodySmall?.copyWith(color: onColor),
),
],
),
@@ -598,7 +861,7 @@ class _RecipeCard extends StatelessWidget {
child: Card(
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: () {}, // recipes detail can be added later
onTap: () {},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [