深色模式、首页设置页面和功能优化

This commit is contained in:
Developer
2026-04-02 07:06:55 +08:00
parent f0a62ed68b
commit 954d173329
88 changed files with 12157 additions and 7578 deletions

View File

@@ -1,15 +1,14 @@
/// 时间: 2025-03-22
/// 功能: 诗词页面组件部分
/// 介绍: 包含诗词卡片、操作按钮、统计信息等组件的独立文件
/// 最新变化: 重构代码结构,使用工具类简化代码
/// 最新变化: 2026-04-02 支持深色模式
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../constants/app_constants.dart';
import '../../../utils/http/poetry_api.dart';
import '../../../utils/audio_manager.dart';
import 'home_components.dart';
import 'set/home_components.dart';
/// 诗词卡片组件 - 优化版本,防止拉伸和处理文本溢出
class PoetryCard extends StatefulWidget {
@@ -18,6 +17,7 @@ class PoetryCard extends StatefulWidget {
final VoidCallback? onTap;
final Map<String, bool>? sectionLoadingStates;
final GlobalKey? repaintKey;
final bool isDark;
const PoetryCard({
super.key,
@@ -26,6 +26,7 @@ class PoetryCard extends StatefulWidget {
this.onTap,
this.sectionLoadingStates,
this.repaintKey,
this.isDark = false,
});
@override
@@ -35,12 +36,12 @@ class PoetryCard extends StatefulWidget {
class _PoetryCardState extends State<PoetryCard> {
bool _showCopyTip = true;
bool _showRecommendation = false;
bool _globalTipsEnabled = true; // 添加全局Tips开关状态
bool _globalTipsEnabled = true;
@override
void initState() {
super.initState();
_loadGlobalTipsSettings(); // 加载全局Tips设置
_loadGlobalTipsSettings();
}
String _getTimeOfDayGreeting() {
@@ -85,7 +86,6 @@ class _PoetryCardState extends State<PoetryCard> {
}
}
// 加载全局Tips设置
Future<void> _loadGlobalTipsSettings() async {
try {
final prefs = await SharedPreferences.getInstance();
@@ -95,7 +95,6 @@ class _PoetryCardState extends State<PoetryCard> {
});
}
} catch (e) {
// 如果加载失败,默认开启
if (mounted) {
setState(() {
_globalTipsEnabled = true;
@@ -106,11 +105,10 @@ class _PoetryCardState extends State<PoetryCard> {
@override
Widget build(BuildContext context) {
final isDark = widget.isDark;
final card = GestureDetector(
onTap: () {
// 播放点击音效(不等待完成)
AudioManager().playClickSound();
// 调用原始的onTap回调
widget.onTap?.call();
},
child: Stack(
@@ -118,11 +116,11 @@ class _PoetryCardState extends State<PoetryCard> {
Container(
margin: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
color: isDark ? const Color(0xFF1E1E1E) : Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(10),
color: Colors.black.withAlpha(isDark ? 40 : 10),
blurRadius: 10,
offset: const Offset(0, 2),
),
@@ -131,19 +129,18 @@ class _PoetryCardState extends State<PoetryCard> {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_buildTimeBar(),
_buildTimeBar(isDark),
Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
_buildTitleSection(),
_buildTitleSection(isDark),
if (widget.poetryData.drtime.isNotEmpty) ...[
_buildContentSection(context),
_buildContentSection(context, isDark),
const SizedBox(height: 3),
],
// 精选诗句标签 - 放在缝隙位置
Align(
alignment: Alignment.centerRight,
child: Container(
@@ -173,14 +170,14 @@ class _PoetryCardState extends State<PoetryCard> {
),
),
),
_buildNameSection(),
_buildNameSection(isDark),
const SizedBox(height: 12),
if (widget.keywordList.isNotEmpty) ...[
_buildKeywordSection(),
_buildKeywordSection(isDark),
const SizedBox(height: 16),
],
if (widget.poetryData.introduce.isNotEmpty) ...[
_buildIntroductionSection(context),
_buildIntroductionSection(context, isDark),
],
],
),
@@ -188,7 +185,7 @@ class _PoetryCardState extends State<PoetryCard> {
],
),
),
_buildCopyTip(),
_buildCopyTip(isDark),
],
),
);
@@ -199,7 +196,7 @@ class _PoetryCardState extends State<PoetryCard> {
return card;
}
Widget _buildTimeBar() {
Widget _buildTimeBar(bool isDark) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
@@ -254,7 +251,7 @@ class _PoetryCardState extends State<PoetryCard> {
);
}
Widget _buildCopyTip() {
Widget _buildCopyTip(bool isDark) {
if (!_globalTipsEnabled || !_showCopyTip) {
return const SizedBox.shrink();
}
@@ -299,7 +296,7 @@ class _PoetryCardState extends State<PoetryCard> {
);
}
Widget _buildTitleSection() {
Widget _buildTitleSection(bool isDark) {
final isLoading = widget.sectionLoadingStates?['title'] ?? false;
return SizedBox(
@@ -313,6 +310,7 @@ class _PoetryCardState extends State<PoetryCard> {
context,
widget.poetryData.url,
'诗人和标题',
isDark: isDark,
),
child: isLoading
? Row(
@@ -332,7 +330,9 @@ class _PoetryCardState extends State<PoetryCard> {
'出处加载中...',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
color: isDark
? Colors.grey[400]
: Colors.grey[600],
fontStyle: FontStyle.italic,
),
),
@@ -340,10 +340,10 @@ class _PoetryCardState extends State<PoetryCard> {
)
: Text(
"出处: ${widget.poetryData.url}",
style: const TextStyle(
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.black,
color: isDark ? Colors.grey[200] : Colors.black,
fontStyle: FontStyle.italic,
),
overflow: TextOverflow.ellipsis,
@@ -357,7 +357,7 @@ class _PoetryCardState extends State<PoetryCard> {
);
}
Widget _buildNameSection() {
Widget _buildNameSection(bool isDark) {
final isLoading = widget.sectionLoadingStates?['name'] ?? false;
return Container(
@@ -366,21 +366,27 @@ class _PoetryCardState extends State<PoetryCard> {
margin: EdgeInsets.zero,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
AppConstants.primaryColor.withAlpha(26),
AppConstants.primaryColor.withAlpha(13),
],
colors: isDark
? [const Color(0xFF2A2A2A), const Color(0xFF252525)]
: [
AppConstants.primaryColor.withAlpha(26),
AppConstants.primaryColor.withAlpha(13),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: AppConstants.primaryColor.withAlpha(51),
color: isDark
? Colors.grey[700]!
: AppConstants.primaryColor.withAlpha(51),
width: 1,
),
boxShadow: [
BoxShadow(
color: AppConstants.primaryColor.withAlpha(26),
color: isDark
? Colors.black.withAlpha(40)
: AppConstants.primaryColor.withAlpha(26),
blurRadius: 8,
offset: const Offset(0, 2),
),
@@ -390,8 +396,12 @@ class _PoetryCardState extends State<PoetryCard> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
GestureDetector(
onLongPress: () =>
CopyUtils.showCopyDialog(context, widget.poetryData.name, '诗词'),
onLongPress: () => CopyUtils.showCopyDialog(
context,
widget.poetryData.name,
'诗词',
isDark: isDark,
),
child: isLoading
? Center(
child: Column(
@@ -411,7 +421,7 @@ class _PoetryCardState extends State<PoetryCard> {
'诗句加载中...',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
),
],
@@ -419,10 +429,10 @@ class _PoetryCardState extends State<PoetryCard> {
)
: Text(
_formatPoetryText(widget.poetryData.name),
style: const TextStyle(
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.black87,
color: isDark ? Colors.grey[100] : Colors.black87,
height: 1.4,
),
textAlign: TextAlign.center,
@@ -434,30 +444,25 @@ class _PoetryCardState extends State<PoetryCard> {
}
String _formatPoetryText(String text) {
// 检查文本长度(文字+符号)
if (text.length < 10) {
return text; // 小于10个字不换行
return text;
}
// 找到第一个逗号的位置
final commaIndex = text.indexOf('');
if (commaIndex != -1) {
// 在第一个逗号处添加换行
return text.replaceFirst('', '\n');
}
return text; // 没有逗号,保持原样
return text;
}
Widget _buildKeywordSection() {
Widget _buildKeywordSection(bool isDark) {
final isLoading = widget.sectionLoadingStates?['keywords'] ?? false;
return Column(
children: [
// 第一行关键词和朝代左边vs 星星和点赞(右边)
Row(
children: [
// 左边:关键词和朝代
Expanded(
child: isLoading
? Center(
@@ -479,7 +484,9 @@ class _PoetryCardState extends State<PoetryCard> {
'关键词加载中...',
style: TextStyle(
fontSize: 12,
color: Colors.grey[600],
color: isDark
? Colors.grey[400]
: Colors.grey[600],
),
),
],
@@ -490,39 +497,43 @@ class _PoetryCardState extends State<PoetryCard> {
runSpacing: 4,
children: [
if (widget.keywordList.isNotEmpty)
...widget.keywordList
.map(
(keyword) => Builder(
builder: (context) => GestureDetector(
onLongPress: () => CopyUtils.showCopyDialog(
context,
keyword,
'关键词',
),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: AppConstants.secondaryColor
.withAlpha(26),
borderRadius: BorderRadius.circular(8),
),
child: Text(
keyword,
style: TextStyle(
color: AppConstants.secondaryColor,
fontSize: 12,
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
...widget.keywordList.map(
(keyword) => Builder(
builder: (context) => GestureDetector(
onLongPress: () => CopyUtils.showCopyDialog(
context,
keyword,
'关键词',
isDark: isDark,
),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: isDark
? AppConstants.secondaryColor.withAlpha(
40,
)
: AppConstants.secondaryColor.withAlpha(
26,
),
borderRadius: BorderRadius.circular(8),
),
child: Text(
keyword,
style: TextStyle(
color: AppConstants.secondaryColor,
fontSize: 12,
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
)
.toList(),
),
),
),
if (widget.poetryData.alias.isNotEmpty)
Builder(
builder: (context) => GestureDetector(
@@ -530,6 +541,7 @@ class _PoetryCardState extends State<PoetryCard> {
context,
widget.poetryData.alias,
'朝代',
isDark: isDark,
),
child: Container(
padding: const EdgeInsets.symmetric(
@@ -537,9 +549,9 @@ class _PoetryCardState extends State<PoetryCard> {
vertical: 4,
),
decoration: BoxDecoration(
color: AppConstants.primaryColor.withAlpha(
26,
),
color: isDark
? AppConstants.primaryColor.withAlpha(40)
: AppConstants.primaryColor.withAlpha(26),
borderRadius: BorderRadius.circular(12),
),
child: Text(
@@ -558,7 +570,6 @@ class _PoetryCardState extends State<PoetryCard> {
],
),
),
// 右边:星星和点赞
if (!isLoading)
Row(
mainAxisSize: MainAxisSize.min,
@@ -570,14 +581,20 @@ class _PoetryCardState extends State<PoetryCard> {
const SizedBox(width: 4),
Text(
PoetryDataUtils.generateLikeText(widget.poetryData.like),
style: const TextStyle(fontSize: 12, color: Colors.grey),
style: TextStyle(
fontSize: 12,
color: isDark ? Colors.grey[400] : Colors.grey,
),
),
const SizedBox(width: 4),
Text(
PoetryDataUtils.generateViewText(
widget.poetryData.hitsTotal,
),
style: const TextStyle(fontSize: 12, color: Colors.grey),
style: TextStyle(
fontSize: 12,
color: isDark ? Colors.grey[400] : Colors.grey,
),
),
],
),
@@ -587,344 +604,162 @@ class _PoetryCardState extends State<PoetryCard> {
);
}
Widget _buildContentSection(BuildContext context) {
Widget _buildContentSection(BuildContext context, bool isDark) {
final isLoading = widget.sectionLoadingStates?['content'] ?? false;
return GestureDetector(
onLongPress: () =>
CopyUtils.showCopyDialog(context, widget.poetryData.drtime, '原文'),
child: Container(
width: double.infinity,
constraints: const BoxConstraints(maxHeight: 200),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFF8F8F8),
borderRadius: BorderRadius.circular(12),
return Builder(
builder: (context) => GestureDetector(
onLongPress: () => CopyUtils.showCopyDialog(
context,
widget.poetryData.drtime,
'时间',
isDark: isDark,
),
child: isLoading
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
AppConstants.primaryColor,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
margin: const EdgeInsets.symmetric(vertical: 4),
decoration: BoxDecoration(
color: isDark
? Colors.grey[800]!.withAlpha(80)
: AppConstants.primaryColor.withAlpha(20),
borderRadius: BorderRadius.circular(8),
),
child: isLoading
? Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
AppConstants.primaryColor,
),
),
),
const SizedBox(width: 8),
Text(
'时间加载中...',
style: TextStyle(
fontSize: 14,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
),
],
),
)
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'诗词原文',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: isDark
? Colors.grey[300]
: AppConstants.primaryColor,
),
),
const SizedBox(height: 8),
Text(
'原文加载中...',
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
widget.poetryData.drtime,
style: TextStyle(
fontSize: 14,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
textAlign: TextAlign.center,
),
],
),
)
: SingleChildScrollView(
child: Text(
widget.poetryData.drtime,
style: const TextStyle(
fontSize: 16,
height: 1.6,
color: Colors.black87,
),
),
),
),
),
);
}
Widget _buildIntroductionSection(BuildContext context) {
Widget _buildIntroductionSection(BuildContext context, bool isDark) {
final isLoading = widget.sectionLoadingStates?['introduction'] ?? false;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'译文',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: AppConstants.primaryColor,
),
return Builder(
builder: (context) => GestureDetector(
onLongPress: () => CopyUtils.showCopyDialog(
context,
widget.poetryData.introduce,
'简介',
isDark: isDark,
),
const SizedBox(height: 8),
GestureDetector(
onLongPress: () => CopyUtils.showCopyDialog(
context,
widget.poetryData.introduce,
'译文',
),
child: Container(
width: double.infinity,
constraints: const BoxConstraints(maxHeight: 150),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AppConstants.infoColor.withAlpha(13),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: AppConstants.infoColor.withAlpha(51)),
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: isDark ? const Color(0xFF252525) : Colors.grey[50],
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: isDark ? Colors.grey[700]! : Colors.grey[200]!,
),
child: isLoading
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
AppConstants.primaryColor,
),
),
child: isLoading
? Center(
child: Column(
children: [
SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
AppConstants.primaryColor,
),
),
const SizedBox(height: 8),
),
const SizedBox(height: 8),
Text(
'简介加载中...',
style: TextStyle(
fontSize: 14,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
),
],
),
)
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.info_outline,
size: 16,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
const SizedBox(width: 4),
Text(
'译文加载中...',
'简介',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
fontWeight: FontWeight.w500,
color: isDark ? Colors.grey[300] : Colors.grey[700],
),
),
],
),
)
: SingleChildScrollView(
child: Text(
const SizedBox(height: 8),
Text(
widget.poetryData.introduce,
style: const TextStyle(
style: TextStyle(
fontSize: 14,
height: 1.6,
color: Colors.black87,
height: 1.5,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
),
),
),
),
],
);
}
}
/// 悬浮上一条按钮组件
class FloatingPreviousButton extends StatelessWidget {
final VoidCallback onPrevious;
const FloatingPreviousButton({super.key, required this.onPrevious});
@override
Widget build(BuildContext context) {
return Tooltip(
message: 'beta功能',
child: 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();
AudioManager().playClickSound();
onPrevious();
},
child: const Center(
child: Icon(Icons.arrow_back, color: Colors.white, size: 28),
),
),
],
),
),
),
);
}
}
/// 悬浮下一条按钮组件
class FloatingNextButton extends StatelessWidget {
final VoidCallback onNext;
const FloatingNextButton({super.key, required this.onNext});
@override
Widget build(BuildContext context) {
return Container(
width: 56,
height: 56,
decoration: BoxDecoration(
color: AppConstants.primaryColor,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: AppConstants.primaryColor.withAlpha(76),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius: BorderRadius.circular(28),
onTap: () {
HapticFeedback.lightImpact();
AudioManager().playNextSound();
onNext();
},
child: const Center(
child: Icon(Icons.arrow_forward, color: Colors.white, size: 28),
),
),
),
);
}
}
/// 悬浮点赞按钮组件
class FloatingLikeButton extends StatelessWidget {
final bool isLiked;
final bool isLoadingLike;
final VoidCallback onToggleLike;
const FloatingLikeButton({
super.key,
required this.isLiked,
required this.isLoadingLike,
required this.onToggleLike,
});
@override
Widget build(BuildContext context) {
return Container(
width: 56,
height: 56,
decoration: BoxDecoration(
color: isLiked ? AppConstants.errorColor : AppConstants.primaryColor,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color:
(isLiked ? AppConstants.errorColor : AppConstants.primaryColor)
.withAlpha(76),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius: BorderRadius.circular(28),
onTap: isLoadingLike
? null
: () {
HapticFeedback.mediumImpact();
AudioManager().playLikeSound();
onToggleLike();
},
child: Center(
child: isLoadingLike
? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white70),
),
)
: Icon(
isLiked ? Icons.favorite : Icons.favorite_border,
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)),
],
),
);
}
}