feat: 卡片式服务器列表 + 壁纸切换闪屏修复

- 新增 ServerCard 卡片组件:三层信息结构、在线呼吸状态点、指标胶囊、
  编辑/删除收进 PopupMenu,窄屏 ellipsis 防溢出
- 列表状态轮询改为渐进式刷新:仅首次探测显示 loading,之后静默替换,
  消除每 5 秒整片跳动的现象
- 背景改为双层交叉淡入:旧图层常驻,新图层 precache 后淡入,
  淡入结束才释放旧 VideoPlayerController
- 修复 WallpaperProvider 在 app.dart 与 main.dart 中存在两个实例、
  hasWallpaper 不同步导致 AppBar 跳色的问题
- 启动时由 main() 预加载结果直接写入 BackgroundWidget.spec,首帧即带壁纸
- 换壁纸时清理旧文件,避免应用目录无限增长
- 新增 test/server_card_test.dart
This commit is contained in:
DriftinBlock committed 2026-10-17 19:43:24 +08:00
1 parent 2dc484154d
commit 67ba7d2c83
8 files changed
+1115 -299

No files matched your search

+27 -10
View File
@@ -9,7 +9,7 @@ import '../../core/theme/app_theme.dart';
import '../../core/utils/update_service.dart';
import '../../core/utils/background_service.dart';
import '../../providers/theme_provider.dart';
import '../../app.dart';
import '../../providers/wallpaper_provider.dart';
class SettingsPage extends StatefulWidget {
const SettingsPage({super.key});
@@ -125,7 +125,9 @@ class _SettingsPageState extends State<SettingsPage> {
final path = await _saveFileToAssets(result.path, 'image');
if (path == null) return;
await BackgroundService.setBackground('image', path);
if (!mounted) return;
// 通过 Provider 落盘并通知,背景层随之交叉淡入
await context.read<WallpaperProvider>().setCustomWallpaper(path, type: 'image');
_refreshUI();
}
@@ -139,7 +141,8 @@ class _SettingsPageState extends State<SettingsPage> {
final path = await _saveFileToAssets(filePath, 'video');
if (path == null) return;
await BackgroundService.setBackground('video', path);
if (!mounted) return;
await context.read<WallpaperProvider>().setCustomWallpaper(path, type: 'video');
_refreshUI();
}
@@ -153,6 +156,9 @@ class _SettingsPageState extends State<SettingsPage> {
final file = File(sourcePath);
await file.copy(destPath);
// 换壁纸时清掉上一份(图片与视频都清),否则应用目录会无限膨胀
await _cleanupOldBackgrounds(dir.path, keep: destPath);
return destPath;
} catch (e) {
print('保存失败: $e');
@@ -165,14 +171,30 @@ class _SettingsPageState extends State<SettingsPage> {
}
}
/// 删除应用目录下除 [keep] 之外的旧壁纸文件。
Future<void> _cleanupOldBackgrounds(String dirPath, {required String keep}) async {
try {
final dir = Directory(dirPath);
if (!dir.existsSync()) return;
await for (final entity in dir.list()) {
if (entity is! File) continue;
final name = entity.path.split(Platform.pathSeparator).last;
if (!name.startsWith('background_')) continue;
if (entity.path == keep) continue;
await entity.delete();
}
} catch (_) {
// 清理失败不影响主流程
}
}
Future<void> _clearBackground() async {
await BackgroundService.clearBackground();
await context.read<WallpaperProvider>().resetWallpaper();
_refreshUI();
}
void _refreshUI() {
_loadCurrentBg();
_refreshBackground();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
@@ -184,11 +206,6 @@ class _SettingsPageState extends State<SettingsPage> {
}
}
void _refreshBackground() {
final appState = context.findAncestorStateOfType<ZVManagerAppState>();
appState?.refreshBackground();
}
// ==================== build ====================
@override
+20 -6
View File
@@ -4,7 +4,6 @@ import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import '../../providers/wallpaper_provider.dart';
import '../../app.dart';
class WallpaperSettings extends StatelessWidget {
const WallpaperSettings({super.key});
@@ -73,9 +72,10 @@ class WallpaperSettings extends StatelessWidget {
if (image != null && context.mounted) {
final savedPath = await _saveFileToAssets(context, image.path);
if (savedPath != null && context.mounted) {
// Provider 变更会同步到 BackgroundWidget.spec,
// 背景层自己完成交叉淡入,这里不再需要手动刷新
await provider.setCustomWallpaper(savedPath);
if (context.mounted) {
_refreshBackground(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('壁纸已更新'), backgroundColor: Colors.green),
);
@@ -124,6 +124,9 @@ class WallpaperSettings extends StatelessWidget {
final file = File(sourcePath);
await file.copy(destPath);
// 清理旧壁纸,避免每次换图都在应用目录留一份副本
await _cleanupOldWallpapers(dir.path, keep: destPath);
return destPath;
} catch (e) {
if (context.mounted) {
@@ -135,9 +138,21 @@ class WallpaperSettings extends StatelessWidget {
}
}
void _refreshBackground(BuildContext context) {
final appState = context.findAncestorStateOfType<ZVManagerAppState>();
appState?.refreshBackground();
/// 删除应用目录下除 [keep] 之外的壁纸文件。
Future<void> _cleanupOldWallpapers(String dirPath, {required String keep}) async {
try {
final dir = Directory(dirPath);
if (!dir.existsSync()) return;
await for (final entity in dir.list()) {
if (entity is! File) continue;
final name = entity.path.split(Platform.pathSeparator).last;
if (!name.startsWith('background_image_')) continue;
if (entity.path == keep) continue;
await entity.delete();
}
} catch (_) {
// 清理失败不影响主流程
}
}
void _showResetDialog(BuildContext context, WallpaperProvider provider) {
@@ -155,7 +170,6 @@ class WallpaperSettings extends StatelessWidget {
onPressed: () async {
await provider.resetWallpaper();
if (context.mounted) {
_refreshBackground(context);
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已恢复默认壁纸'), backgroundColor: Colors.green),