feat:增加分享图片功能

This commit is contained in:
2025-11-20 18:43:09 +08:00
parent a9f2e102c5
commit 4f033a4d0a
10 changed files with 220 additions and 4 deletions

View File

@@ -44,14 +44,17 @@ class _HomePage extends State<HomePage> {
onPressed: () {
buildBottomSheet(context);
},
)
),
],
)
),
],
),
// backgroundColor: Color(0xFFF5F5F5),
drawer: SettingsDrawer(),
body: Padding(padding: EdgeInsets.all(10), child: tabPages[_currentIndex]),
body: Padding(
padding: EdgeInsets.all(10),
child: tabPages[_currentIndex],
),
bottomNavigationBar: buildBottomNavBar(
currentIndex: _currentIndex,
onTap: (index) {

View File

@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:food_hub_app/apis/recipe.dart';
import 'package:food_hub_app/models/recipe.dart';
import 'package:food_hub_app/utils/screenshot_util.dart';
import 'package:food_hub_app/widgets/common/index.dart';
class RecipeDetailPage extends StatefulWidget {
@@ -12,6 +13,8 @@ class RecipeDetailPage extends StatefulWidget {
class _RecipeDetailState extends State<RecipeDetailPage> {
RecipeDetail recipe = RecipeDetail.getEmpty();
final GlobalKey _recipeDetailKey = GlobalKey();
bool _isSharing = false;
// 定义三个分类列表
List<RecipeMaterial> mainMaterialList = []; // 主料
@@ -36,6 +39,10 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
}
void getRecipeMaterial() {
mainMaterialList.clear();
auxiliaryMaterialList.clear();
accessoryMaterialList.clear();
for (var material in recipe.materialList) {
switch (material.type) {
case '主料':
@@ -51,12 +58,45 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
}
}
// 分享图片
Future<void> _shareRecipeAsImage() async {
if (_isSharing) return;
setState(() {
_isSharing = true;
});
final timestamp = DateTime.now().millisecondsSinceEpoch;
final imageName = '${recipe.name}_$timestamp.png';
await ScreenshotUtil.captureAndShare(
globalKey: _recipeDetailKey,
fileName: imageName,
shareText: '分享菜谱: ${recipe.name}',
);
setState(() {
_isSharing = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('菜谱信息')),
body: SingleChildScrollView(
child: Padding(padding: EdgeInsets.all(10), child: _buildRecipeDetail()),
child: Column(
children: [
// 用RepaintBoundary包裹要截图的部分
RepaintBoundary(
key: _recipeDetailKey,
child: Padding(
padding: EdgeInsets.all(10),
child: _buildRecipeDetail(),
),
),
_buildButtons(),
],
),
),
);
}
@@ -206,4 +246,17 @@ class _RecipeDetailState extends State<RecipeDetailPage> {
),
);
}
Widget _buildButtons() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
circleIconButton(
context: context,
icon: _isSharing ? Icons.hourglass_top : Icons.share,
onPressed: _shareRecipeAsImage,
),
],
);
}
}