feat:更新编辑菜谱功能
This commit is contained in:
@@ -66,6 +66,86 @@ class FoodProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateRecipeFormItem({
|
||||
String? name,
|
||||
String? category,
|
||||
double? recommendRate,
|
||||
String? remark,
|
||||
bool? isShare,
|
||||
}) {
|
||||
if (name != null) recipeFormItem.name = name;
|
||||
if (category != null) recipeFormItem.category = category;
|
||||
if (recommendRate != null) recipeFormItem.recommendRate = recommendRate;
|
||||
if (remark != null) recipeFormItem.remark = remark;
|
||||
if (isShare != null) recipeFormItem.isShare = isShare;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void addRecipeMaterial() {
|
||||
recipeFormItem.materialList.add(
|
||||
RecipeMaterial(
|
||||
id: DateTime.now().microsecondsSinceEpoch,
|
||||
type: '主料',
|
||||
name: '',
|
||||
amount: '',
|
||||
),
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateRecipeMaterial(
|
||||
int index, {
|
||||
String? type,
|
||||
String? name,
|
||||
String? amount,
|
||||
}) {
|
||||
final material = recipeFormItem.materialList[index];
|
||||
recipeFormItem.materialList[index] = RecipeMaterial(
|
||||
id: material.id,
|
||||
type: type ?? material.type,
|
||||
name: name ?? material.name,
|
||||
amount: amount ?? material.amount,
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void removeRecipeMaterial(int index) {
|
||||
recipeFormItem.materialList.removeAt(index);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void addRecipeStep() {
|
||||
recipeFormItem.stepList.add(
|
||||
RecipeStep(
|
||||
id: DateTime.now().microsecondsSinceEpoch,
|
||||
sort: recipeFormItem.stepList.length,
|
||||
content: '',
|
||||
imageUrl: '',
|
||||
),
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateRecipeStep(int index, {String? content, String? imageUrl}) {
|
||||
final step = recipeFormItem.stepList[index];
|
||||
recipeFormItem.stepList[index] = RecipeStep(
|
||||
id: step.id,
|
||||
sort: step.sort,
|
||||
content: content ?? step.content,
|
||||
imageUrl: imageUrl ?? step.imageUrl,
|
||||
);
|
||||
}
|
||||
|
||||
void removeRecipeStep(int index) {
|
||||
recipeFormItem.stepList.removeAt(index);
|
||||
|
||||
for (int i = 0; i < recipeFormItem.stepList.length; i++) {
|
||||
recipeFormItem.stepList[i].sort = i;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateRecordFormItem({String? name, String? date, String? imageUrl}) {
|
||||
if (name != null) recordFormItem.name = name;
|
||||
if (date != null) recordFormItem.date = date;
|
||||
@@ -167,6 +247,30 @@ class FoodProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> handleRecipe() async {
|
||||
if (isLoading) return true;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
if (isEditing) {
|
||||
await updateRecipeApi(recipeFormItem.id!, recipeFormItem);
|
||||
} else {
|
||||
await addRecipeApi(recipeFormItem);
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
error = '处理数据失败: $e';
|
||||
debugPrint('处理数据失败: $e');
|
||||
return false;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshRecordList() async {
|
||||
if (isLoading) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user