feat:更新美食记录表单

This commit is contained in:
2025-11-23 00:02:41 +08:00
parent 1ac504f3d7
commit 6308169013
41 changed files with 465 additions and 1618 deletions

View File

@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_common/models/common_model.dart';
import 'package:flutter_common/utils/toast_util.dart';
import 'package:food_hub_app/apis/moment.dart';
import 'package:food_hub_app/apis/recipe.dart';
import 'package:food_hub_app/apis/stats.dart';
@@ -7,8 +9,10 @@ import 'package:food_hub_app/models/recipe.dart';
import 'package:food_hub_app/models/stats.dart';
import 'package:food_hub_app/utils/date_util.dart';
import 'package:food_hub_app/utils/index.dart';
import 'package:food_hub_app/views/record.dart';
class FoodProvider with ChangeNotifier {
late RecordTab currentRecordTab = RecordTab.recipe;
late FoodRecord recordFormItem;
late Moment momentFormItem;
@@ -20,6 +24,7 @@ class FoodProvider with ChangeNotifier {
late List<String> categoryList = [];
late List<RecipeSummary> recipeSummaryList = [];
int selectYear = DateTime.now().year;
DateTime selectedDay = DateTime.now();
DateTime focusedDay = DateTime.now();
@@ -61,6 +66,18 @@ class FoodProvider with ChangeNotifier {
notifyListeners();
}
Future<void> onRecordTabChange() async {
switch (currentRecordTab) {
case RecordTab.recipe:
await refreshRecipeList();
break;
case RecordTab.calendar:
case RecordTab.timeline:
await refreshRecordList();
break;
}
}
Future<void> queryCategoryList() async {
try {
error = null;
@@ -98,16 +115,31 @@ class FoodProvider with ChangeNotifier {
}
}
Future<void> refreshRecordList(String? startDate, String? endDate) async {
Future<void> refreshRecordList() async {
if (isLoading) return;
if (currentRecordTab == RecordTab.recipe) return;
try {
isLoading = true;
error = null;
notifyListeners();
startDate ??= getFirstDayOfMonth(focusedDay);
endDate ??= getLastDayOfMonth(focusedDay);
late String startDate;
late String endDate;
switch (currentRecordTab) {
case RecordTab.calendar:
startDate = getFirstDayOfMonth(focusedDay);
endDate = getLastDayOfMonth(focusedDay);
break;
case RecordTab.timeline:
startDate = '$selectYear-01-01';
endDate = '$selectYear-12-31';
break;
case RecordTab.recipe:
break;
}
final result = await queryRecordApi(startDate, endDate);
@@ -129,6 +161,30 @@ class FoodProvider with ChangeNotifier {
}
}
Future<void> handleRecord() async {
if (isLoading) return;
try {
isLoading = true;
error = null;
notifyListeners();
if (isEditing) {
await updateRecordApi(recordFormItem.id!, recordFormItem);
ToastUtil.success('更新记录成功');
} else {
await addRecordApi(recordFormItem);
ToastUtil.success('上传记录成功');
}
} catch (e) {
error = '处理数据失败: $e';
debugPrint('处理数据失败: $e');
} finally {
isLoading = false;
notifyListeners();
}
}
void refreshSelectRecord() {
selectRecordList =
recordList
@@ -136,7 +192,7 @@ class FoodProvider with ChangeNotifier {
.toList();
}
Future<void> refreshBlogStats() async {
Future<void> refreshFoodStats() async {
if (isLoading) return;
try {

View File

@@ -1,76 +0,0 @@
import 'package:flutter/material.dart';
import 'package:food_hub_app/utils/theme_utils.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ThemeProvider with ChangeNotifier {
static const String _themeKey = 'selected_theme';
static const String _darkModeKey = 'is_dark_mode';
bool isDarkMode = false;
ThemeColor currentTheme = defaultThemes[0];
late SharedPreferences _prefs;
bool isInitialized = false;
ThemeProvider() {
_initPreferences();
}
// 初始化 SharedPreferences
Future<void> _initPreferences() async {
_prefs = await SharedPreferences.getInstance();
_loadPreferences();
isInitialized = true;
notifyListeners();
}
// 加载存储的设置
void _loadPreferences() {
isDarkMode = _prefs.getBool(_darkModeKey) ?? false;
// 加载主题色
final themeName = _prefs.getString(_themeKey);
if (themeName != null) {
currentTheme = getThemeColor(themeName);
}
}
// 保存主题色
Future<void> _saveTheme() async {
await _prefs.setString(_themeKey, currentTheme.name);
}
// 保存暗黑模式
Future<void> _saveDarkMode() async {
await _prefs.setBool(_darkModeKey, isDarkMode);
}
List<ThemeColor> get availableThemes => defaultThemes;
// 切换明暗模式
void toggleDarkMode(bool value) {
isDarkMode = value;
_saveDarkMode();
notifyListeners();
}
// 更改主题色
void changeTheme(ThemeColor theme) {
currentTheme = theme;
_saveTheme();
notifyListeners();
}
ThemeData get currentThemeData {
return ThemeData(
primarySwatch: currentTheme.materialColor,
colorScheme: ColorScheme.fromSeed(
seedColor: currentTheme.primaryColor,
brightness: isDarkMode ? Brightness.dark : Brightness.light,
),
useMaterial3: true,
fontFamily: 'CustomFont',
);
}
}