Files
wushu/lib/views/profile/settings/app_fun.dart
2026-03-30 21:30:11 +08:00

451 lines
13 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../constants/app_constants.dart';
import './widgets.dart';
import '../../home/home-load.dart';
import '../../../controllers/load/locally.dart';
/// 时间: 2026-03-26
/// 功能: 功能设置页面
/// 介绍: 应用功能开关和设置项管理
/// 最新变化: 添加自动刷新功能
class AppFunSettingsPage extends StatefulWidget {
const AppFunSettingsPage({super.key});
@override
State<AppFunSettingsPage> createState() => _AppFunSettingsPageState();
}
class _AppFunSettingsPageState extends State<AppFunSettingsPage> {
bool _autoRefreshEnabled = false;
bool _debugInfoEnabled = false;
bool _soundEnabled = true;
bool _vibrationEnabled = true;
bool _darkModeEnabled = false;
bool _preloadEnabled = true;
bool _notificationEnabled = true;
bool _globalTipsEnabled = true; // 添加全局Tips开关状态
int _cacheSize = 128;
static const String _autoRefreshKey = 'auto_refresh_enabled';
static const String _debugInfoKey = 'debug_info_enabled';
static const String _globalTipsKey = 'global_tips_enabled'; // 添加全局Tips开关key
@override
void initState() {
super.initState();
_loadSettings();
}
Future<void> _loadSettings() async {
final prefs = await SharedPreferences.getInstance();
if (mounted) {
setState(() {
_autoRefreshEnabled = prefs.getBool(_autoRefreshKey) ?? false;
_debugInfoEnabled = prefs.getBool(_debugInfoKey) ?? false;
_globalTipsEnabled =
prefs.getBool(_globalTipsKey) ?? true; // 加载全局Tips开关状态
_preloadEnabled = prefs.getBool('preload_enabled') ?? true;
});
}
}
Future<void> _setAutoRefresh(bool value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_autoRefreshKey, value);
await AutoRefreshManager().setEnabled(value);
if (mounted) {
setState(() {
_autoRefreshEnabled = value;
});
}
}
Future<void> _setDebugInfo(bool value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_debugInfoKey, value);
await DebugInfoManager().setEnabled(value);
if (mounted) {
setState(() {
_debugInfoEnabled = value;
});
}
}
Future<void> _setPreload(bool value) async {
await LocalCacheManager().setPreloadEnabled(value);
if (mounted) {
setState(() {
_preloadEnabled = value;
});
}
}
// 设置全局Tips开关
Future<void> _setGlobalTips(bool value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_globalTipsKey, value);
if (mounted) {
setState(() {
_globalTipsEnabled = value;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
appBar: AppBar(
title: Text(
'功能设置',
style: TextStyle(
color: AppConstants.primaryColor,
fontWeight: FontWeight.bold,
),
),
backgroundColor: Colors.white,
elevation: 0,
centerTitle: true,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: AppConstants.primaryColor),
onPressed: () => Navigator.of(context).pop(),
),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
_buildSettingsGroup('基础功能', [
_buildSwitchItem(
'自动刷新',
'首页诗句自动刷新5s ',
Icons.refresh,
_autoRefreshEnabled,
_setAutoRefresh,
),
_buildSwitchItem(
'调式信息',
'开启后可加载更多信息',
Icons.bug_report,
_debugInfoEnabled,
_setDebugInfo,
),
_buildSwitchItem(
'预加载',
_preloadEnabled
? '开启后 部分数据优先使用本地缓存,减少与服务器的通信次数'
: '关闭后,优先使用云端数据,无延迟,但刷新缓慢',
Icons.storage,
_preloadEnabled,
_setPreload,
),
]),
const SizedBox(height: 16),
_buildSettingsGroup('显示设置', [
_buildSwitchItem(
'Tap沉浸光感',
'开启后底栏显示类iOS26 风格\nbeta实验功能',
Icons.dark_mode,
_darkModeEnabled,
(value) => setState(() => _darkModeEnabled = value),
),
_buildFontSliderItem(),
]),
const SizedBox(height: 16),
_buildSettingsGroup('交互反馈', [
_buildSwitchItem(
'全局Tips开关',
'显示一些使用技巧',
Icons.volume_up,
_globalTipsEnabled,
(value) => _setGlobalTips(value),
),
_buildSwitchItem(
'声音反馈',
'操作时播放提示音',
Icons.volume_up,
_soundEnabled,
(value) => setState(() => _soundEnabled = value),
),
_buildSwitchItem(
'震动反馈',
'操作时震动提示',
Icons.vibration,
_vibrationEnabled,
(value) => setState(() => _vibrationEnabled = value),
),
]),
const SizedBox(height: 16),
_buildSettingsGroup('高级设置', [
_buildActionItem(
'Beta开关 关闭软件',
'开发者选项',
Icons.developer_mode,
() => _showSnackBar('调试模式开发中'),
),
_buildActionItem(
'重置设置',
'恢复默认设置',
Icons.restore,
() => _showResetDialog(),
),
_buildActionItem(
'调试模式',
'开发者选项',
Icons.developer_mode,
() => _showSnackBar('调试模式开发中'),
),
]),
const SizedBox(height: 32),
_buildVersionInfo(),
],
),
);
}
Widget _buildSettingsGroup(String title, List<Widget> items) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
child: Text(
title,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: AppConstants.primaryColor,
),
),
),
...items,
],
),
);
}
Widget _buildSwitchItem(
String title,
String subtitle,
IconData icon,
bool value,
ValueChanged<bool> onChanged,
) {
return ListTile(
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: AppConstants.primaryColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, color: AppConstants.primaryColor, size: 20),
),
title: Text(
title,
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
),
subtitle: Text(
subtitle,
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
),
trailing: Switch(
value: value,
onChanged: onChanged,
activeThumbColor: AppConstants.primaryColor,
),
);
}
Widget _buildFontSliderItem() {
return ListTile(
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: AppConstants.primaryColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(Icons.widgets, color: AppConstants.primaryColor, size: 20),
),
title: const Text(
'桌面卡片',
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
),
subtitle: Text(
'使用帮助',
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
),
trailing: Icon(Icons.chevron_right, color: Colors.grey[400]),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const WidgetsPage()),
);
},
);
}
Widget _buildCacheItem() {
return ListTile(
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: AppConstants.primaryColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(Icons.storage, color: AppConstants.primaryColor, size: 20),
),
title: const Text(
'缓存大小',
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
),
subtitle: Text(
'$_cacheSize MB',
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
),
trailing: Icon(Icons.chevron_right, color: Colors.grey[400]),
);
}
Widget _buildActionItem(
String title,
String subtitle,
IconData icon,
VoidCallback onTap,
) {
return ListTile(
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: AppConstants.primaryColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, color: AppConstants.primaryColor, size: 20),
),
title: Text(
title,
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
),
subtitle: Text(
subtitle,
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
),
trailing: Icon(Icons.chevron_right, color: Colors.grey[400]),
onTap: onTap,
);
}
Widget _buildVersionInfo() {
return Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: Column(
children: [
Icon(
Icons.settings_suggest,
size: 40,
color: AppConstants.primaryColor,
),
const SizedBox(height: 12),
Text(
AppConstants.appName,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(
'版本 ${AppConstants.appVersion}',
style: TextStyle(fontSize: 13, color: Colors.grey[600]),
),
],
),
);
}
void _showSnackBar(String message) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(message)));
}
void _showClearCacheDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('清除缓存'),
content: const Text('确定要清除所有缓存数据吗?这不会影响您的笔记和收藏。'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
setState(() {
_cacheSize = 0;
});
_showSnackBar('缓存已清除');
},
child: Text('确定', style: TextStyle(color: Colors.red[400])),
),
],
),
);
}
void _showResetDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('重置设置'),
content: const Text('确定要恢复默认设置吗?'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消'),
),
TextButton(
onPressed: () async {
Navigator.of(context).pop();
await _setAutoRefresh(false);
await _setDebugInfo(false);
setState(() {
_soundEnabled = true;
_vibrationEnabled = true;
_globalTipsEnabled = true; // 重置全局Tips开关为开启
_darkModeEnabled = false;
_notificationEnabled = true;
});
_showSnackBar('已恢复默认设置');
},
child: Text('确定', style: TextStyle(color: Colors.red[400])),
),
],
),
);
}
}