import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../../constants/app_constants.dart'; import '../../../utils/audio_manager.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 createState() => _AppFunSettingsPageState(); } class _AppFunSettingsPageState extends State { bool _autoRefreshEnabled = false; bool _debugInfoEnabled = false; bool _soundEnabled = false; // 默认关闭 bool _vibrationEnabled = true; bool _preloadEnabled = true; bool _globalTipsEnabled = true; // 添加全局Tips开关状态 bool _hideSecondaryButtons = false; // 隐藏次要按钮 static const String _autoRefreshKey = 'auto_refresh_enabled'; static const String _debugInfoKey = 'debug_info_enabled'; static const String _globalTipsKey = 'global_tips_enabled'; // 添加全局Tips开关key static const String _soundEnabledKey = 'sound_enabled'; // 声音反馈开关key static const String _hideSecondaryButtonsKey = 'hide_secondary_buttons'; // 隐藏次要按钮key @override void initState() { super.initState(); _loadSettings(); } Future _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; _soundEnabled = prefs.getBool(_soundEnabledKey) ?? false; // 加载声音反馈状态 _hideSecondaryButtons = prefs.getBool(_hideSecondaryButtonsKey) ?? false; // 加载隐藏次要按钮状态 }); } } Future _setAutoRefresh(bool value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_autoRefreshKey, value); await AutoRefreshManager().setEnabled(value); if (mounted) { setState(() { _autoRefreshEnabled = value; }); } } Future _setDebugInfo(bool value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_debugInfoKey, value); await DebugInfoManager().setEnabled(value); if (mounted) { setState(() { _debugInfoEnabled = value; }); } } Future _setPreload(bool value) async { await LocalCacheManager().setPreloadEnabled(value); if (mounted) { setState(() { _preloadEnabled = value; }); } } // 设置声音反馈 Future _setSoundEnabled(bool value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_soundEnabledKey, value); // 更新 AudioManager 的静音状态 AudioManager().setMuted(!value); if (mounted) { setState(() { _soundEnabled = value; }); } } // 设置震动反馈 Future _setVibrationEnabled(bool value) async { if (value && !_vibrationEnabled) { // 从关闭到开启,显示提示对话框 _showVibrationDialog(); } else { // 从开启到关闭,直接设置 if (mounted) { setState(() { _vibrationEnabled = value; }); } } } // 显示震动提示对话框 void _showVibrationDialog() { 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(); if (mounted) { setState(() { _vibrationEnabled = true; }); } }, child: Text( '确定', style: TextStyle(color: AppConstants.primaryColor), ), ), ], ), ); } // 设置隐藏次要按钮 Future _setHideSecondaryButtons(bool value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_hideSecondaryButtonsKey, value); // 更新管理器状态 await SecondaryButtonsManager().setHidden(value); if (mounted) { setState(() { _hideSecondaryButtons = 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('主页显示设置', [ _buildDevelopmentItem( 'Tap沉浸光感', '开启后底栏显示类iOS26 风格', Icons.dark_mode, ), _buildSwitchItem( '隐藏次要按钮', '开启后隐藏上一条和分享按钮', Icons.share, _hideSecondaryButtons, _setHideSecondaryButtons, ), _buildFontSliderItem(), ]), const SizedBox(height: 16), _buildSettingsGroup('交互反馈', [ _buildSwitchItem( '全局Tips开关', '显示一些使用技巧', Icons.volume_up, _globalTipsEnabled, (value) async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_globalTipsKey, value); if (mounted) { setState(() { _globalTipsEnabled = value; }); } }, ), _buildSwitchItem( '声音反馈', '操作时播放提示音', Icons.volume_up, _soundEnabled, _setSoundEnabled, ), _buildSwitchItem( '震动反馈', '操作时震动提示', Icons.vibration, _vibrationEnabled, (value) => _setVibrationEnabled(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 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 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: () { // 显示对话框提示鸿蒙设备设置方法 showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Row( children: [ Icon(Icons.info_outline, color: AppConstants.primaryColor), const SizedBox(width: 8), const Text('桌面卡片设置'), ], ), content: const Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( '鸿蒙设备请在桌面端设置:', style: TextStyle(fontWeight: FontWeight.bold), ), SizedBox(height: 8), Text('1. 长按桌面空白处'), Text('2. 选择「情景诗词」卡片'), Text('3. 添加后点击桌面卡片即可设置'), Text('4. 后续版本支持在应用内设置卡片'), SizedBox(height: 12), Text( '注意:该页面设置对鸿蒙设备不生效', style: TextStyle(color: Colors.orange, fontSize: 12), ), ], ), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); // 返回桌面 SystemNavigator.pop(); }, child: const Text('前往桌面'), ), ElevatedButton( onPressed: () { Navigator.of(context).pop(); Navigator.push( context, MaterialPageRoute( builder: (context) => const WidgetsPage(), ), ); }, style: ElevatedButton.styleFrom( backgroundColor: AppConstants.primaryColor, foregroundColor: Colors.white, ), child: const Text('仍要进入'), ), ], ); }, ); }, ); } 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 _buildDevelopmentItem(String title, String subtitle, IconData icon) { return ListTile( leading: Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.grey.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(8), ), child: Icon(icon, color: Colors.grey, size: 20), ), title: Text( title, style: TextStyle( fontSize: 15, fontWeight: FontWeight.w500, color: Colors.grey[600], ), ), subtitle: Text( subtitle, style: TextStyle(fontSize: 12, color: Colors.grey[400]), ), trailing: Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4), decoration: BoxDecoration( color: Colors.grey.withValues(alpha: 0.2), borderRadius: BorderRadius.circular(12), ), child: Text( '开发中', style: TextStyle( fontSize: 12, color: Colors.grey[600], fontWeight: FontWeight.w500, ), ), ), ); } 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 _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开关为开启 }); _showSnackBar('已恢复默认设置'); }, child: Text('确定', style: TextStyle(color: Colors.red[400])), ), ], ), ); } }