feat:更新组件UI
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.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';
|
||||
import 'package:food_hub_app/models/moment.dart';
|
||||
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';
|
||||
@@ -13,6 +15,8 @@ class FoodProvider with ChangeNotifier {
|
||||
bool isLoading = false;
|
||||
String? error;
|
||||
|
||||
String queryCategory = '全部菜系';
|
||||
late List<String> categoryList = [];
|
||||
late List<RecipeSummary> recipeSummaryList = [];
|
||||
|
||||
DateTime selectedDay = DateTime.now();
|
||||
@@ -31,6 +35,11 @@ class FoodProvider with ChangeNotifier {
|
||||
late List<ChartData> categoryStats = [];
|
||||
late List<ChartData> rankStats = [];
|
||||
|
||||
List<Moment> momentList = [];
|
||||
int currentPage = 1;
|
||||
final int pageSize = 3;
|
||||
bool hasMore = true;
|
||||
|
||||
void resetRecordForm() {
|
||||
recordFormItem = FoodRecord.getEmpty();
|
||||
notifyListeners();
|
||||
@@ -41,6 +50,23 @@ class FoodProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> queryCategoryList() async {
|
||||
try {
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryCategoryApi();
|
||||
categoryList.clear();
|
||||
categoryList.add('全部菜系');
|
||||
categoryList.addAll(result);
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshRecipeList() async {
|
||||
if (isLoading) return;
|
||||
|
||||
@@ -49,7 +75,8 @@ class FoodProvider with ChangeNotifier {
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryRecipeApi(RecipeQuery(category: ""));
|
||||
final category = queryCategory == '全部菜系' ? '' : queryCategory;
|
||||
final result = await queryRecipeApi(RecipeQuery(category: category));
|
||||
recipeSummaryList = result;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
@@ -131,4 +158,50 @@ class FoodProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> queryMomentByPage({bool isRefresh = true}) async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
// 如果是刷新,重置页码
|
||||
if (isRefresh) {
|
||||
currentPage = 1;
|
||||
}
|
||||
|
||||
final result = await queryMomentByPageApi(currentPage, pageSize);
|
||||
|
||||
// 更新数据
|
||||
if (isRefresh) {
|
||||
momentList = result.records;
|
||||
} else {
|
||||
momentList.addAll(result.records);
|
||||
}
|
||||
|
||||
// 判断是否还有更多数据
|
||||
hasMore = result.current < result.pages;
|
||||
if (hasMore) {
|
||||
currentPage++;
|
||||
}
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadMoreMomentList() async {
|
||||
if (hasMore && !isLoading) {
|
||||
await queryMomentByPage(isRefresh: false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshMomentList() async {
|
||||
await queryMomentByPage(isRefresh: true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user