fix: 修复 release 构建失败(theme 未定义 / _refreshBackground 残留)

This commit is contained in:
venti1112 committed 2026-10-18 13:02:42 +08:00
1 parent 6302e897ba
commit 21925929e1
6 files changed
+48 -11

No files matched your search

+1
View File
@@ -122,6 +122,7 @@ class ZVManagerAppState extends State<ZVManagerApp> {
builder: (context, child) {
child ??= const SizedBox.shrink();
return BackgroundWidget(
key: backgroundWidgetKey,
child: Stack(
children: [
child,
+29
View File
@@ -40,6 +40,14 @@ class _Layer {
Future<void> dispose() async => video?.dispose();
}
/// 背景层全局 Key。
///
/// 背景层挂在 `MaterialApp.builder` 顶层(见 `app.dart`),普通页面通过
/// `context` 拿不到它的 State;这个 Key 提供了稳定的访问入口,例如设置页
/// 切换「视频声音」开关后立即调整当前视频的音量。
final GlobalKey<BackgroundWidgetState> backgroundWidgetKey =
GlobalKey<BackgroundWidgetState>();
/// 全局壁纸背景。
///
/// 原实现切壁纸会闪屏,原因有三条,这里逐一解决:
@@ -87,6 +95,10 @@ class BackgroundWidgetState extends State<BackgroundWidget>
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<BackgroundWidget>
/// 外部入口:按 `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<BackgroundWidget>
@override
void dispose() {
_requestSeq++; // 让在途请求失效
BackgroundWidget.spec.removeListener(_onSpecChanged);
WidgetsBinding.instance.removeObserver(this);
_active?.dispose();
_incoming?.dispose();
+2 -3
View File
@@ -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<UserAgreementDialog> {
@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),
+1 -2
View File
@@ -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) {
+4 -2
View File
@@ -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<SettingsPage> {
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<SettingsPage> {
onChanged: (value) async {
await BackgroundService.setVideoSound(value);
setState(() => _videoSoundEnabled = value);
_refreshBackground();
// 立即应用到正在播放的背景视频,无需重建图层
backgroundWidgetKey.currentState?.applyVideoSound(value);
},
),
],