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:
@@ -94,8 +94,8 @@ type ReceiptResult struct {
|
|||||||
Unrecognized []UnrecognizedItem `json:"unrecognized"`
|
Unrecognized []UnrecognizedItem `json:"unrecognized"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DishResult is the result of dish recognition.
|
// DishCandidate is a single dish recognition candidate with estimated nutrition.
|
||||||
type DishResult struct {
|
type DishCandidate struct {
|
||||||
DishName string `json:"dish_name"`
|
DishName string `json:"dish_name"`
|
||||||
WeightGrams int `json:"weight_grams"`
|
WeightGrams int `json:"weight_grams"`
|
||||||
Calories float64 `json:"calories"`
|
Calories float64 `json:"calories"`
|
||||||
@@ -103,7 +103,11 @@ type DishResult struct {
|
|||||||
FatG float64 `json:"fat_g"`
|
FatG float64 `json:"fat_g"`
|
||||||
CarbsG float64 `json:"carbs_g"`
|
CarbsG float64 `json:"carbs_g"`
|
||||||
Confidence float64 `json:"confidence"`
|
Confidence float64 `json:"confidence"`
|
||||||
SimilarDishes []string `json:"similar_dishes"`
|
}
|
||||||
|
|
||||||
|
// DishResult is the result of dish recognition with multiple ranked candidates.
|
||||||
|
type DishResult struct {
|
||||||
|
Candidates []DishCandidate `json:"candidates"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IngredientTranslation holds the localized name and aliases for one language.
|
// IngredientTranslation holds the localized name and aliases for one language.
|
||||||
|
|||||||
@@ -9,30 +9,43 @@ import (
|
|||||||
"github.com/food-ai/backend/internal/adapters/ai"
|
"github.com/food-ai/backend/internal/adapters/ai"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RecognizeReceipt uses the vision model to extract food items from a receipt photo.
|
// langNames maps ISO 639-1 codes to English language names used in AI prompts.
|
||||||
func (c *Client) RecognizeReceipt(ctx context.Context, imageBase64, mimeType string) (*ai.ReceiptResult, error) {
|
var langNames = map[string]string{
|
||||||
prompt := `Ты — OCR-система для чеков из продуктовых магазинов.
|
"en": "English", "ru": "Russian", "es": "Spanish",
|
||||||
|
"de": "German", "fr": "French", "it": "Italian",
|
||||||
|
"pt": "Portuguese", "zh": "Chinese", "ja": "Japanese",
|
||||||
|
"ko": "Korean", "ar": "Arabic", "hi": "Hindi",
|
||||||
|
}
|
||||||
|
|
||||||
Проанализируй фото чека и извлеки список продуктов питания.
|
// RecognizeReceipt uses the vision model to extract food items from a receipt photo.
|
||||||
Для каждого продукта определи:
|
func (c *Client) RecognizeReceipt(ctx context.Context, imageBase64, mimeType, lang string) (*ai.ReceiptResult, error) {
|
||||||
- name: название на русском языке (убери артикулы, коды, лишние символы)
|
langName := langNames[lang]
|
||||||
- quantity: количество (число)
|
if langName == "" {
|
||||||
- unit: единица (г, кг, мл, л, шт, уп)
|
langName = "English"
|
||||||
|
}
|
||||||
|
prompt := fmt.Sprintf(`You are an OCR system for grocery receipts.
|
||||||
|
|
||||||
|
Analyse the receipt photo and extract a list of food products.
|
||||||
|
For each product determine:
|
||||||
|
- name: product name (remove article codes, extra symbols)
|
||||||
|
- quantity: amount (number)
|
||||||
|
- unit: unit (g, kg, ml, l, pcs, pack)
|
||||||
- category: dairy | meat | produce | bakery | frozen | beverages | other
|
- category: dairy | meat | produce | bakery | frozen | beverages | other
|
||||||
- confidence: 0.0–1.0
|
- confidence: 0.0–1.0
|
||||||
|
|
||||||
Позиции, которые не являются едой (бытовая химия, табак, алкоголь) — пропусти.
|
Skip items that are not food (household chemicals, tobacco, alcohol).
|
||||||
Позиции с нечитаемым текстом — добавь в unrecognized.
|
Items with unreadable text — add to unrecognized.
|
||||||
|
|
||||||
Верни ТОЛЬКО валидный JSON без markdown:
|
Return all text fields (name) in %s.
|
||||||
|
Return ONLY valid JSON without markdown:
|
||||||
{
|
{
|
||||||
"items": [
|
"items": [
|
||||||
{"name": "Молоко 2.5%", "quantity": 1, "unit": "л", "category": "dairy", "confidence": 0.95}
|
{"name": "...", "quantity": 1, "unit": "l", "category": "dairy", "confidence": 0.95}
|
||||||
],
|
],
|
||||||
"unrecognized": [
|
"unrecognized": [
|
||||||
{"raw_text": "ТОВ АРТИК 1ШТ", "price": 89.0}
|
{"raw_text": "...", "price": 89.0}
|
||||||
]
|
]
|
||||||
}`
|
}`, langName)
|
||||||
|
|
||||||
text, err := c.generateVisionContent(ctx, prompt, imageBase64, mimeType)
|
text, err := c.generateVisionContent(ctx, prompt, imageBase64, mimeType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -53,25 +66,30 @@ func (c *Client) RecognizeReceipt(ctx context.Context, imageBase64, mimeType str
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RecognizeProducts uses the vision model to identify food items in a photo (fridge, shelf, etc.).
|
// RecognizeProducts uses the vision model to identify food items in a photo (fridge, shelf, etc.).
|
||||||
func (c *Client) RecognizeProducts(ctx context.Context, imageBase64, mimeType string) ([]ai.RecognizedItem, error) {
|
func (c *Client) RecognizeProducts(ctx context.Context, imageBase64, mimeType, lang string) ([]ai.RecognizedItem, error) {
|
||||||
prompt := `Ты — система распознавания продуктов питания.
|
langName := langNames[lang]
|
||||||
|
if langName == "" {
|
||||||
|
langName = "English"
|
||||||
|
}
|
||||||
|
prompt := fmt.Sprintf(`You are a food product recognition system.
|
||||||
|
|
||||||
Посмотри на фото и определи все видимые продукты питания.
|
Look at the photo and identify all visible food products.
|
||||||
Для каждого продукта оцени:
|
For each product estimate:
|
||||||
- name: название на русском языке
|
- name: product name
|
||||||
- quantity: приблизительное количество (число)
|
- quantity: approximate amount (number)
|
||||||
- unit: единица (г, кг, мл, л, шт)
|
- unit: unit (g, kg, ml, l, pcs)
|
||||||
- category: dairy | meat | produce | bakery | frozen | beverages | other
|
- category: dairy | meat | produce | bakery | frozen | beverages | other
|
||||||
- confidence: 0.0–1.0
|
- confidence: 0.0–1.0
|
||||||
|
|
||||||
Только продукты питания. Пустые упаковки и несъедобные предметы — пропусти.
|
Food products only. Skip empty packaging and inedible objects.
|
||||||
|
|
||||||
Верни ТОЛЬКО валидный JSON без markdown:
|
Return all text fields (name) in %s.
|
||||||
|
Return ONLY valid JSON without markdown:
|
||||||
{
|
{
|
||||||
"items": [
|
"items": [
|
||||||
{"name": "Яйца", "quantity": 10, "unit": "шт", "category": "dairy", "confidence": 0.9}
|
{"name": "...", "quantity": 10, "unit": "pcs", "category": "dairy", "confidence": 0.9}
|
||||||
]
|
]
|
||||||
}`
|
}`, langName)
|
||||||
|
|
||||||
text, err := c.generateVisionContent(ctx, prompt, imageBase64, mimeType)
|
text, err := c.generateVisionContent(ctx, prompt, imageBase64, mimeType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -91,28 +109,49 @@ func (c *Client) RecognizeProducts(ctx context.Context, imageBase64, mimeType st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RecognizeDish uses the vision model to identify a dish and estimate its nutritional content.
|
// RecognizeDish uses the vision model to identify a dish and estimate its nutritional content.
|
||||||
func (c *Client) RecognizeDish(ctx context.Context, imageBase64, mimeType string) (*ai.DishResult, error) {
|
// Returns 3–5 ranked candidates so the user can correct mis-identifications.
|
||||||
prompt := `Ты — диетолог и кулинарный эксперт.
|
func (c *Client) RecognizeDish(ctx context.Context, imageBase64, mimeType, lang string) (*ai.DishResult, error) {
|
||||||
|
langName := langNames[lang]
|
||||||
|
if langName == "" {
|
||||||
|
langName = "English"
|
||||||
|
}
|
||||||
|
prompt := fmt.Sprintf(`You are a dietitian and culinary expert.
|
||||||
|
|
||||||
Посмотри на фото блюда и определи:
|
Look at the dish photo and suggest 3 to 5 possible dishes it could be.
|
||||||
- dish_name: название блюда на русском языке
|
Even if the first option is obvious, add 2–4 alternative dishes with lower confidence.
|
||||||
- weight_grams: приблизительный вес порции в граммах
|
For each candidate specify:
|
||||||
- calories: калорийность порции (приблизительно)
|
- dish_name: dish name
|
||||||
- protein_g, fat_g, carbs_g: БЖУ на порцию
|
- weight_grams: approximate portion weight in grams (estimate from photo)
|
||||||
- confidence: 0.0–1.0
|
- calories: calories for this portion (kcal)
|
||||||
- similar_dishes: до 3 похожих блюд (для поиска рецептов)
|
- protein_g, fat_g, carbs_g: macros for this portion (grams)
|
||||||
|
- confidence: certainty 0.0–1.0
|
||||||
|
|
||||||
Верни ТОЛЬКО валидный JSON без markdown:
|
Sort candidates by descending confidence. First — most likely.
|
||||||
|
|
||||||
|
Return dish_name values in %s.
|
||||||
|
Return ONLY valid JSON without markdown:
|
||||||
{
|
{
|
||||||
"dish_name": "Паста Карбонара",
|
"candidates": [
|
||||||
|
{
|
||||||
|
"dish_name": "...",
|
||||||
"weight_grams": 350,
|
"weight_grams": 350,
|
||||||
"calories": 520,
|
"calories": 520,
|
||||||
"protein_g": 22,
|
"protein_g": 22,
|
||||||
"fat_g": 26,
|
"fat_g": 26,
|
||||||
"carbs_g": 48,
|
"carbs_g": 48,
|
||||||
"confidence": 0.85,
|
"confidence": 0.88
|
||||||
"similar_dishes": ["Паста с беконом", "Спагетти"]
|
},
|
||||||
}`
|
{
|
||||||
|
"dish_name": "...",
|
||||||
|
"weight_grams": 350,
|
||||||
|
"calories": 540,
|
||||||
|
"protein_g": 20,
|
||||||
|
"fat_g": 28,
|
||||||
|
"carbs_g": 49,
|
||||||
|
"confidence": 0.65
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`, langName)
|
||||||
|
|
||||||
text, err := c.generateVisionContent(ctx, prompt, imageBase64, mimeType)
|
text, err := c.generateVisionContent(ctx, prompt, imageBase64, mimeType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -120,11 +159,11 @@ func (c *Client) RecognizeDish(ctx context.Context, imageBase64, mimeType string
|
|||||||
}
|
}
|
||||||
|
|
||||||
var result ai.DishResult
|
var result ai.DishResult
|
||||||
if err := parseJSON(text, &result); err != nil {
|
if parseError := parseJSON(text, &result); parseError != nil {
|
||||||
return nil, fmt.Errorf("parse dish result: %w", err)
|
return nil, fmt.Errorf("parse dish result: %w", parseError)
|
||||||
}
|
}
|
||||||
if result.SimilarDishes == nil {
|
if result.Candidates == nil {
|
||||||
result.SimilarDishes = []string{}
|
result.Candidates = []ai.DishCandidate{}
|
||||||
}
|
}
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/food-ai/backend/internal/adapters/ai"
|
"github.com/food-ai/backend/internal/adapters/ai"
|
||||||
|
"github.com/food-ai/backend/internal/infra/locale"
|
||||||
"github.com/food-ai/backend/internal/infra/middleware"
|
"github.com/food-ai/backend/internal/infra/middleware"
|
||||||
"github.com/food-ai/backend/internal/domain/ingredient"
|
"github.com/food-ai/backend/internal/domain/ingredient"
|
||||||
)
|
)
|
||||||
@@ -23,9 +24,9 @@ type IngredientRepository interface {
|
|||||||
|
|
||||||
// Recognizer is the AI provider interface for image-based food recognition.
|
// Recognizer is the AI provider interface for image-based food recognition.
|
||||||
type Recognizer interface {
|
type Recognizer interface {
|
||||||
RecognizeReceipt(ctx context.Context, imageBase64, mimeType string) (*ai.ReceiptResult, error)
|
RecognizeReceipt(ctx context.Context, imageBase64, mimeType, lang string) (*ai.ReceiptResult, error)
|
||||||
RecognizeProducts(ctx context.Context, imageBase64, mimeType string) ([]ai.RecognizedItem, error)
|
RecognizeProducts(ctx context.Context, imageBase64, mimeType, lang string) ([]ai.RecognizedItem, error)
|
||||||
RecognizeDish(ctx context.Context, imageBase64, mimeType string) (*ai.DishResult, error)
|
RecognizeDish(ctx context.Context, imageBase64, mimeType, lang string) (*ai.DishResult, error)
|
||||||
ClassifyIngredient(ctx context.Context, name string) (*ai.IngredientClassification, error)
|
ClassifyIngredient(ctx context.Context, name string) (*ai.IngredientClassification, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +92,8 @@ func (h *Handler) RecognizeReceipt(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := h.recognizer.RecognizeReceipt(r.Context(), req.ImageBase64, req.MimeType)
|
lang := locale.FromContext(r.Context())
|
||||||
|
result, err := h.recognizer.RecognizeReceipt(r.Context(), req.ImageBase64, req.MimeType, lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("recognize receipt", "err", err)
|
slog.Error("recognize receipt", "err", err)
|
||||||
writeErrorJSON(w, http.StatusServiceUnavailable, "recognition failed, please try again")
|
writeErrorJSON(w, http.StatusServiceUnavailable, "recognition failed, please try again")
|
||||||
@@ -118,13 +120,14 @@ func (h *Handler) RecognizeProducts(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process each image in parallel.
|
// Process each image in parallel.
|
||||||
|
lang := locale.FromContext(r.Context())
|
||||||
allItems := make([][]ai.RecognizedItem, len(req.Images))
|
allItems := make([][]ai.RecognizedItem, len(req.Images))
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i, img := range req.Images {
|
for i, img := range req.Images {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(i int, img imageRequest) {
|
go func(i int, img imageRequest) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
items, err := h.recognizer.RecognizeProducts(r.Context(), img.ImageBase64, img.MimeType)
|
items, err := h.recognizer.RecognizeProducts(r.Context(), img.ImageBase64, img.MimeType, lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("recognize products from image", "index", i, "err", err)
|
slog.Warn("recognize products from image", "index", i, "err", err)
|
||||||
return
|
return
|
||||||
@@ -148,7 +151,8 @@ func (h *Handler) RecognizeDish(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := h.recognizer.RecognizeDish(r.Context(), req.ImageBase64, req.MimeType)
|
lang := locale.FromContext(r.Context())
|
||||||
|
result, err := h.recognizer.RecognizeDish(r.Context(), req.ImageBase64, req.MimeType, lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("recognize dish", "err", err)
|
slog.Error("recognize dish", "err", err)
|
||||||
writeErrorJSON(w, http.StatusServiceUnavailable, "recognition failed, please try again")
|
writeErrorJSON(w, http.StatusServiceUnavailable, "recognition failed, please try again")
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ func buildUpdateQuery(id string, req UpdateProfileRequest) (string, []interface{
|
|||||||
argIdx++
|
argIdx++
|
||||||
}
|
}
|
||||||
if req.Preferences != nil {
|
if req.Preferences != nil {
|
||||||
setClauses = append(setClauses, fmt.Sprintf("preferences = $%d", argIdx))
|
setClauses = append(setClauses, fmt.Sprintf("preferences = preferences || $%d::jsonb", argIdx))
|
||||||
args = append(args, string(*req.Preferences))
|
args = append(args, string(*req.Preferences))
|
||||||
argIdx++
|
argIdx++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,10 +69,15 @@ final routerProvider = Provider<GoRouter>((ref) {
|
|||||||
}
|
}
|
||||||
final profileUser = profileState.valueOrNull;
|
final profileUser = profileState.valueOrNull;
|
||||||
// If profile failed to load, don't block navigation.
|
// If profile failed to load, don't block navigation.
|
||||||
if (profileUser == null) return isAuthRoute ? '/home' : null;
|
if (profileUser == null) {
|
||||||
|
if (isAuthRoute || state.matchedLocation == '/loading') return '/home';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
final needsOnboarding = !profileUser.hasCompletedOnboarding;
|
final needsOnboarding = !profileUser.hasCompletedOnboarding;
|
||||||
if (isAuthRoute) return needsOnboarding ? '/onboarding' : '/home';
|
if (isAuthRoute || state.matchedLocation == '/loading') {
|
||||||
|
return needsOnboarding ? '/onboarding' : '/home';
|
||||||
|
}
|
||||||
if (needsOnboarding && !isOnboarding) return '/onboarding';
|
if (needsOnboarding && !isOnboarding) return '/onboarding';
|
||||||
if (!needsOnboarding && isOnboarding) return '/home';
|
if (!needsOnboarding && isOnboarding) return '/home';
|
||||||
}
|
}
|
||||||
@@ -148,9 +153,11 @@ final routerProvider = Provider<GoRouter>((ref) {
|
|||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/scan/dish',
|
path: '/scan/dish',
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
final dish = state.extra as DishResult?;
|
final extra = state.extra as Map<String, dynamic>?;
|
||||||
|
final dish = extra?['dish'] as DishResult?;
|
||||||
|
final mealType = extra?['meal_type'] as String?;
|
||||||
if (dish == null) return const _InvalidRoute();
|
if (dish == null) return const _InvalidRoute();
|
||||||
return DishResultScreen(dish: dish);
|
return DishResultScreen(dish: dish, preselectedMealType: mealType);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
ShellRoute(
|
ShellRoute(
|
||||||
|
|||||||
@@ -3,6 +3,19 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|||||||
import '../../shared/models/home_summary.dart';
|
import '../../shared/models/home_summary.dart';
|
||||||
import 'home_service.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>> {
|
class HomeNotifier extends StateNotifier<AsyncValue<HomeSummary>> {
|
||||||
final HomeService _service;
|
final HomeService _service;
|
||||||
|
|
||||||
|
|||||||
@@ -6,54 +6,92 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
import '../../core/theme/app_colors.dart';
|
import '../../core/theme/app_colors.dart';
|
||||||
|
import '../../shared/models/diary_entry.dart';
|
||||||
import '../../shared/models/home_summary.dart';
|
import '../../shared/models/home_summary.dart';
|
||||||
|
import '../../shared/models/meal_type.dart';
|
||||||
|
import '../menu/menu_provider.dart';
|
||||||
import '../profile/profile_provider.dart';
|
import '../profile/profile_provider.dart';
|
||||||
import 'home_provider.dart';
|
import 'home_provider.dart';
|
||||||
|
|
||||||
|
// ── Root screen ───────────────────────────────────────────────
|
||||||
|
|
||||||
class HomeScreen extends ConsumerWidget {
|
class HomeScreen extends ConsumerWidget {
|
||||||
const HomeScreen({super.key});
|
const HomeScreen({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final state = ref.watch(homeProvider);
|
final homeSummaryState = ref.watch(homeProvider);
|
||||||
final userName = ref.watch(profileProvider).valueOrNull?.name;
|
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(
|
final selectedDate = ref.watch(selectedDateProvider);
|
||||||
profileProvider.select((asyncUser) => asyncUser.valueOrNull?.goal));
|
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(
|
return Scaffold(
|
||||||
body: state.when(
|
body: RefreshIndicator(
|
||||||
loading: () => const Center(child: CircularProgressIndicator()),
|
onRefresh: () async {
|
||||||
error: (_, __) => Center(
|
ref.read(homeProvider.notifier).load();
|
||||||
child: FilledButton(
|
ref.invalidate(diaryProvider(dateString));
|
||||||
onPressed: () => ref.read(homeProvider.notifier).load(),
|
},
|
||||||
child: const Text('Повторить'),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
data: (summary) => RefreshIndicator(
|
|
||||||
onRefresh: () => ref.read(homeProvider.notifier).load(),
|
|
||||||
child: CustomScrollView(
|
child: CustomScrollView(
|
||||||
slivers: [
|
slivers: [
|
||||||
_AppBar(summary: summary, userName: userName),
|
_AppBar(userName: userName),
|
||||||
SliverPadding(
|
SliverPadding(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 100),
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 100),
|
||||||
sliver: SliverList(
|
sliver: SliverList(
|
||||||
delegate: SliverChildListDelegate([
|
delegate: SliverChildListDelegate([
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
_DateSelector(
|
||||||
|
selectedDate: selectedDate,
|
||||||
|
onDateSelected: (date) =>
|
||||||
|
ref.read(selectedDateProvider.notifier).state = date,
|
||||||
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_CaloriesCard(today: summary.today, goalType: goalType),
|
_CaloriesCard(
|
||||||
|
loggedCalories: loggedCalories,
|
||||||
|
dailyGoal: dailyGoal,
|
||||||
|
goalType: goalType,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
_MacrosRow(
|
||||||
|
proteinG: loggedProtein,
|
||||||
|
fatG: loggedFat,
|
||||||
|
carbsG: loggedCarbs,
|
||||||
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_TodayMealsCard(plan: summary.today.plan),
|
_DailyMealsSection(
|
||||||
if (summary.expiringSoon.isNotEmpty) ...[
|
mealTypeIds: userMealTypes,
|
||||||
|
entries: entries,
|
||||||
|
dateString: dateString,
|
||||||
|
),
|
||||||
|
if (expiringSoon.isNotEmpty) ...[
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_ExpiringBanner(items: summary.expiringSoon),
|
_ExpiringBanner(items: expiringSoon),
|
||||||
],
|
],
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_QuickActionsRow(date: summary.today.date),
|
_QuickActionsRow(date: dateString),
|
||||||
if (summary.recommendations.isNotEmpty) ...[
|
if (recommendations.isNotEmpty) ...[
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
_SectionTitle('Рекомендуем приготовить'),
|
_SectionTitle('Рекомендуем приготовить'),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
_RecommendationsRow(recipes: summary.recommendations),
|
_RecommendationsRow(recipes: recommendations),
|
||||||
],
|
],
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
@@ -61,7 +99,6 @@ class HomeScreen extends ConsumerWidget {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,9 +106,8 @@ class HomeScreen extends ConsumerWidget {
|
|||||||
// ── App bar ───────────────────────────────────────────────────
|
// ── App bar ───────────────────────────────────────────────────
|
||||||
|
|
||||||
class _AppBar extends StatelessWidget {
|
class _AppBar extends StatelessWidget {
|
||||||
final HomeSummary summary;
|
|
||||||
final String? userName;
|
final String? userName;
|
||||||
const _AppBar({required this.summary, this.userName});
|
const _AppBar({this.userName});
|
||||||
|
|
||||||
String get _greetingBase {
|
String get _greetingBase {
|
||||||
final hour = DateTime.now().hour;
|
final hour = DateTime.now().hour;
|
||||||
@@ -86,32 +122,90 @@ class _AppBar extends StatelessWidget {
|
|||||||
return _greetingBase;
|
return _greetingBase;
|
||||||
}
|
}
|
||||||
|
|
||||||
String get _dateLabel {
|
|
||||||
final now = DateTime.now();
|
|
||||||
const months = [
|
|
||||||
'января', 'февраля', 'марта', 'апреля', 'мая', 'июня',
|
|
||||||
'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря',
|
|
||||||
];
|
|
||||||
return '${now.day} ${months[now.month - 1]}';
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
return SliverAppBar(
|
return SliverAppBar(
|
||||||
pinned: false,
|
pinned: false,
|
||||||
floating: true,
|
floating: true,
|
||||||
title: Column(
|
title: Text(_greeting, style: theme.textTheme.titleMedium),
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── 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: [
|
children: [
|
||||||
Text(_greeting, style: theme.textTheme.titleMedium),
|
|
||||||
Text(
|
Text(
|
||||||
_dateLabel,
|
_weekDayShort[date.weekday - 1],
|
||||||
style: theme.textTheme.bodySmall
|
style: theme.textTheme.labelSmall?.copyWith(
|
||||||
?.copyWith(color: theme.colorScheme.onSurfaceVariant),
|
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 ─────────────────────────────────────────────
|
// ── Calories card ─────────────────────────────────────────────
|
||||||
|
|
||||||
class _CaloriesCard extends StatelessWidget {
|
class _CaloriesCard extends StatelessWidget {
|
||||||
final TodaySummary today;
|
final double loggedCalories;
|
||||||
|
final int dailyGoal;
|
||||||
final String? goalType;
|
final String? goalType;
|
||||||
|
|
||||||
const _CaloriesCard({required this.today, required this.goalType});
|
const _CaloriesCard({
|
||||||
|
required this.loggedCalories,
|
||||||
|
required this.dailyGoal,
|
||||||
|
required this.goalType,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
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 theme = Theme.of(context);
|
||||||
final logged = today.loggedCalories.toInt();
|
final logged = loggedCalories.toInt();
|
||||||
final goal = today.dailyGoal;
|
final rawProgress = dailyGoal > 0 ? loggedCalories / dailyGoal : 0.0;
|
||||||
final rawProgress =
|
|
||||||
goal > 0 ? today.loggedCalories / goal : 0.0;
|
|
||||||
final isOverGoal = rawProgress > 1.0;
|
final isOverGoal = rawProgress > 1.0;
|
||||||
final ringColor = _ringColorFor(rawProgress, goalType);
|
final ringColor = _ringColorFor(rawProgress, goalType);
|
||||||
|
|
||||||
final String secondaryLabel;
|
final String secondaryLabel;
|
||||||
final Color secondaryColor;
|
final Color secondaryColor;
|
||||||
if (isOverGoal) {
|
if (isOverGoal) {
|
||||||
final overBy = (today.loggedCalories - goal).toInt();
|
final overBy = (loggedCalories - dailyGoal).toInt();
|
||||||
secondaryLabel = '+$overBy перебор';
|
secondaryLabel = '+$overBy перебор';
|
||||||
secondaryColor = AppColors.error;
|
secondaryColor = AppColors.error;
|
||||||
} else {
|
} else {
|
||||||
final remaining = today.remainingCalories.toInt();
|
final remaining = (dailyGoal - loggedCalories).toInt();
|
||||||
secondaryLabel = 'осталось $remaining';
|
secondaryLabel = 'осталось $remaining';
|
||||||
secondaryColor = AppColors.textSecondary;
|
secondaryColor = AppColors.textSecondary;
|
||||||
}
|
}
|
||||||
@@ -184,7 +281,7 @@ class _CaloriesCard extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 4),
|
||||||
Text(
|
Text(
|
||||||
'цель: $goal',
|
'цель: $dailyGoal',
|
||||||
style: theme.textTheme.labelSmall?.copyWith(
|
style: theme.textTheme.labelSmall?.copyWith(
|
||||||
color: AppColors.textSecondary,
|
color: AppColors.textSecondary,
|
||||||
),
|
),
|
||||||
@@ -213,7 +310,7 @@ class _CaloriesCard extends StatelessWidget {
|
|||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
_CalorieStat(
|
_CalorieStat(
|
||||||
label: 'Цель',
|
label: 'Цель',
|
||||||
value: '$goal ккал',
|
value: '$dailyGoal ккал',
|
||||||
valueColor: AppColors.textPrimary,
|
valueColor: AppColors.textPrimary,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -245,8 +342,8 @@ class _CalorieStat extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
label,
|
label,
|
||||||
style: theme.textTheme.labelSmall
|
style:
|
||||||
?.copyWith(color: AppColors.textSecondary),
|
theme.textTheme.labelSmall?.copyWith(color: AppColors.textSecondary),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 2),
|
const SizedBox(height: 2),
|
||||||
Text(
|
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 ──────────────────────────────────────────
|
// ── 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) {
|
Color _ringColorFor(double rawProgress, String? goalType) {
|
||||||
switch (goalType) {
|
switch (goalType) {
|
||||||
case 'lose':
|
case 'lose':
|
||||||
// Ceiling semantics: over goal is bad
|
|
||||||
if (rawProgress >= 1.10) return AppColors.error;
|
if (rawProgress >= 1.10) return AppColors.error;
|
||||||
if (rawProgress >= 0.95) return AppColors.warning;
|
if (rawProgress >= 0.95) return AppColors.warning;
|
||||||
if (rawProgress >= 0.75) return AppColors.success;
|
if (rawProgress >= 0.75) return AppColors.success;
|
||||||
return AppColors.primary;
|
return AppColors.primary;
|
||||||
|
|
||||||
case 'maintain':
|
case 'maintain':
|
||||||
// Bidirectional target: closeness in either direction is good
|
|
||||||
if (rawProgress >= 1.25) return AppColors.error;
|
if (rawProgress >= 1.25) return AppColors.error;
|
||||||
if (rawProgress >= 1.11) return AppColors.warning;
|
if (rawProgress >= 1.11) return AppColors.warning;
|
||||||
if (rawProgress >= 0.90) return AppColors.success;
|
if (rawProgress >= 0.90) return AppColors.success;
|
||||||
@@ -283,7 +463,6 @@ Color _ringColorFor(double rawProgress, String? goalType) {
|
|||||||
return AppColors.primary;
|
return AppColors.primary;
|
||||||
|
|
||||||
case 'gain':
|
case 'gain':
|
||||||
// Floor semantics: under goal is bad, over is neutral
|
|
||||||
if (rawProgress < 0.60) return AppColors.error;
|
if (rawProgress < 0.60) return AppColors.error;
|
||||||
if (rawProgress < 0.85) return AppColors.warning;
|
if (rawProgress < 0.85) return AppColors.warning;
|
||||||
if (rawProgress <= 1.15) return AppColors.success;
|
if (rawProgress <= 1.15) return AppColors.success;
|
||||||
@@ -308,13 +487,11 @@ class _CalorieRingPainter extends CustomPainter {
|
|||||||
@override
|
@override
|
||||||
void paint(Canvas canvas, Size size) {
|
void paint(Canvas canvas, Size size) {
|
||||||
final center = Offset(size.width / 2, size.height / 2);
|
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 strokeWidth = 10.0;
|
||||||
const overflowStrokeWidth = 6.0;
|
const overflowStrokeWidth = 6.0;
|
||||||
// Arc starts at 12 o'clock (−π/2) and goes clockwise
|
|
||||||
const startAngle = -math.pi / 2;
|
const startAngle = -math.pi / 2;
|
||||||
|
|
||||||
// Background track — full circle
|
|
||||||
final trackPaint = Paint()
|
final trackPaint = Paint()
|
||||||
..color = AppColors.separator.withValues(alpha: 0.5)
|
..color = AppColors.separator.withValues(alpha: 0.5)
|
||||||
..style = PaintingStyle.stroke
|
..style = PaintingStyle.stroke
|
||||||
@@ -322,7 +499,6 @@ class _CalorieRingPainter extends CustomPainter {
|
|||||||
..strokeCap = StrokeCap.round;
|
..strokeCap = StrokeCap.round;
|
||||||
canvas.drawCircle(center, radius, trackPaint);
|
canvas.drawCircle(center, radius, trackPaint);
|
||||||
|
|
||||||
// Primary arc — clamp sweep to max one full circle
|
|
||||||
final clampedProgress = rawProgress.clamp(0.0, 1.0);
|
final clampedProgress = rawProgress.clamp(0.0, 1.0);
|
||||||
if (clampedProgress > 0) {
|
if (clampedProgress > 0) {
|
||||||
final arcPaint = Paint()
|
final arcPaint = Paint()
|
||||||
@@ -339,7 +515,6 @@ class _CalorieRingPainter extends CustomPainter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overflow arc — second lap when rawProgress > 1.0
|
|
||||||
if (rawProgress > 1.0) {
|
if (rawProgress > 1.0) {
|
||||||
final overflowProgress = rawProgress - 1.0;
|
final overflowProgress = rawProgress - 1.0;
|
||||||
final overflowPaint = Paint()
|
final overflowPaint = Paint()
|
||||||
@@ -363,72 +538,158 @@ class _CalorieRingPainter extends CustomPainter {
|
|||||||
oldDelegate.ringColor != ringColor;
|
oldDelegate.ringColor != ringColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Today meals card ──────────────────────────────────────────
|
// ── Daily meals section ───────────────────────────────────────
|
||||||
|
|
||||||
class _TodayMealsCard extends StatelessWidget {
|
class _DailyMealsSection extends ConsumerWidget {
|
||||||
final List<TodayMealPlan> plan;
|
final List<String> mealTypeIds;
|
||||||
const _TodayMealsCard({required this.plan});
|
final List<DiaryEntry> entries;
|
||||||
|
final String dateString;
|
||||||
|
|
||||||
|
const _DailyMealsSection({
|
||||||
|
required this.mealTypeIds,
|
||||||
|
required this.entries,
|
||||||
|
required this.dateString,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Padding(
|
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),
|
padding: const EdgeInsets.only(bottom: 8),
|
||||||
child: Text('Приёмы пищи сегодня',
|
child: _MealCard(
|
||||||
style: theme.textTheme.titleSmall),
|
mealTypeOption: mealTypeOption,
|
||||||
|
entries: mealEntries,
|
||||||
|
dateString: dateString,
|
||||||
),
|
),
|
||||||
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(),
|
}),
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MealRow extends StatelessWidget {
|
class _MealCard extends ConsumerWidget {
|
||||||
final TodayMealPlan meal;
|
final MealTypeOption mealTypeOption;
|
||||||
const _MealRow({required this.meal});
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(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(
|
return ListTile(
|
||||||
leading: Text(meal.mealEmoji, style: const TextStyle(fontSize: 24)),
|
dense: true,
|
||||||
title: Text(meal.mealLabel, style: theme.textTheme.labelMedium),
|
title: Text(entry.name, style: theme.textTheme.bodyMedium),
|
||||||
subtitle: meal.hasRecipe
|
subtitle: (hasProtein || hasFat || hasCarbs)
|
||||||
? Text(meal.recipeTitle!, style: theme.textTheme.bodyMedium)
|
|
||||||
: Text(
|
|
||||||
'Не запланировано',
|
|
||||||
style: theme.textTheme.bodyMedium?.copyWith(
|
|
||||||
color: theme.colorScheme.onSurfaceVariant,
|
|
||||||
fontStyle: FontStyle.italic,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
trailing: meal.calories != null
|
|
||||||
? Text(
|
? Text(
|
||||||
'≈${meal.calories!.toInt()} ккал',
|
[
|
||||||
|
if (hasProtein) 'Б ${entry.proteinG!.toStringAsFixed(1)}',
|
||||||
|
if (hasFat) 'Ж ${entry.fatG!.toStringAsFixed(1)}',
|
||||||
|
if (hasCarbs) 'У ${entry.carbsG!.toStringAsFixed(1)}',
|
||||||
|
].join(' '),
|
||||||
style: theme.textTheme.bodySmall?.copyWith(
|
style: theme.textTheme.bodySmall?.copyWith(
|
||||||
color: theme.colorScheme.onSurfaceVariant),
|
color: theme.colorScheme.onSurfaceVariant,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
: const Icon(Icons.chevron_right),
|
: null,
|
||||||
onTap: () => context.push('/menu'),
|
trailing: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
if (calories != null)
|
||||||
|
Text(
|
||||||
|
'$calories ккал',
|
||||||
|
style: theme.textTheme.bodySmall?.copyWith(
|
||||||
|
color: theme.colorScheme.onSurfaceVariant,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
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: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
'Истекает срок годности',
|
'Истекает срок годности',
|
||||||
style: theme.textTheme.labelMedium
|
style: theme.textTheme.labelMedium?.copyWith(
|
||||||
?.copyWith(color: onColor, fontWeight: FontWeight.w600),
|
color: onColor, fontWeight: FontWeight.w600),
|
||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
items
|
items
|
||||||
.take(3)
|
.take(3)
|
||||||
.map((e) => '${e.name} — ${e.expiryLabel}')
|
.map((expiringSoonItem) =>
|
||||||
|
'${expiringSoonItem.name} — ${expiringSoonItem.expiryLabel}')
|
||||||
.join(', '),
|
.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(
|
child: Card(
|
||||||
clipBehavior: Clip.antiAlias,
|
clipBehavior: Clip.antiAlias,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () {}, // recipes detail can be added later
|
onTap: () {},
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
import '../../core/theme/app_colors.dart';
|
import '../../core/theme/app_colors.dart';
|
||||||
|
import '../../shared/models/meal_type.dart';
|
||||||
import '../profile/profile_provider.dart';
|
import '../profile/profile_provider.dart';
|
||||||
import '../profile/profile_service.dart';
|
import '../profile/profile_service.dart';
|
||||||
|
|
||||||
const int _totalSteps = 6;
|
const int _totalSteps = 7;
|
||||||
|
|
||||||
const List<Color> _stepAccentColors = [
|
const List<Color> _stepAccentColors = [
|
||||||
Color(0xFF5856D6), // Goal — iOS purple
|
Color(0xFF5856D6), // Goal — iOS purple
|
||||||
@@ -15,6 +16,7 @@ const List<Color> _stepAccentColors = [
|
|||||||
Color(0xFFFF9500), // DOB — iOS orange
|
Color(0xFFFF9500), // DOB — iOS orange
|
||||||
Color(0xFF34C759), // Height + Weight — iOS green
|
Color(0xFF34C759), // Height + Weight — iOS green
|
||||||
Color(0xFFFF2D55), // Activity — iOS pink
|
Color(0xFFFF2D55), // Activity — iOS pink
|
||||||
|
Color(0xFF30B0C7), // Meal Types — teal
|
||||||
Color(0xFFFF6B00), // Calories — deep orange
|
Color(0xFFFF6B00), // Calories — deep orange
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -24,6 +26,7 @@ const List<IconData> _stepIcons = [
|
|||||||
Icons.cake_outlined,
|
Icons.cake_outlined,
|
||||||
Icons.monitor_weight_outlined,
|
Icons.monitor_weight_outlined,
|
||||||
Icons.directions_run,
|
Icons.directions_run,
|
||||||
|
Icons.restaurant_menu,
|
||||||
Icons.local_fire_department,
|
Icons.local_fire_department,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -45,6 +48,7 @@ class _OnboardingScreenState extends ConsumerState<OnboardingScreen> {
|
|||||||
final _heightController = TextEditingController();
|
final _heightController = TextEditingController();
|
||||||
final _weightController = TextEditingController();
|
final _weightController = TextEditingController();
|
||||||
String? _activity;
|
String? _activity;
|
||||||
|
List<String> _mealTypes = List<String>.from(kDefaultMealTypeIds);
|
||||||
int _dailyCalories = 2000;
|
int _dailyCalories = 2000;
|
||||||
|
|
||||||
bool _saving = false;
|
bool _saving = false;
|
||||||
@@ -116,7 +120,9 @@ class _OnboardingScreenState extends ConsumerState<OnboardingScreen> {
|
|||||||
weightValue <= 300;
|
weightValue <= 300;
|
||||||
case 4: // Activity
|
case 4: // Activity
|
||||||
return _activity != null;
|
return _activity != null;
|
||||||
case 5: // Calories
|
case 5: // Meal Types — at least one must be selected
|
||||||
|
return _mealTypes.isNotEmpty;
|
||||||
|
case 6: // Calories
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@@ -127,7 +133,7 @@ class _OnboardingScreenState extends ConsumerState<OnboardingScreen> {
|
|||||||
if (!_canAdvance()) return;
|
if (!_canAdvance()) return;
|
||||||
|
|
||||||
// Entering calorie review — pre-calculate based on collected data
|
// Entering calorie review — pre-calculate based on collected data
|
||||||
if (_currentStep == 4) {
|
if (_currentStep == 5) {
|
||||||
setState(() => _dailyCalories = _calculateCalories());
|
setState(() => _dailyCalories = _calculateCalories());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +172,7 @@ class _OnboardingScreenState extends ConsumerState<OnboardingScreen> {
|
|||||||
gender: _gender,
|
gender: _gender,
|
||||||
goal: _goal,
|
goal: _goal,
|
||||||
activity: _activity,
|
activity: _activity,
|
||||||
|
mealTypes: _mealTypes,
|
||||||
dailyCalories: _dailyCalories,
|
dailyCalories: _dailyCalories,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -257,9 +264,19 @@ class _OnboardingScreenState extends ConsumerState<OnboardingScreen> {
|
|||||||
_StepPage(
|
_StepPage(
|
||||||
accentColor: _stepAccentColors[5],
|
accentColor: _stepAccentColors[5],
|
||||||
icon: _stepIcons[5],
|
icon: _stepIcons[5],
|
||||||
|
child: _MealTypesStepContent(
|
||||||
|
selected: _mealTypes,
|
||||||
|
accentColor: _stepAccentColors[5],
|
||||||
|
onChanged: (updated) =>
|
||||||
|
setState(() => _mealTypes = updated),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
_StepPage(
|
||||||
|
accentColor: _stepAccentColors[6],
|
||||||
|
icon: _stepIcons[6],
|
||||||
child: _CalorieStepContent(
|
child: _CalorieStepContent(
|
||||||
calories: _dailyCalories,
|
calories: _dailyCalories,
|
||||||
accentColor: _stepAccentColors[5],
|
accentColor: _stepAccentColors[6],
|
||||||
onChanged: (value) =>
|
onChanged: (value) =>
|
||||||
setState(() => _dailyCalories = value),
|
setState(() => _dailyCalories = value),
|
||||||
),
|
),
|
||||||
@@ -1108,7 +1125,83 @@ class _ActivityCard extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Step 6: Calorie review ─────────────────────────────────────
|
// ── Step 6: Meal types ─────────────────────────────────────────
|
||||||
|
|
||||||
|
class _MealTypesStepContent extends StatelessWidget {
|
||||||
|
final List<String> selected;
|
||||||
|
final Color accentColor;
|
||||||
|
final ValueChanged<List<String>> onChanged;
|
||||||
|
|
||||||
|
const _MealTypesStepContent({
|
||||||
|
required this.selected,
|
||||||
|
required this.accentColor,
|
||||||
|
required this.onChanged,
|
||||||
|
});
|
||||||
|
|
||||||
|
void _toggle(String mealTypeId) {
|
||||||
|
final updated = List<String>.from(selected);
|
||||||
|
if (updated.contains(mealTypeId)) {
|
||||||
|
// Keep at least one meal type selected.
|
||||||
|
if (updated.length > 1) updated.remove(mealTypeId);
|
||||||
|
} else {
|
||||||
|
updated.add(mealTypeId);
|
||||||
|
}
|
||||||
|
onChanged(updated);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final theme = Theme.of(context);
|
||||||
|
return Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text('Ваши приёмы пищи', style: theme.textTheme.headlineSmall),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
'Выберите, какие приёмы пищи вы хотите отслеживать',
|
||||||
|
style: theme.textTheme.bodyMedium
|
||||||
|
?.copyWith(color: AppColors.textSecondary),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
...kAllMealTypes.map((mealTypeOption) {
|
||||||
|
final isSelected = selected.contains(mealTypeOption.id);
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.only(bottom: 10),
|
||||||
|
child: _SelectableCard(
|
||||||
|
selected: isSelected,
|
||||||
|
accentColor: accentColor,
|
||||||
|
onTap: () => _toggle(mealTypeOption.id),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Text(mealTypeOption.emoji,
|
||||||
|
style: const TextStyle(fontSize: 24)),
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
mealTypeOption.label,
|
||||||
|
style: theme.textTheme.bodyLarge?.copyWith(
|
||||||
|
color: isSelected
|
||||||
|
? accentColor
|
||||||
|
: AppColors.textPrimary,
|
||||||
|
fontWeight: isSelected
|
||||||
|
? FontWeight.w600
|
||||||
|
: FontWeight.normal,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
if (isSelected)
|
||||||
|
Icon(Icons.check_circle, color: accentColor),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Step 7: Calorie review ─────────────────────────────────────
|
||||||
|
|
||||||
class _CalorieStepContent extends StatelessWidget {
|
class _CalorieStepContent extends StatelessWidget {
|
||||||
final int calories;
|
final int calories;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|||||||
import '../../core/auth/auth_provider.dart';
|
import '../../core/auth/auth_provider.dart';
|
||||||
import '../../core/locale/language_provider.dart';
|
import '../../core/locale/language_provider.dart';
|
||||||
import '../../core/theme/app_colors.dart';
|
import '../../core/theme/app_colors.dart';
|
||||||
|
import '../../shared/models/meal_type.dart';
|
||||||
import '../../shared/models/user.dart';
|
import '../../shared/models/user.dart';
|
||||||
import 'profile_provider.dart';
|
import 'profile_provider.dart';
|
||||||
import 'profile_service.dart';
|
import 'profile_service.dart';
|
||||||
@@ -92,7 +93,7 @@ class _ProfileBody extends ConsumerWidget {
|
|||||||
]),
|
]),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
// Calories
|
// Calories + meal types
|
||||||
_SectionLabel('ПИТАНИЕ'),
|
_SectionLabel('ПИТАНИЕ'),
|
||||||
const SizedBox(height: 6),
|
const SizedBox(height: 6),
|
||||||
_InfoCard(children: [
|
_InfoCard(children: [
|
||||||
@@ -102,6 +103,14 @@ class _ProfileBody extends ConsumerWidget {
|
|||||||
? '${user.dailyCalories} ккал/день'
|
? '${user.dailyCalories} ккал/день'
|
||||||
: null,
|
: null,
|
||||||
),
|
),
|
||||||
|
const Divider(height: 1, indent: 16),
|
||||||
|
_InfoRow(
|
||||||
|
'Приёмы пищи',
|
||||||
|
user.mealTypes
|
||||||
|
.map((mealTypeId) =>
|
||||||
|
mealTypeById(mealTypeId)?.label ?? mealTypeId)
|
||||||
|
.join(', '),
|
||||||
|
),
|
||||||
]),
|
]),
|
||||||
if (user.dailyCalories != null) ...[
|
if (user.dailyCalories != null) ...[
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 4),
|
||||||
@@ -342,6 +351,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
String? _goal;
|
String? _goal;
|
||||||
String? _activity;
|
String? _activity;
|
||||||
String? _language;
|
String? _language;
|
||||||
|
List<String> _mealTypes = [];
|
||||||
bool _saving = false;
|
bool _saving = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -358,6 +368,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
_goal = u.goal;
|
_goal = u.goal;
|
||||||
_activity = u.activity;
|
_activity = u.activity;
|
||||||
_language = u.preferences['language'] as String? ?? 'ru';
|
_language = u.preferences['language'] as String? ?? 'ru';
|
||||||
|
_mealTypes = List<String>.from(u.mealTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -400,6 +411,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
goal: _goal,
|
goal: _goal,
|
||||||
activity: _activity,
|
activity: _activity,
|
||||||
language: _language,
|
language: _language,
|
||||||
|
mealTypes: _mealTypes,
|
||||||
);
|
);
|
||||||
|
|
||||||
final ok = await ref.read(profileProvider.notifier).update(req);
|
final ok = await ref.read(profileProvider.notifier).update(req);
|
||||||
@@ -590,12 +602,39 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
|
|
||||||
|
// Meal types
|
||||||
|
Text('Приёмы пищи', style: theme.textTheme.labelMedium),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Wrap(
|
||||||
|
spacing: 8,
|
||||||
|
runSpacing: 6,
|
||||||
|
children: kAllMealTypes.map((mealTypeOption) {
|
||||||
|
final isSelected =
|
||||||
|
_mealTypes.contains(mealTypeOption.id);
|
||||||
|
return FilterChip(
|
||||||
|
label: Text(
|
||||||
|
'${mealTypeOption.emoji} ${mealTypeOption.label}'),
|
||||||
|
selected: isSelected,
|
||||||
|
onSelected: (selected) {
|
||||||
|
setState(() {
|
||||||
|
if (selected) {
|
||||||
|
_mealTypes.add(mealTypeOption.id);
|
||||||
|
} else if (_mealTypes.length > 1) {
|
||||||
|
_mealTypes.remove(mealTypeOption.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
|
||||||
// Language
|
// Language
|
||||||
ref.watch(supportedLanguagesProvider).when(
|
ref.watch(supportedLanguagesProvider).when(
|
||||||
data: (languages) => DropdownButtonFormField<String>(
|
data: (languages) => DropdownButtonFormField<String>(
|
||||||
value: _language,
|
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
labelText: 'Язык интерфейса'),
|
labelText: 'Язык интерфейса'),
|
||||||
|
initialValue: _language,
|
||||||
items: languages.entries
|
items: languages.entries
|
||||||
.map((e) => DropdownMenuItem(
|
.map((e) => DropdownMenuItem(
|
||||||
value: e.key,
|
value: e.key,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class UpdateProfileRequest {
|
|||||||
final String? activity;
|
final String? activity;
|
||||||
final String? goal;
|
final String? goal;
|
||||||
final String? language;
|
final String? language;
|
||||||
|
final List<String>? mealTypes;
|
||||||
final int? dailyCalories;
|
final int? dailyCalories;
|
||||||
|
|
||||||
const UpdateProfileRequest({
|
const UpdateProfileRequest({
|
||||||
@@ -28,6 +29,7 @@ class UpdateProfileRequest {
|
|||||||
this.activity,
|
this.activity,
|
||||||
this.goal,
|
this.goal,
|
||||||
this.language,
|
this.language,
|
||||||
|
this.mealTypes,
|
||||||
this.dailyCalories,
|
this.dailyCalories,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -40,8 +42,12 @@ class UpdateProfileRequest {
|
|||||||
if (gender != null) map['gender'] = gender;
|
if (gender != null) map['gender'] = gender;
|
||||||
if (activity != null) map['activity'] = activity;
|
if (activity != null) map['activity'] = activity;
|
||||||
if (goal != null) map['goal'] = goal;
|
if (goal != null) map['goal'] = goal;
|
||||||
if (language != null) map['preferences'] = {'language': language};
|
|
||||||
if (dailyCalories != null) map['daily_calories'] = dailyCalories;
|
if (dailyCalories != null) map['daily_calories'] = dailyCalories;
|
||||||
|
// Build preferences patch — backend merges into existing JSONB.
|
||||||
|
final prefPatch = <String, dynamic>{};
|
||||||
|
if (language != null) prefPatch['language'] = language;
|
||||||
|
if (mealTypes != null) prefPatch['meal_types'] = mealTypes;
|
||||||
|
if (prefPatch.isNotEmpty) map['preferences'] = prefPatch;
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,50 +1,347 @@
|
|||||||
import 'package:flutter/material.dart';
|
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';
|
import 'recognition_service.dart';
|
||||||
|
|
||||||
/// Shows the nutritional breakdown of a recognized dish.
|
/// Shows the recognition candidates and lets the user confirm a dish entry
|
||||||
class DishResultScreen extends StatelessWidget {
|
/// before adding it to the diary.
|
||||||
const DishResultScreen({super.key, required this.dish});
|
class DishResultScreen extends ConsumerStatefulWidget {
|
||||||
|
const DishResultScreen({
|
||||||
|
super.key,
|
||||||
|
required this.dish,
|
||||||
|
this.preselectedMealType,
|
||||||
|
});
|
||||||
|
|
||||||
final DishResult dish;
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
final confPct = (dish.confidence * 100).toInt();
|
final hasCandidates = widget.dish.candidates.isNotEmpty;
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Распознано блюдо')),
|
appBar: AppBar(title: const Text('Распознано блюдо')),
|
||||||
body: ListView(
|
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('Добавить в журнал'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: null,
|
||||||
|
body: hasCandidates
|
||||||
|
? ListView(
|
||||||
padding: const EdgeInsets.all(20),
|
padding: const EdgeInsets.all(20),
|
||||||
children: [
|
children: [
|
||||||
// Dish name + confidence
|
_CandidatesSection(
|
||||||
Text(
|
candidates: widget.dish.candidates,
|
||||||
dish.dishName,
|
selectedIndex: _selectedIndex,
|
||||||
style: theme.textTheme.headlineSmall
|
onSelect: _selectCandidate,
|
||||||
?.copyWith(fontWeight: FontWeight.bold),
|
|
||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 20),
|
||||||
Row(
|
_NutritionCard(
|
||||||
children: [
|
calories: _scale(_selected.calories),
|
||||||
Icon(
|
proteinG: _scale(_selected.proteinG),
|
||||||
Icons.info_outline,
|
fatG: _scale(_selected.fatG),
|
||||||
size: 14,
|
carbsG: _scale(_selected.carbsG),
|
||||||
color: theme.colorScheme.onSurfaceVariant,
|
|
||||||
),
|
),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(height: 8),
|
||||||
Text(
|
Text(
|
||||||
'Уверенность: $confPct%',
|
'КБЖУ приблизительные — определены по фото.',
|
||||||
style: theme.textTheme.bodySmall?.copyWith(
|
style: theme.textTheme.bodySmall?.copyWith(
|
||||||
color: theme.colorScheme.onSurfaceVariant,
|
color: theme.colorScheme.onSurfaceVariant,
|
||||||
),
|
),
|
||||||
|
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),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => context.pop(),
|
||||||
|
child: const Text('Попробовать снова'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 24),
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Nutrition card
|
// ---------------------------------------------------------------------------
|
||||||
Card(
|
// 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(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
child: Column(
|
child: Column(
|
||||||
@@ -53,7 +350,7 @@ class DishResultScreen extends StatelessWidget {
|
|||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
'≈ ${dish.calories.toInt()} ккал',
|
'≈ ${calories.toInt()} ккал',
|
||||||
style: theme.textTheme.titleLarge?.copyWith(
|
style: theme.textTheme.titleLarge?.copyWith(
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
color: theme.colorScheme.primary,
|
color: theme.colorScheme.primary,
|
||||||
@@ -77,59 +374,24 @@ class DishResultScreen extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
_MacroChip(
|
_MacroChip(
|
||||||
label: 'Белки',
|
label: 'Белки',
|
||||||
value: '${dish.proteinG.toStringAsFixed(1)} г',
|
value: '${proteinG.toStringAsFixed(1)} г',
|
||||||
color: Colors.blue,
|
color: Colors.blue,
|
||||||
),
|
),
|
||||||
_MacroChip(
|
_MacroChip(
|
||||||
label: 'Жиры',
|
label: 'Жиры',
|
||||||
value: '${dish.fatG.toStringAsFixed(1)} г',
|
value: '${fatG.toStringAsFixed(1)} г',
|
||||||
color: Colors.orange,
|
color: Colors.orange,
|
||||||
),
|
),
|
||||||
_MacroChip(
|
_MacroChip(
|
||||||
label: 'Углеводы',
|
label: 'Углеводы',
|
||||||
value: '${dish.carbsG.toStringAsFixed(1)} г',
|
value: '${carbsG.toStringAsFixed(1)} г',
|
||||||
color: Colors.green,
|
color: Colors.green,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
|
||||||
Text(
|
|
||||||
'Вес порции: ~${dish.weightGrams} г',
|
|
||||||
style: theme.textTheme.bodySmall?.copyWith(
|
|
||||||
color: theme.colorScheme.onSurfaceVariant,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
|
|
||||||
// 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(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
|
|
||||||
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,
|
color: color,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Text(
|
Text(label, style: Theme.of(context).textTheme.labelSmall),
|
||||||
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,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -61,7 +61,8 @@ class ReceiptResult {
|
|||||||
const ReceiptResult({required this.items, required this.unrecognized});
|
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 String dishName;
|
||||||
final int weightGrams;
|
final int weightGrams;
|
||||||
final double calories;
|
final double calories;
|
||||||
@@ -69,9 +70,8 @@ class DishResult {
|
|||||||
final double fatG;
|
final double fatG;
|
||||||
final double carbsG;
|
final double carbsG;
|
||||||
final double confidence;
|
final double confidence;
|
||||||
final List<String> similarDishes;
|
|
||||||
|
|
||||||
const DishResult({
|
const DishCandidate({
|
||||||
required this.dishName,
|
required this.dishName,
|
||||||
required this.weightGrams,
|
required this.weightGrams,
|
||||||
required this.calories,
|
required this.calories,
|
||||||
@@ -79,11 +79,10 @@ class DishResult {
|
|||||||
required this.fatG,
|
required this.fatG,
|
||||||
required this.carbsG,
|
required this.carbsG,
|
||||||
required this.confidence,
|
required this.confidence,
|
||||||
required this.similarDishes,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
factory DishResult.fromJson(Map<String, dynamic> json) {
|
factory DishCandidate.fromJson(Map<String, dynamic> json) {
|
||||||
return DishResult(
|
return DishCandidate(
|
||||||
dishName: json['dish_name'] as String? ?? '',
|
dishName: json['dish_name'] as String? ?? '',
|
||||||
weightGrams: json['weight_grams'] as int? ?? 0,
|
weightGrams: json['weight_grams'] as int? ?? 0,
|
||||||
calories: (json['calories'] as num?)?.toDouble() ?? 0,
|
calories: (json['calories'] as num?)?.toDouble() ?? 0,
|
||||||
@@ -91,14 +90,46 @@ class DishResult {
|
|||||||
fatG: (json['fat_g'] as num?)?.toDouble() ?? 0,
|
fatG: (json['fat_g'] as num?)?.toDouble() ?? 0,
|
||||||
carbsG: (json['carbs_g'] as num?)?.toDouble() ?? 0,
|
carbsG: (json['carbs_g'] as num?)?.toDouble() ?? 0,
|
||||||
confidence: (json['confidence'] 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
|
// Service
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -12,11 +12,36 @@ final _recognitionServiceProvider = Provider<RecognitionService>((ref) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/// Entry screen — lets the user choose how to add products.
|
/// 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});
|
const ScanScreen({super.key});
|
||||||
|
|
||||||
@override
|
@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(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Добавить продукты')),
|
appBar: AppBar(title: const Text('Добавить продукты')),
|
||||||
body: ListView(
|
body: ListView(
|
||||||
@@ -33,21 +58,21 @@ class ScanScreen extends ConsumerWidget {
|
|||||||
emoji: '🧾',
|
emoji: '🧾',
|
||||||
title: 'Сфотографировать чек',
|
title: 'Сфотографировать чек',
|
||||||
subtitle: 'Распознаем все продукты из чека',
|
subtitle: 'Распознаем все продукты из чека',
|
||||||
onTap: () => _pickAndRecognize(context, ref, _Mode.receipt),
|
onTap: () => _pickAndRecognize(context, _Mode.receipt),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_ModeCard(
|
_ModeCard(
|
||||||
emoji: '🥦',
|
emoji: '🥦',
|
||||||
title: 'Сфотографировать продукты',
|
title: 'Сфотографировать продукты',
|
||||||
subtitle: 'Холодильник, стол, полка — до 3 фото',
|
subtitle: 'Холодильник, стол, полка — до 3 фото',
|
||||||
onTap: () => _pickAndRecognize(context, ref, _Mode.products),
|
onTap: () => _pickAndRecognize(context, _Mode.products),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_ModeCard(
|
_ModeCard(
|
||||||
emoji: '🍽️',
|
emoji: '🍽️',
|
||||||
title: 'Определить блюдо',
|
title: 'Определить блюдо',
|
||||||
subtitle: 'КБЖУ≈ по фото готового блюда',
|
subtitle: 'КБЖУ≈ по фото готового блюда',
|
||||||
onTap: () => _pickAndRecognize(context, ref, _Mode.dish),
|
onTap: () => _pickAndRecognize(context, _Mode.dish),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_ModeCard(
|
_ModeCard(
|
||||||
@@ -63,9 +88,9 @@ class ScanScreen extends ConsumerWidget {
|
|||||||
|
|
||||||
Future<void> _pickAndRecognize(
|
Future<void> _pickAndRecognize(
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
WidgetRef ref,
|
_Mode mode, {
|
||||||
_Mode mode,
|
String? mealType,
|
||||||
) async {
|
}) async {
|
||||||
final picker = ImagePicker();
|
final picker = ImagePicker();
|
||||||
|
|
||||||
List<XFile> files = [];
|
List<XFile> files = [];
|
||||||
@@ -120,11 +145,11 @@ class ScanScreen extends ConsumerWidget {
|
|||||||
final dish = await service.recognizeDish(files.first);
|
final dish = await service.recognizeDish(files.first);
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
context.push('/scan/dish', extra: dish);
|
context.push('/scan/dish', extra: {'dish': dish, 'meal_type': mealType});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e, s) {
|
} catch (recognitionError) {
|
||||||
debugPrint('Recognition error: $e\n$s');
|
debugPrint('Recognition error: $recognitionError');
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.pop(context); // close loading
|
Navigator.pop(context); // close loading
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
|||||||
29
client/lib/shared/models/meal_type.dart
Normal file
29
client/lib/shared/models/meal_type.dart
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/// A configurable meal type that the user tracks throughout the day.
|
||||||
|
class MealTypeOption {
|
||||||
|
final String id;
|
||||||
|
final String label;
|
||||||
|
final String emoji;
|
||||||
|
|
||||||
|
const MealTypeOption({
|
||||||
|
required this.id,
|
||||||
|
required this.label,
|
||||||
|
required this.emoji,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// All meal types available for selection.
|
||||||
|
const kAllMealTypes = [
|
||||||
|
MealTypeOption(id: 'breakfast', label: 'Завтрак', emoji: '🌅'),
|
||||||
|
MealTypeOption(id: 'second_breakfast', label: 'Второй завтрак', emoji: '☕'),
|
||||||
|
MealTypeOption(id: 'lunch', label: 'Обед', emoji: '🍽️'),
|
||||||
|
MealTypeOption(id: 'afternoon_snack', label: 'Полдник', emoji: '🥗'),
|
||||||
|
MealTypeOption(id: 'dinner', label: 'Ужин', emoji: '🌙'),
|
||||||
|
MealTypeOption(id: 'snack', label: 'Перекус', emoji: '🍎'),
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Default meal type IDs assigned to new users.
|
||||||
|
const kDefaultMealTypeIds = ['breakfast', 'lunch', 'dinner'];
|
||||||
|
|
||||||
|
/// Returns the [MealTypeOption] for the given [id], or null if not found.
|
||||||
|
MealTypeOption? mealTypeById(String id) =>
|
||||||
|
kAllMealTypes.where((option) => option.id == id).firstOrNull;
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import 'package:json_annotation/json_annotation.dart';
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
|
import 'meal_type.dart';
|
||||||
|
|
||||||
part 'user.g.dart';
|
part 'user.g.dart';
|
||||||
|
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
@@ -59,4 +61,12 @@ class User {
|
|||||||
bool get hasCompletedOnboarding =>
|
bool get hasCompletedOnboarding =>
|
||||||
heightCm != null && weightKg != null && dateOfBirth != null &&
|
heightCm != null && weightKg != null && dateOfBirth != null &&
|
||||||
gender != null && goal != null && activity != null;
|
gender != null && goal != null && activity != null;
|
||||||
|
|
||||||
|
/// Returns the user's configured meal type IDs from preferences,
|
||||||
|
/// falling back to the default set if not yet configured.
|
||||||
|
List<String> get mealTypes {
|
||||||
|
final value = preferences['meal_types'];
|
||||||
|
if (value is List && value.isNotEmpty) return List<String>.from(value);
|
||||||
|
return List<String>.from(kDefaultMealTypeIds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,10 +125,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: characters
|
name: characters
|
||||||
sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b
|
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.4.1"
|
version: "1.4.0"
|
||||||
checked_yaml:
|
checked_yaml:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -668,26 +668,26 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: matcher
|
name: matcher
|
||||||
sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861
|
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.12.19"
|
version: "0.12.17"
|
||||||
material_color_utilities:
|
material_color_utilities:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: material_color_utilities
|
name: material_color_utilities
|
||||||
sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b"
|
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.13.0"
|
version: "0.11.1"
|
||||||
meta:
|
meta:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: meta
|
name: meta
|
||||||
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
|
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.17.0"
|
version: "1.16.0"
|
||||||
mime:
|
mime:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -993,10 +993,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: test_api
|
name: test_api
|
||||||
sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a"
|
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.7.10"
|
version: "0.7.6"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
Reference in New Issue
Block a user