feat:更新编辑菜谱功能
This commit is contained in:
@@ -2,8 +2,11 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/widget/common_widget.dart';
|
||||
import 'package:food_hub_app/apis/recipe.dart';
|
||||
import 'package:food_hub_app/models/recipe.dart';
|
||||
import 'package:food_hub_app/provider/food_provider.dart';
|
||||
import 'package:food_hub_app/utils/screenshot_util.dart';
|
||||
import 'package:food_hub_app/views/recipe_form.dart';
|
||||
import 'package:food_hub_app/widgets/common/index.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class RecipeDetailPage extends StatefulWidget {
|
||||
const RecipeDetailPage({super.key});
|
||||
@@ -13,14 +16,11 @@ class RecipeDetailPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
RecipeDetail recipe = RecipeDetail.getEmpty();
|
||||
Recipe recipe = Recipe.getEmpty();
|
||||
final GlobalKey _recipeDetailKey = GlobalKey();
|
||||
bool _isSharing = false;
|
||||
|
||||
// 定义三个分类列表
|
||||
List<RecipeMaterial> mainMaterialList = []; // 主料
|
||||
List<RecipeMaterial> auxiliaryMaterialList = []; // 配料
|
||||
List<RecipeMaterial> accessoryMaterialList = []; // 辅料
|
||||
List<RecipeMaterial> materialList = [];
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
@@ -40,25 +40,29 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
}
|
||||
|
||||
void getRecipeMaterial() {
|
||||
mainMaterialList.clear();
|
||||
auxiliaryMaterialList.clear();
|
||||
accessoryMaterialList.clear();
|
||||
materialList.clear();
|
||||
|
||||
for (var material in recipe.materialList) {
|
||||
switch (material.type) {
|
||||
case '主料':
|
||||
mainMaterialList.add(material);
|
||||
break;
|
||||
case '配料':
|
||||
auxiliaryMaterialList.add(material);
|
||||
break;
|
||||
case '辅料':
|
||||
accessoryMaterialList.add(material);
|
||||
break;
|
||||
}
|
||||
materialList.add(material);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleEditRecipe(FoodProvider provider) {
|
||||
provider.initRecipeForm(recipe);
|
||||
provider.isEditing = true;
|
||||
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
constraints: BoxConstraints(minHeight: 600),
|
||||
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
|
||||
isScrollControlled: true,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
||||
),
|
||||
builder: (context) => RecipeForm(),
|
||||
);
|
||||
}
|
||||
|
||||
// 分享图片
|
||||
Future<void> _shareRecipeAsImage() async {
|
||||
if (_isSharing) return;
|
||||
@@ -83,6 +87,7 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final provider = context.watch<FoodProvider>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
@@ -104,7 +109,7 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
child: _buildRecipeDetail(),
|
||||
),
|
||||
),
|
||||
_buildButtons(),
|
||||
_buildButtons(provider),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -121,7 +126,23 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
context,
|
||||
icon: Icons.info,
|
||||
title: "基础信息",
|
||||
content: buildTag(context, recipe.category),
|
||||
content: Text(recipe.category),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
context,
|
||||
icon: Icons.star,
|
||||
title: "推荐指数",
|
||||
content: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: List.generate(5, (index) {
|
||||
return Icon(
|
||||
index < recipe.recommendRate ? Icons.star : Icons.star_border,
|
||||
color: Theme.of(context).primaryColor,
|
||||
size: 20,
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
@@ -130,11 +151,7 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
title: "食材信息",
|
||||
content: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildMaterialSection(context, '主料', mainMaterialList),
|
||||
_buildMaterialSection(context, '辅料', auxiliaryMaterialList),
|
||||
_buildMaterialSection(context, '调料', accessoryMaterialList),
|
||||
],
|
||||
children: [_buildMaterialSection(materialList)],
|
||||
),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
@@ -142,10 +159,7 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
context,
|
||||
icon: Icons.list,
|
||||
title: "步骤信息",
|
||||
content: Column(
|
||||
children:
|
||||
recipe.stepList.map((step) => _buildStepSection(step)).toList(),
|
||||
),
|
||||
content: Column(children: [_buildStepSection(recipe.stepList)]),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
@@ -168,34 +182,43 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMaterialSection(
|
||||
BuildContext context,
|
||||
String title,
|
||||
List<RecipeMaterial> materials,
|
||||
) {
|
||||
Widget _buildMaterialSection(List<RecipeMaterial> materials) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8, top: 12),
|
||||
child: Text(title, style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
),
|
||||
if (materials.isEmpty)
|
||||
buildTag(context, '无')
|
||||
else
|
||||
Wrap(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 8.0,
|
||||
runSpacing: 8.0,
|
||||
children:
|
||||
materials
|
||||
.map((m) => buildTag(context, '${m.name} ${m.amount}'))
|
||||
.toList(),
|
||||
children: materials.map((m) => _buildMaterialItem(m)).toList(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStepSection(RecipeStep step) {
|
||||
Widget _buildMaterialItem(RecipeMaterial material) {
|
||||
return Text('${material.name} ${material.amount}');
|
||||
}
|
||||
|
||||
Widget _buildStepSection(List<RecipeStep> steps) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (steps.isEmpty)
|
||||
buildTag(context, '无')
|
||||
else
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 8.0,
|
||||
children: steps.map((m) => _buildStepItem(m)).toList(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStepItem(RecipeStep step) {
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -213,16 +236,6 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (step.imageUrl.isNotEmpty)
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Image.network(
|
||||
step.imageUrl,
|
||||
width: double.infinity,
|
||||
height: 180,
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -257,10 +270,16 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildButtons() {
|
||||
Widget _buildButtons(FoodProvider provider) {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
circleIconButton(
|
||||
context: context,
|
||||
icon: Icons.edit,
|
||||
onPressed: () => _handleEditRecipe(provider),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
circleIconButton(
|
||||
context: context,
|
||||
icon: _isSharing ? Icons.hourglass_top : Icons.share,
|
||||
|
||||
Reference in New Issue
Block a user