From 21925929e164fcacbd37fe801d732abff0af7492 Mon Sep 17 00:00:00 2001 From: venti1112 <3788365896@qq.com> Date: Sun, 18 Oct 2026 13:02:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20release=20=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E5=A4=B1=E8=B4=A5=EF=BC=88theme=20=E6=9C=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20/=20=5FrefreshBackground=20=E6=AE=8B=E7=95=99?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/app.dart | 1 + lib/core/widgets/background_widget.dart | 29 +++++++++++++++++++++ lib/core/widgets/user_agreement_dialog.dart | 5 ++-- lib/features/server_list/server_card.dart | 3 +-- lib/features/settings/settings_page.dart | 6 +++-- test/server_card_test.dart | 15 ++++++++--- 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index 5adaf8e..5b29d48 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -122,6 +122,7 @@ class ZVManagerAppState extends State { builder: (context, child) { child ??= const SizedBox.shrink(); return BackgroundWidget( + key: backgroundWidgetKey, child: Stack( children: [ child, diff --git a/lib/core/widgets/background_widget.dart b/lib/core/widgets/background_widget.dart index e50add6..48afa64 100644 --- a/lib/core/widgets/background_widget.dart +++ b/lib/core/widgets/background_widget.dart @@ -40,6 +40,14 @@ class _Layer { Future dispose() async => video?.dispose(); } +/// 背景层全局 Key。 +/// +/// 背景层挂在 `MaterialApp.builder` 顶层(见 `app.dart`),普通页面通过 +/// `context` 拿不到它的 State;这个 Key 提供了稳定的访问入口,例如设置页 +/// 切换「视频声音」开关后立即调整当前视频的音量。 +final GlobalKey backgroundWidgetKey = + GlobalKey(); + /// 全局壁纸背景。 /// /// 原实现切壁纸会闪屏,原因有三条,这里逐一解决: @@ -87,6 +95,10 @@ class BackgroundWidgetState extends State super.initState(); WidgetsBinding.instance.addObserver(this); + // spec 是背景层的数据源:启动预加载完成、或设置页切换壁纸时, + // 都由 main.dart 写入新值,这里监听并交叉淡入到新图层。 + BackgroundWidget.spec.addListener(_onSpecChanged); + // 关键:不能在这里同步调用 _apply()。 // _buildLayer() 内部要用 precacheImage(provider, context), // 而 initState 阶段 context 尚未挂载到渲染树,precacheImage 会抛异常, @@ -114,6 +126,22 @@ class BackgroundWidgetState extends State /// 外部入口:按 `spec` 最新值切换壁纸。 void refreshBackground() => _apply(BackgroundWidget.spec.value, animate: true); + void _onSpecChanged() { + if (!mounted) return; + refreshBackground(); + } + + /// 外部入口:视频声音开关变化后立即应用,无需重建图层。 + void applyVideoSound(bool enabled) { + final volume = enabled ? 1.0 : 0.0; + for (final layer in [_active, _incoming]) { + final video = layer?.video; + if (video != null && video.value.isInitialized) { + video.setVolume(volume); + } + } + } + static String _keyOf(BackgroundSpec s) => s.isEmpty ? 'none' : '${s.type}:${s.path}'; Future<_Layer?> _buildLayer(BackgroundSpec spec) async { @@ -226,6 +254,7 @@ class BackgroundWidgetState extends State @override void dispose() { _requestSeq++; // 让在途请求失效 + BackgroundWidget.spec.removeListener(_onSpecChanged); WidgetsBinding.instance.removeObserver(this); _active?.dispose(); _incoming?.dispose(); diff --git a/lib/core/widgets/user_agreement_dialog.dart b/lib/core/widgets/user_agreement_dialog.dart index 6171366..9a14149 100644 --- a/lib/core/widgets/user_agreement_dialog.dart +++ b/lib/core/widgets/user_agreement_dialog.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; class UserAgreementDialog extends StatefulWidget { final VoidCallback onAgree; @@ -15,8 +14,8 @@ class _UserAgreementDialogState extends State { @override Widget build(BuildContext context) { - return WillPopScope( - onWillPop: () async => false, + return PopScope( + canPop: false, child: Dialog( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), insetPadding: const EdgeInsets.symmetric(horizontal: 20), diff --git a/lib/features/server_list/server_card.dart b/lib/features/server_list/server_card.dart index 70feb1b..3f52156 100644 --- a/lib/features/server_list/server_card.dart +++ b/lib/features/server_list/server_card.dart @@ -1,5 +1,3 @@ -import 'dart:ui' show FontFeature; - import 'package:flutter/material.dart'; /// 服务器卡片的数据状态。 @@ -252,6 +250,7 @@ class ServerCard extends StatelessWidget { } Widget _buildMetrics(BuildContext context, ColorScheme scheme) { + final theme = Theme.of(context); final online = state == ServerCardState.online; if (state == ServerCardState.loading) { diff --git a/lib/features/settings/settings_page.dart b/lib/features/settings/settings_page.dart index eacb147..7859dcc 100644 --- a/lib/features/settings/settings_page.dart +++ b/lib/features/settings/settings_page.dart @@ -8,6 +8,7 @@ import 'package:path_provider/path_provider.dart'; import '../../core/theme/app_theme.dart'; import '../../core/utils/update_service.dart'; import '../../core/utils/background_service.dart'; +import '../../core/widgets/background_widget.dart'; import '../../providers/theme_provider.dart'; import '../../providers/wallpaper_provider.dart'; @@ -161,7 +162,7 @@ class _SettingsPageState extends State { return destPath; } catch (e) { - print('保存失败: $e'); + debugPrint('保存失败: $e'); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('保存失败,请重试')), @@ -255,7 +256,8 @@ class _SettingsPageState extends State { onChanged: (value) async { await BackgroundService.setVideoSound(value); setState(() => _videoSoundEnabled = value); - _refreshBackground(); + // 立即应用到正在播放的背景视频,无需重建图层 + backgroundWidgetKey.currentState?.applyVideoSound(value); }, ), ], diff --git a/test/server_card_test.dart b/test/server_card_test.dart index ddd47bc..cc59277 100644 --- a/test/server_card_test.dart +++ b/test/server_card_test.dart @@ -109,16 +109,23 @@ void main() { ), )); + // 在线态的状态圆点是无限循环动画,pumpAndSettle 永远不会结束。 + // 这里改为推进固定时长,足够让弹出菜单的开/关动画走完。 + Future settleMenu() async { + await tester.pump(); + await tester.pump(const Duration(milliseconds: 500)); + } + await tester.tap(find.byIcon(Icons.more_vert)); - await tester.pumpAndSettle(); + await settleMenu(); await tester.tap(find.text('编辑')); - await tester.pumpAndSettle(); + await settleMenu(); expect(edited, isTrue); await tester.tap(find.byIcon(Icons.more_vert)); - await tester.pumpAndSettle(); + await settleMenu(); await tester.tap(find.text('删除')); - await tester.pumpAndSettle(); + await settleMenu(); expect(deleted, isTrue); });