feat:增加分享图片功能
This commit is contained in:
61
lib/utils/screenshot_util.dart
Normal file
61
lib/utils/screenshot_util.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:food_hub_app/widgets/common/index.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
/// 截图工具类
|
||||
class ScreenshotUtil {
|
||||
// 截图功能
|
||||
static Future<Uint8List?> captureRecipeImage({
|
||||
required GlobalKey globalKey,
|
||||
double pixelRatio = 3.0,
|
||||
Duration delay = const Duration(milliseconds: 50),
|
||||
}) async {
|
||||
// 等待一帧确保UI已渲染
|
||||
await Future.delayed(delay);
|
||||
final RenderObject? render = globalKey.currentContext!.findRenderObject();
|
||||
final RenderRepaintBoundary boundary = render as RenderRepaintBoundary;
|
||||
final ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
|
||||
final ByteData? byteData = await image.toByteData(
|
||||
format: ui.ImageByteFormat.png,
|
||||
);
|
||||
|
||||
return byteData?.buffer.asUint8List();
|
||||
}
|
||||
|
||||
// 保存图片到临时文件
|
||||
static Future<File> saveImageToFile(Uint8List bytes, String fileName) async {
|
||||
final directory = await getTemporaryDirectory();
|
||||
final File imageFile = File('${directory.path}/$fileName');
|
||||
await imageFile.writeAsBytes(bytes);
|
||||
return imageFile;
|
||||
}
|
||||
|
||||
static Future<void> captureAndShare({
|
||||
required GlobalKey globalKey,
|
||||
required String fileName,
|
||||
required String shareText,
|
||||
}) async {
|
||||
try {
|
||||
final imageBytes = await captureRecipeImage(globalKey: globalKey);
|
||||
|
||||
if (imageBytes != null) {
|
||||
final imageFile = await saveImageToFile(imageBytes, fileName);
|
||||
await SharePlus.instance.share(
|
||||
ShareParams(files: [XFile(imageFile.path)], text: shareText),
|
||||
);
|
||||
|
||||
// 分享后删除临时文件
|
||||
await imageFile.delete();
|
||||
} else {
|
||||
showErrorToast('生成图片失败,请重试');
|
||||
}
|
||||
} catch (e) {
|
||||
showErrorToast('分享失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user