图片生成

This commit is contained in:
Developer
2026-03-31 06:11:32 +08:00
parent d6ac0ed1e4
commit 66f72abab4
6 changed files with 912 additions and 69 deletions

View File

@@ -2,8 +2,13 @@
/// 功能: 诗词页面通用组件和工具函数
/// 介绍: 从 home_page.dart 和 home_part.dart 中提取的公共组件,用于代码复用和简化
import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import '../../constants/app_constants.dart';
import '../../utils/http/poetry_api.dart';
import 'home-load.dart';
@@ -34,6 +39,84 @@ class LoadingWidget extends StatelessWidget {
}
}
/// 截图和分享工具类
class ShareImageUtils {
/// 将 Widget 截图并分享
static Future<void> captureAndShare(
BuildContext context,
GlobalKey repaintKey, {
String? subject,
}) async {
try {
PoetryStateManager.showSnackBar(
context,
'正在生成图片...',
backgroundColor: AppConstants.primaryColor,
);
final boundary =
repaintKey.currentContext?.findRenderObject()
as RenderRepaintBoundary?;
if (boundary == null) {
if (context.mounted) {
PoetryStateManager.showSnackBar(
context,
'生成图片失败',
backgroundColor: AppConstants.errorColor,
);
}
return;
}
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData? byteData = await image.toByteData(
format: ui.ImageByteFormat.png,
);
if (byteData == null) {
if (context.mounted) {
PoetryStateManager.showSnackBar(
context,
'生成图片失败',
backgroundColor: AppConstants.errorColor,
);
}
return;
}
Uint8List pngBytes = byteData.buffer.asUint8List();
final directory = await getTemporaryDirectory();
final file = File(
'${directory.path}/poetry_${DateTime.now().millisecondsSinceEpoch}.png',
);
await file.writeAsBytes(pngBytes);
final result = await Share.shareXFiles([
XFile(file.path),
], subject: subject ?? '诗词分享');
if (result.status == ShareResultStatus.success) {
if (context.mounted) {
PoetryStateManager.showSnackBar(
context,
'分享成功!',
backgroundColor: AppConstants.successColor,
);
}
}
} catch (e) {
if (context.mounted) {
PoetryStateManager.showSnackBar(
context,
'分享失败:${e.toString().substring(0, e.toString().length > 50 ? 50 : e.toString().length)}',
backgroundColor: AppConstants.errorColor,
);
}
}
}
}
/// 错误状态组件
class CustomErrorWidget extends StatelessWidget {
final String errorMessage;
@@ -277,3 +360,112 @@ class CopyUtils {
);
}
}
/// 悬浮分享按钮组件
class FloatingShareButton extends StatelessWidget {
final VoidCallback onShare;
const FloatingShareButton({super.key, required this.onShare});
@override
Widget build(BuildContext context) {
return Container(
width: 56,
height: 56,
decoration: BoxDecoration(
color: AppConstants.secondaryColor,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: AppConstants.secondaryColor.withAlpha(76),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius: BorderRadius.circular(28),
onTap: () {
HapticFeedback.lightImpact();
onShare();
},
child: const Center(
child: Icon(Icons.share, color: Colors.white, size: 28),
),
),
),
);
}
}
/// 统计信息卡片组件
class StatsCard extends StatelessWidget {
final PoetryData poetryData;
const StatsCard({super.key, required this.poetryData});
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(5),
blurRadius: 5,
offset: const Offset(0, 1),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'统计信息',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: AppConstants.primaryColor,
),
),
const SizedBox(height: 12),
Row(
children: [
_buildStatItem('今日', poetryData.hitsDay.toString()),
const SizedBox(width: 20),
_buildStatItem('本月', poetryData.hitsMonth.toString()),
const SizedBox(width: 20),
_buildStatItem('总计', poetryData.hitsTotal.toString()),
],
),
],
),
);
}
Widget _buildStatItem(String label, String value) {
return Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
value,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
const SizedBox(height: 4),
Text(label, style: const TextStyle(fontSize: 12, color: Colors.grey)),
],
),
);
}
}

View File

@@ -39,6 +39,8 @@ class _HomePageState extends State<HomePage>
late Animation<double> _fadeAnimation;
late Animation<Offset> _slideAnimation;
final GlobalKey _repaintKey = GlobalKey();
// 动态加载状态
bool _isLoadingNext = false;
Map<String, bool> _sectionLoadingStates = {
@@ -724,6 +726,15 @@ class _HomePageState extends State<HomePage>
}
}
Future<void> _sharePoetryImage() async {
if (_poetryData == null) return;
await ShareImageUtils.captureAndShare(
context,
_repaintKey,
subject: _poetryData!.name,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -773,6 +784,7 @@ class _HomePageState extends State<HomePage>
keywordList: _keywordList,
onTap: _loadNextPoetry,
sectionLoadingStates: _sectionLoadingStates,
repaintKey: _repaintKey,
),
const SizedBox(height: 160), // 为悬浮按钮留出更多空间
],
@@ -799,6 +811,12 @@ class _HomePageState extends State<HomePage>
bottom: 32,
child: FloatingNextButton(onNext: _loadNextPoetry),
),
// 悬浮分享按钮 - 右边上方
Positioned(
right: 16,
bottom: 100,
child: FloatingShareButton(onShare: _sharePoetryImage),
),
// 悬浮点赞按钮 - 右边(仅在线状态显示)
if (isOnline)
Positioned(

View File

@@ -17,6 +17,7 @@ class PoetryCard extends StatefulWidget {
final List<String> keywordList;
final VoidCallback? onTap;
final Map<String, bool>? sectionLoadingStates;
final GlobalKey? repaintKey;
const PoetryCard({
super.key,
@@ -24,6 +25,7 @@ class PoetryCard extends StatefulWidget {
required this.keywordList,
this.onTap,
this.sectionLoadingStates,
this.repaintKey,
});
@override
@@ -104,7 +106,7 @@ class _PoetryCardState extends State<PoetryCard> {
@override
Widget build(BuildContext context) {
return GestureDetector(
final card = GestureDetector(
onTap: () {
// 播放点击音效(不等待完成)
AudioManager().playClickSound();
@@ -160,6 +162,11 @@ class _PoetryCardState extends State<PoetryCard> {
],
),
);
if (widget.repaintKey != null) {
return RepaintBoundary(key: widget.repaintKey, child: card);
}
return card;
}
Widget _buildTimeBar() {

View File

@@ -21,9 +21,9 @@ class PopMenu extends StatelessWidget {
static Future<void> shareApp(BuildContext context) async {
try {
const String shareText = '诗词学习App - 一款优雅的诗词学习应用,包含丰富的诗词内容和答题功能';
const String shareText = '情景诗词App - 一款优雅的诗词学习应用,包含丰富的诗词内容和答题功能';
final result = await Share.shareWithResult(shareText, subject: '诗词学习App');
final result = await Share.shareWithResult(shareText, subject: '情景诗词App');
if (result.status == ShareResultStatus.success) {
if (context.mounted) {