深色模式、首页设置页面和功能优化
This commit is contained in:
394
lib/views/home/set/home-load.dart
Normal file
394
lib/views/home/set/home-load.dart
Normal file
@@ -0,0 +1,394 @@
|
||||
/// 时间: 2026-03-29
|
||||
/// 功能: 首页自动刷新管理
|
||||
/// 介绍: 管理首页诗句自动刷新功能,包括定时器、状态管理等
|
||||
/// 最新变化: 新建文件
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../../../utils/http/poetry_api.dart';
|
||||
import '../../../../constants/app_constants.dart';
|
||||
|
||||
class SecondaryButtonsManager {
|
||||
static const String _hideSecondaryButtonsKey = 'hide_secondary_buttons';
|
||||
|
||||
static SecondaryButtonsManager? _instance;
|
||||
bool _isHidden = false;
|
||||
final ValueNotifier<bool> _hiddenNotifier = ValueNotifier<bool>(false);
|
||||
|
||||
SecondaryButtonsManager._internal();
|
||||
|
||||
factory SecondaryButtonsManager() {
|
||||
_instance ??= SecondaryButtonsManager._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isHidden = prefs.getBool(_hideSecondaryButtonsKey) ?? false;
|
||||
_hiddenNotifier.value = _isHidden;
|
||||
}
|
||||
|
||||
bool get isHidden => _isHidden;
|
||||
ValueNotifier<bool> get hiddenNotifier => _hiddenNotifier;
|
||||
|
||||
Future<void> setHidden(bool hidden) async {
|
||||
_isHidden = hidden;
|
||||
_hiddenNotifier.value = hidden;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_hideSecondaryButtonsKey, hidden);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_hiddenNotifier.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
class AutoRefreshManager {
|
||||
static const String _autoRefreshKey = 'auto_refresh_enabled';
|
||||
static const Duration _refreshInterval = Duration(seconds: 5);
|
||||
|
||||
static AutoRefreshManager? _instance;
|
||||
Timer? _refreshTimer;
|
||||
bool _isEnabled = false;
|
||||
VoidCallback? _onRefresh;
|
||||
final ValueNotifier<bool> _enabledNotifier = ValueNotifier<bool>(false);
|
||||
|
||||
AutoRefreshManager._internal();
|
||||
|
||||
factory AutoRefreshManager() {
|
||||
_instance ??= AutoRefreshManager._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isEnabled = prefs.getBool(_autoRefreshKey) ?? false;
|
||||
_enabledNotifier.value = _isEnabled;
|
||||
|
||||
if (_isEnabled) {
|
||||
_startTimer();
|
||||
}
|
||||
}
|
||||
|
||||
bool get isEnabled => _isEnabled;
|
||||
ValueNotifier<bool> get enabledNotifier => _enabledNotifier;
|
||||
|
||||
Future<void> setEnabled(bool enabled) async {
|
||||
_isEnabled = enabled;
|
||||
_enabledNotifier.value = enabled;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_autoRefreshKey, enabled);
|
||||
|
||||
if (enabled) {
|
||||
_startTimer();
|
||||
} else {
|
||||
_stopTimer();
|
||||
}
|
||||
}
|
||||
|
||||
void setOnRefresh(VoidCallback? callback) {
|
||||
_onRefresh = callback;
|
||||
}
|
||||
|
||||
void _startTimer() {
|
||||
_stopTimer();
|
||||
_refreshTimer = Timer.periodic(_refreshInterval, (timer) {
|
||||
if (_onRefresh != null) {
|
||||
_onRefresh!();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _stopTimer() {
|
||||
if (_refreshTimer != null) {
|
||||
_refreshTimer!.cancel();
|
||||
_refreshTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
void stopTimer() {
|
||||
_stopTimer();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_stopTimer();
|
||||
_onRefresh = null;
|
||||
}
|
||||
}
|
||||
|
||||
class DebugInfoManager {
|
||||
static const String _debugInfoKey = 'debug_info_enabled';
|
||||
|
||||
static DebugInfoManager? _instance;
|
||||
bool _isEnabled = false;
|
||||
final ValueNotifier<bool> _enabledNotifier = ValueNotifier<bool>(false);
|
||||
|
||||
DebugInfoManager._internal();
|
||||
|
||||
factory DebugInfoManager() {
|
||||
_instance ??= DebugInfoManager._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isEnabled = prefs.getBool(_debugInfoKey) ?? false;
|
||||
_enabledNotifier.value = _isEnabled;
|
||||
}
|
||||
|
||||
bool get isEnabled => _isEnabled;
|
||||
ValueNotifier<bool> get enabledNotifier => _enabledNotifier;
|
||||
|
||||
Future<void> setEnabled(bool enabled) async {
|
||||
_isEnabled = enabled;
|
||||
_enabledNotifier.value = enabled;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_debugInfoKey, enabled);
|
||||
}
|
||||
|
||||
void showMessage(String message) {
|
||||
if (!_isEnabled) return;
|
||||
|
||||
Get.snackbar(
|
||||
'调试信息',
|
||||
message,
|
||||
snackPosition: SnackPosition.TOP,
|
||||
backgroundColor: Colors.transparent,
|
||||
colorText: Colors.white,
|
||||
duration: const Duration(seconds: 2),
|
||||
margin: const EdgeInsets.all(16),
|
||||
borderRadius: 20,
|
||||
animationDuration: const Duration(milliseconds: 300),
|
||||
snackbarStatus: (status) {},
|
||||
maxWidth: Get.width - 32,
|
||||
overlayBlur: 0.1,
|
||||
overlayColor: Colors.transparent,
|
||||
isDismissible: true,
|
||||
padding: EdgeInsets.zero,
|
||||
shouldIconPulse: false,
|
||||
barBlur: 20,
|
||||
// backgroundColor: Colors.black.withValues(alpha: 0.15),
|
||||
);
|
||||
}
|
||||
|
||||
void showRefreshSuccess() {
|
||||
showMessage('刷新成功');
|
||||
}
|
||||
|
||||
void showRefreshFailed() {
|
||||
showMessage('刷新失败');
|
||||
}
|
||||
|
||||
void showNextSuccess() {
|
||||
showMessage('下一条成功');
|
||||
}
|
||||
|
||||
void showNextFailed() {
|
||||
showMessage('下一条失败');
|
||||
}
|
||||
|
||||
void showPreviousSuccess() {
|
||||
showMessage('上一条成功');
|
||||
}
|
||||
|
||||
void showPreviousFailed() {
|
||||
showMessage('上一条失败');
|
||||
}
|
||||
|
||||
void showLiked() {
|
||||
showMessage('已点赞');
|
||||
}
|
||||
|
||||
void showUnliked() {
|
||||
showMessage('已取消点赞');
|
||||
}
|
||||
|
||||
void showCopySuccess() {
|
||||
showMessage('复制成功');
|
||||
}
|
||||
|
||||
void showCopyFailed() {
|
||||
showMessage('复制失败');
|
||||
}
|
||||
|
||||
void dispose() {}
|
||||
}
|
||||
|
||||
class OfflineDataManager {
|
||||
static const String _offlineDataKey = 'offline_poetry_data';
|
||||
static const String _onlineStatusKey = 'personal_card_online';
|
||||
|
||||
static OfflineDataManager? _instance;
|
||||
List<Map<String, dynamic>> _cachedPoetryList = [];
|
||||
int _currentIndex = 0;
|
||||
|
||||
OfflineDataManager._internal();
|
||||
|
||||
factory OfflineDataManager() {
|
||||
_instance ??= OfflineDataManager._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
await _loadCachedData();
|
||||
}
|
||||
|
||||
Future<bool> isOnline() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getBool(_onlineStatusKey) ?? true;
|
||||
}
|
||||
|
||||
Future<bool> hasCachedData() async {
|
||||
await _loadCachedData();
|
||||
return _cachedPoetryList.isNotEmpty;
|
||||
}
|
||||
|
||||
Future<PoetryData?> getNextPoetry() async {
|
||||
await _loadCachedData();
|
||||
|
||||
if (_cachedPoetryList.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final poetryData = _cachedPoetryList[_currentIndex];
|
||||
_currentIndex = (_currentIndex + 1) % _cachedPoetryList.length;
|
||||
|
||||
return _convertToPoetryData(poetryData);
|
||||
}
|
||||
|
||||
Future<PoetryData?> getPreviousPoetry() async {
|
||||
await _loadCachedData();
|
||||
|
||||
if (_cachedPoetryList.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
_currentIndex =
|
||||
(_currentIndex - 1 + _cachedPoetryList.length) %
|
||||
_cachedPoetryList.length;
|
||||
final poetryData = _cachedPoetryList[_currentIndex];
|
||||
|
||||
return _convertToPoetryData(poetryData);
|
||||
}
|
||||
|
||||
Future<void> _loadCachedData() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final cachedData = prefs.getStringList(_offlineDataKey) ?? [];
|
||||
|
||||
_cachedPoetryList = [];
|
||||
for (final item in cachedData) {
|
||||
try {
|
||||
// 移除 Map 两边的大括号,然后分割键值对
|
||||
final cleanItem = item.substring(1, item.length - 1);
|
||||
final keyValuePairs = cleanItem.split(', ');
|
||||
final map = <String, dynamic>{};
|
||||
|
||||
for (final pair in keyValuePairs) {
|
||||
final parts = pair.split(': ');
|
||||
if (parts.length == 2) {
|
||||
final key = parts[0].replaceAll('"', '');
|
||||
var value = parts[1].replaceAll('"', '');
|
||||
|
||||
// 尝试转换数字
|
||||
if (int.tryParse(value) != null) {
|
||||
map[key] = int.parse(value);
|
||||
} else if (double.tryParse(value) != null) {
|
||||
map[key] = double.parse(value);
|
||||
} else {
|
||||
map[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_cachedPoetryList.add(map);
|
||||
} catch (e) {
|
||||
// 解析失败
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PoetryData? _convertToPoetryData(Map<String, dynamic> data) {
|
||||
try {
|
||||
final id = data['id'] ?? 0;
|
||||
final name = data['name'] ?? '';
|
||||
final alias = data['alias'] ?? '';
|
||||
final keywords = data['keywords'] ?? '';
|
||||
final introduce = data['introduce'] ?? '';
|
||||
final drtime = data['drtime'] ?? '';
|
||||
final like = data['like'] ?? 0;
|
||||
final url = data['url'] ?? '';
|
||||
final tui = data['tui'] ?? 0;
|
||||
final star = data['star'] ?? 5;
|
||||
final hitsTotal = data['hitsTotal'] ?? 0;
|
||||
final hitsMonth = data['hitsMonth'] ?? 0;
|
||||
final hitsDay = data['hitsDay'] ?? 0;
|
||||
final date = data['date'] ?? '';
|
||||
final datem = data['datem'] ?? '';
|
||||
final time = data['time'] ?? '';
|
||||
final createTime = data['createTime'] ?? '';
|
||||
final updateTime = data['updateTime'] ?? '';
|
||||
|
||||
return PoetryData(
|
||||
id: id,
|
||||
name: name,
|
||||
alias: alias,
|
||||
keywords: keywords,
|
||||
introduce: introduce,
|
||||
drtime: drtime,
|
||||
like: like,
|
||||
url: url,
|
||||
tui: tui,
|
||||
star: star,
|
||||
hitsTotal: hitsTotal,
|
||||
hitsMonth: hitsMonth,
|
||||
hitsDay: hitsDay,
|
||||
date: date,
|
||||
datem: datem,
|
||||
time: time,
|
||||
createTime: createTime,
|
||||
updateTime: updateTime,
|
||||
);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalTipsManager {
|
||||
static const String _globalTipsKey = 'global_tips_enabled';
|
||||
|
||||
static GlobalTipsManager? _instance;
|
||||
bool _isEnabled = true;
|
||||
final ValueNotifier<bool> _enabledNotifier = ValueNotifier<bool>(true);
|
||||
|
||||
GlobalTipsManager._internal();
|
||||
|
||||
factory GlobalTipsManager() {
|
||||
_instance ??= GlobalTipsManager._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isEnabled = prefs.getBool(_globalTipsKey) ?? true;
|
||||
_enabledNotifier.value = _isEnabled;
|
||||
}
|
||||
|
||||
bool get isEnabled => _isEnabled;
|
||||
ValueNotifier<bool> get enabledNotifier => _enabledNotifier;
|
||||
|
||||
Future<void> setEnabled(bool enabled) async {
|
||||
_isEnabled = enabled;
|
||||
_enabledNotifier.value = enabled;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_globalTipsKey, enabled);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_enabledNotifier.value = true;
|
||||
}
|
||||
}
|
||||
190
lib/views/home/set/home-set.dart
Normal file
190
lib/views/home/set/home-set.dart
Normal file
@@ -0,0 +1,190 @@
|
||||
/// 时间: 2026-04-02
|
||||
/// 功能: 主页设置和控制组件
|
||||
/// 介绍: 包含收起/恢复悬浮按钮的管理器和组件
|
||||
/// 最新变化: 2026-04-02 初始创建
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../../../constants/app_constants.dart';
|
||||
|
||||
/// 悬浮按钮收起管理器
|
||||
class FloatingButtonsVisibilityManager {
|
||||
static const String _key = 'floating_buttons_visible';
|
||||
|
||||
static FloatingButtonsVisibilityManager? _instance;
|
||||
bool _isVisible = true;
|
||||
bool _isFlashing = false;
|
||||
int _flashCount = 0;
|
||||
final ValueNotifier<bool> _visibleNotifier = ValueNotifier<bool>(true);
|
||||
final ValueNotifier<bool> _flashingNotifier = ValueNotifier<bool>(false);
|
||||
Timer? _hideTimer;
|
||||
Timer? _flashTimer;
|
||||
|
||||
FloatingButtonsVisibilityManager._internal();
|
||||
|
||||
factory FloatingButtonsVisibilityManager() {
|
||||
_instance ??= FloatingButtonsVisibilityManager._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isVisible = prefs.getBool(_key) ?? true;
|
||||
_visibleNotifier.value = _isVisible;
|
||||
}
|
||||
|
||||
bool get isVisible => _isVisible;
|
||||
bool get isFlashing => _isFlashing;
|
||||
ValueNotifier<bool> get visibleNotifier => _visibleNotifier;
|
||||
ValueNotifier<bool> get flashingNotifier => _flashingNotifier;
|
||||
|
||||
Future<void> toggle() async {
|
||||
if (_isFlashing) {
|
||||
await _restoreFromFlashing();
|
||||
return;
|
||||
}
|
||||
|
||||
_isVisible = !_isVisible;
|
||||
_visibleNotifier.value = _isVisible;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_key, _isVisible);
|
||||
|
||||
if (!_isVisible) {
|
||||
_startHideTimer();
|
||||
} else {
|
||||
_cancelAllTimers();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _restoreFromFlashing() async {
|
||||
_cancelAllTimers();
|
||||
_isFlashing = false;
|
||||
_flashingNotifier.value = false;
|
||||
_isVisible = true;
|
||||
_visibleNotifier.value = true;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_key, true);
|
||||
}
|
||||
|
||||
void _startHideTimer() {
|
||||
_cancelAllTimers();
|
||||
_hideTimer = Timer(const Duration(seconds: 5), () {
|
||||
_startFlashing();
|
||||
});
|
||||
}
|
||||
|
||||
void _startFlashing() {
|
||||
_isFlashing = true;
|
||||
_flashingNotifier.value = true;
|
||||
_flashCount = 0;
|
||||
_flashTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
|
||||
_flashCount++;
|
||||
_flashingNotifier.value = !_flashingNotifier.value;
|
||||
|
||||
if (_flashCount >= 6) {
|
||||
timer.cancel();
|
||||
_isFlashing = false;
|
||||
_flashingNotifier.value = false;
|
||||
_isVisible = false;
|
||||
_visibleNotifier.value = false;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_key, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _cancelAllTimers() {
|
||||
if (_hideTimer != null) {
|
||||
_hideTimer!.cancel();
|
||||
_hideTimer = null;
|
||||
}
|
||||
if (_flashTimer != null) {
|
||||
_flashTimer!.cancel();
|
||||
_flashTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_cancelAllTimers();
|
||||
_visibleNotifier.value = true;
|
||||
_flashingNotifier.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 收起/恢复悬浮按钮组件
|
||||
class FloatingButtonsToggleButton extends StatelessWidget {
|
||||
final FloatingButtonsVisibilityManager manager;
|
||||
final bool isDark;
|
||||
|
||||
const FloatingButtonsToggleButton({
|
||||
super.key,
|
||||
required this.manager,
|
||||
required this.isDark,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ValueListenableBuilder<bool>(
|
||||
valueListenable: manager.flashingNotifier,
|
||||
builder: (context, isFlashing, child) {
|
||||
return ValueListenableBuilder<bool>(
|
||||
valueListenable: manager.visibleNotifier,
|
||||
builder: (context, isVisible, child) {
|
||||
final shouldShow = isFlashing ? !isFlashing : isVisible;
|
||||
|
||||
return SizedBox(
|
||||
width: 44,
|
||||
height: 44,
|
||||
child: shouldShow || isFlashing
|
||||
? Container(
|
||||
decoration: BoxDecoration(
|
||||
color: isDark ? const Color(0xFF2A2A2A) : Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(isDark ? 40 : 20),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
onTap: () {
|
||||
manager.toggle();
|
||||
},
|
||||
child: Center(
|
||||
child: Icon(
|
||||
isFlashing
|
||||
? Icons.visibility
|
||||
: Icons.visibility_off,
|
||||
color: isFlashing
|
||||
? AppConstants.primaryColor
|
||||
: (isDark
|
||||
? Colors.grey[300]
|
||||
: AppConstants.primaryColor),
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
onTap: () {
|
||||
manager.toggle();
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
677
lib/views/home/set/home_components.dart
Normal file
677
lib/views/home/set/home_components.dart
Normal file
@@ -0,0 +1,677 @@
|
||||
/// 时间: 2025-03-22
|
||||
/// 功能: 诗词页面通用组件和工具函数
|
||||
/// 介绍: 从 home_page.dart 和 home_part.dart 中提取的公共组件,用于代码复用和简化
|
||||
/// 最新变化: 2026-04-02 支持深色模式
|
||||
|
||||
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:get/get.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';
|
||||
|
||||
/// 加载状态组件
|
||||
class LoadingWidget extends StatelessWidget {
|
||||
final bool isDark;
|
||||
|
||||
const LoadingWidget({super.key, this.isDark = false});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CircularProgressIndicator(
|
||||
valueColor: AlwaysStoppedAnimation<Color>(
|
||||
AppConstants.primaryColor,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'加载中...',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: isDark ? Colors.grey[400] : const Color(0xFF757575),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 截图和分享工具类
|
||||
class ShareImageUtils {
|
||||
static Future<void> captureAndShare(
|
||||
BuildContext context,
|
||||
GlobalKey repaintKey, {
|
||||
String? subject,
|
||||
}) async {
|
||||
try {
|
||||
Get.snackbar('提示', '正在生成图片...');
|
||||
|
||||
final boundary =
|
||||
repaintKey.currentContext?.findRenderObject()
|
||||
as RenderRepaintBoundary?;
|
||||
if (boundary == null) {
|
||||
Get.snackbar('错误', '生成图片失败');
|
||||
return;
|
||||
}
|
||||
|
||||
final image = await boundary.toImage(pixelRatio: 3.0);
|
||||
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
|
||||
if (byteData == null) {
|
||||
Get.snackbar('错误', '生成图片失败');
|
||||
return;
|
||||
}
|
||||
|
||||
final pngBytes = byteData.buffer.asUint8List();
|
||||
final directory = await getTemporaryDirectory();
|
||||
final file = File(
|
||||
'${directory.path}/poetry_${DateTime.now().millisecondsSinceEpoch}.png',
|
||||
);
|
||||
await file.writeAsBytes(pngBytes);
|
||||
|
||||
await Share.shareXFiles([XFile(file.path)], subject: subject ?? '诗词分享');
|
||||
|
||||
Get.snackbar(
|
||||
'成功',
|
||||
'分享成功!',
|
||||
|
||||
// snackPosition: SnackPosition.TOP,
|
||||
// backgroundColor: Colors.transparent,
|
||||
// colorText: Colors.white,
|
||||
// duration: const Duration(seconds: 2),
|
||||
// margin: const EdgeInsets.all(16),
|
||||
// borderRadius: 20,
|
||||
// animationDuration: const Duration(milliseconds: 300),
|
||||
// maxWidth: Get.width - 32,
|
||||
// overlayBlur: 0.1,
|
||||
// overlayColor: Colors.transparent,
|
||||
// isDismissible: true,
|
||||
// padding: EdgeInsets.zero,
|
||||
// shouldIconPulse: false,
|
||||
// barBlur: 20,
|
||||
);
|
||||
} catch (e) {
|
||||
Get.snackbar(
|
||||
'错误',
|
||||
'分享失败:${e.toString().substring(0, e.toString().length > 50 ? 50 : e.toString().length)}',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 错误状态组件
|
||||
class CustomErrorWidget extends StatelessWidget {
|
||||
final String errorMessage;
|
||||
final VoidCallback onRetry;
|
||||
final bool isDark;
|
||||
|
||||
const CustomErrorWidget({
|
||||
super.key,
|
||||
required this.errorMessage,
|
||||
required this.onRetry,
|
||||
this.isDark = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: isDark ? Colors.grey[600] : Colors.grey[400],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
errorMessage,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: isDark ? Colors.grey[400] : Colors.grey,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: onRetry,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppConstants.primaryColor,
|
||||
),
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 空状态组件
|
||||
class EmptyWidget extends StatelessWidget {
|
||||
final bool isDark;
|
||||
|
||||
const EmptyWidget({super.key, this.isDark = false});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'暂无诗词内容',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: isDark ? Colors.grey[400] : Colors.grey,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 诗词状态管理工具类
|
||||
class PoetryStateManager {
|
||||
static void setLoadingState(VoidCallback setState, bool loading) {
|
||||
setState();
|
||||
}
|
||||
|
||||
static void setErrorState(VoidCallback setState, String error) {
|
||||
setState();
|
||||
}
|
||||
|
||||
static void setSuccessState(VoidCallback setState, PoetryData data) {
|
||||
setState();
|
||||
}
|
||||
|
||||
static String formatErrorMessage(String error) {
|
||||
return error.toString().contains('HttpException')
|
||||
? error.toString().replaceAll('HttpException: ', '')
|
||||
: '获取诗词失败';
|
||||
}
|
||||
|
||||
static void showSnackBar(
|
||||
BuildContext context,
|
||||
String message, {
|
||||
Color? backgroundColor,
|
||||
Duration? duration,
|
||||
}) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
backgroundColor: backgroundColor ?? AppConstants.successColor,
|
||||
duration: duration ?? const Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static void triggerHapticFeedback() {
|
||||
HapticFeedback.lightImpact();
|
||||
}
|
||||
|
||||
static void triggerHapticFeedbackMedium() {
|
||||
HapticFeedback.mediumImpact();
|
||||
}
|
||||
}
|
||||
|
||||
/// 诗词数据工具类
|
||||
class PoetryDataUtils {
|
||||
static List<String> extractKeywords(PoetryData? poetryData) {
|
||||
return poetryData?.keywordList ?? [];
|
||||
}
|
||||
|
||||
static String getStarDisplay(PoetryData? poetryData) {
|
||||
return poetryData?.starDisplay ?? '';
|
||||
}
|
||||
|
||||
static String generateStars(int? starCount) {
|
||||
if (starCount == null) return '';
|
||||
final count = starCount > 5 ? 5 : starCount;
|
||||
if (count == 5) {
|
||||
return ' 🌟$count';
|
||||
} else {
|
||||
return ' ⭐$count';
|
||||
}
|
||||
}
|
||||
|
||||
static String generateLikeText(int? likeCount) {
|
||||
if (likeCount == null) return '';
|
||||
return ' ❤️ $likeCount';
|
||||
}
|
||||
|
||||
static String generateViewText(int? viewCount) {
|
||||
if (viewCount == null) return '';
|
||||
return ' 🔥 $viewCount';
|
||||
}
|
||||
|
||||
static bool isValidPoetryData(PoetryData? poetryData) {
|
||||
return poetryData != null && poetryData.name.isNotEmpty;
|
||||
}
|
||||
}
|
||||
|
||||
/// 动画工具类
|
||||
class AnimationUtils {
|
||||
static AnimationController createFadeController(TickerProvider vsync) {
|
||||
return AnimationController(
|
||||
duration: AppConstants.animationDurationMedium,
|
||||
vsync: vsync,
|
||||
);
|
||||
}
|
||||
|
||||
static AnimationController createSlideController(TickerProvider vsync) {
|
||||
return AnimationController(
|
||||
duration: AppConstants.animationDurationShort,
|
||||
vsync: vsync,
|
||||
);
|
||||
}
|
||||
|
||||
static Animation<double> createFadeAnimation(AnimationController controller) {
|
||||
return CurvedAnimation(parent: controller, curve: Curves.easeIn);
|
||||
}
|
||||
|
||||
static Animation<Offset> createSlideAnimation(
|
||||
AnimationController controller,
|
||||
) {
|
||||
return Tween<Offset>(
|
||||
begin: const Offset(0.0, 0.3),
|
||||
end: Offset.zero,
|
||||
).animate(CurvedAnimation(parent: controller, curve: Curves.easeOutCubic));
|
||||
}
|
||||
}
|
||||
|
||||
/// 复制工具类
|
||||
class CopyUtils {
|
||||
static void copyToClipboard(
|
||||
BuildContext context,
|
||||
String content,
|
||||
String contentType, {
|
||||
VoidCallback? onSuccess,
|
||||
bool isDark = false,
|
||||
}) {
|
||||
try {
|
||||
Clipboard.setData(ClipboardData(text: content));
|
||||
Get.snackbar('提示', '已复制$contentType');
|
||||
DebugInfoManager().showCopySuccess();
|
||||
onSuccess?.call();
|
||||
} catch (e) {
|
||||
Get.snackbar('错误', '复制失败');
|
||||
DebugInfoManager().showCopyFailed();
|
||||
}
|
||||
}
|
||||
|
||||
static void showCopyDialog(
|
||||
BuildContext context,
|
||||
String content,
|
||||
String contentType, {
|
||||
bool isDark = false,
|
||||
}) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: isDark ? const Color(0xFF1E1E1E) : Colors.white,
|
||||
title: Text(
|
||||
'复制$contentType',
|
||||
style: TextStyle(color: isDark ? Colors.white : Colors.black87),
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'受隐私权限约束,频繁写入剪切板需告知用户',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: isDark ? Colors.grey[300] : Colors.black87,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'预览内容:',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: isDark ? Colors.grey[400] : Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: isDark ? const Color(0xFF2A2A2A) : Colors.grey[100],
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(
|
||||
color: isDark ? Colors.grey[700]! : Colors.grey[300]!,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
content.length > 50
|
||||
? '${content.substring(0, 50)}...'
|
||||
: content,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: isDark ? Colors.grey[300] : Colors.black87,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(
|
||||
'返回',
|
||||
style: TextStyle(
|
||||
color: isDark ? Colors.grey[400] : Colors.grey[600],
|
||||
),
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
copyToClipboard(context, content, contentType, isDark: isDark);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppConstants.primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: Text('复制$contentType'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 悬浮分享按钮组件
|
||||
class FloatingShareButton extends StatelessWidget {
|
||||
final VoidCallback onShare;
|
||||
final bool isDark;
|
||||
|
||||
const FloatingShareButton({
|
||||
super.key,
|
||||
required this.onShare,
|
||||
this.isDark = false,
|
||||
});
|
||||
|
||||
@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 FloatingPreviousButton extends StatelessWidget {
|
||||
final VoidCallback onPrevious;
|
||||
final bool isDark;
|
||||
|
||||
const FloatingPreviousButton({
|
||||
super.key,
|
||||
required this.onPrevious,
|
||||
this.isDark = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: 56,
|
||||
height: 56,
|
||||
decoration: BoxDecoration(
|
||||
color: isDark ? const Color(0xFF2A2A2A) : Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(isDark ? 40 : 20),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(28),
|
||||
onTap: () {
|
||||
HapticFeedback.lightImpact();
|
||||
onPrevious();
|
||||
},
|
||||
child: Center(
|
||||
child: Icon(
|
||||
Icons.arrow_back,
|
||||
color: isDark ? Colors.grey[300] : AppConstants.primaryColor,
|
||||
size: 28,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 悬浮下一条按钮
|
||||
class FloatingNextButton extends StatelessWidget {
|
||||
final VoidCallback onNext;
|
||||
final bool isDark;
|
||||
|
||||
const FloatingNextButton({
|
||||
super.key,
|
||||
required this.onNext,
|
||||
this.isDark = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: 56,
|
||||
height: 56,
|
||||
decoration: BoxDecoration(
|
||||
color: isDark ? const Color(0xFF2A2A2A) : Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(isDark ? 40 : 20),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(28),
|
||||
onTap: () {
|
||||
HapticFeedback.lightImpact();
|
||||
onNext();
|
||||
},
|
||||
child: Center(
|
||||
child: Icon(
|
||||
Icons.arrow_forward,
|
||||
color: isDark ? Colors.grey[300] : AppConstants.primaryColor,
|
||||
size: 28,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 悬浮点赞按钮
|
||||
class FloatingLikeButton extends StatelessWidget {
|
||||
final bool isLiked;
|
||||
final bool isLoadingLike;
|
||||
final VoidCallback onToggleLike;
|
||||
final bool isDark;
|
||||
|
||||
const FloatingLikeButton({
|
||||
super.key,
|
||||
required this.isLiked,
|
||||
required this.isLoadingLike,
|
||||
required this.onToggleLike,
|
||||
this.isDark = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: 56,
|
||||
height: 56,
|
||||
decoration: BoxDecoration(
|
||||
color: isLiked
|
||||
? Colors.red
|
||||
: (isDark ? const Color(0xFF2A2A2A) : Colors.white),
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: (isLiked ? Colors.red : Colors.black).withAlpha(
|
||||
isDark ? 40 : 20,
|
||||
),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(28),
|
||||
onTap: isLoadingLike
|
||||
? null
|
||||
: () {
|
||||
HapticFeedback.lightImpact();
|
||||
onToggleLike();
|
||||
},
|
||||
child: Center(
|
||||
child: isLoadingLike
|
||||
? SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(
|
||||
isDark ? Colors.grey[300]! : AppConstants.primaryColor,
|
||||
),
|
||||
),
|
||||
)
|
||||
: Icon(
|
||||
isLiked ? Icons.favorite : Icons.favorite_border,
|
||||
color: isLiked
|
||||
? Colors.white
|
||||
: (isDark ? Colors.grey[300] : Colors.grey),
|
||||
size: 28,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 统计信息卡片组件
|
||||
class StatsCard extends StatelessWidget {
|
||||
final PoetryData poetryData;
|
||||
final bool isDark;
|
||||
|
||||
const StatsCard({super.key, required this.poetryData, this.isDark = false});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: isDark ? const Color(0xFF1E1E1E) : Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(isDark ? 40 : 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: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: isDark ? Colors.grey[200] : Colors.black87,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: isDark ? Colors.grey[400] : Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user