This commit is contained in:
TangJo 2026-03-25 17:55:03 +08:00
commit 44cf59749d
1 changed files with 95 additions and 30 deletions

View File

@ -199,6 +199,12 @@ class _WebShellPageState extends State<WebShellPage>
return generation == _webViewGeneration; return generation == _webViewGeneration;
} }
void _debugLog(String message) {
if (kDebugMode) {
debugPrint(message);
}
}
PlatformWebViewWidgetCreationParams _buildAndroidWidgetParams( PlatformWebViewWidgetCreationParams _buildAndroidWidgetParams(
PlatformWebViewWidgetCreationParams widgetParams, PlatformWebViewWidgetCreationParams widgetParams,
AndroidRenderMode renderMode, AndroidRenderMode renderMode,
@ -282,6 +288,11 @@ class _WebShellPageState extends State<WebShellPage>
_hasMeasuredProgress = true; _hasMeasuredProgress = true;
} }
}); });
_debugLog(
'🔍 [onProgress] progress=$progress, '
'hasStartedRemote=$_hasStartedRemoteMainFrame, '
'hasMeasured=$_hasMeasuredProgress',
);
}, },
onNavigationRequest: (request) async { onNavigationRequest: (request) async {
if (!_isActiveWebViewGeneration(generation)) { if (!_isActiveWebViewGeneration(generation)) {
@ -316,6 +327,13 @@ class _WebShellPageState extends State<WebShellPage>
_hasStartedRemoteMainFrame = true; _hasStartedRemoteMainFrame = true;
_cancelStartupWatchdog(); _cancelStartupWatchdog();
_recordWebViewEvent('页面开始加载:$url'); _recordWebViewEvent('页面开始加载:$url');
_debugLog(
'🔍 [onPageStarted] url=$url, '
'hasStartedRemote=$_hasStartedRemoteMainFrame, '
'hasBootstrapped=$_hasBootstrapped, '
'hasMainFrameError=$_hasMainFrameError, '
'watchdog已取消',
);
if (!mounted) { if (!mounted) {
return; return;
} }
@ -341,6 +359,12 @@ class _WebShellPageState extends State<WebShellPage>
} }
_recordWebViewEvent('页面加载完成:$url'); _recordWebViewEvent('页面加载完成:$url');
_cancelStartupWatchdog(); _cancelStartupWatchdog();
_debugLog(
'🔍 [onPageFinished] url=$url, '
'hasMainFrameError=$_hasMainFrameError, '
'injectBridge=${!_hasMainFrameError}, '
'retryCount=$_startupRetryCount',
);
if (!_hasMainFrameError) { if (!_hasMainFrameError) {
unawaited(_injectAppShellBridge(_controller, url)); unawaited(_injectAppShellBridge(_controller, url));
} }
@ -392,6 +416,11 @@ class _WebShellPageState extends State<WebShellPage>
'url=${error.url}, ' 'url=${error.url}, '
'description=${error.description}', 'description=${error.description}',
); );
_debugLog(
'🔍 [onWebResourceError] isMainFrame=${error.isForMainFrame}, '
'errorType=${error.errorType}, '
'willSetMainFrameError=${error.isForMainFrame != false}',
);
if (!mounted || error.isForMainFrame == false) { if (!mounted || error.isForMainFrame == false) {
return; return;
} }
@ -476,10 +505,19 @@ class _WebShellPageState extends State<WebShellPage>
} }
Future<void> _handleStartupTimeout() async { Future<void> _handleStartupTimeout() async {
_debugLog(
'🔍 [_handleStartupTimeout] mounted=$mounted, '
'isLoading=$_isLoadingPage, '
'hasMainFrameError=$_hasMainFrameError, '
'hasStartedRemote=$_hasStartedRemoteMainFrame, '
'retryCount=$_startupRetryCount, '
'renderIndex=$_renderModeIndex',
);
if (!mounted || if (!mounted ||
!_isLoadingPage || !_isLoadingPage ||
_hasMainFrameError || _hasMainFrameError ||
_hasStartedRemoteMainFrame) { _hasStartedRemoteMainFrame) {
_debugLog('🔍 [_handleStartupTimeout] 提前返回,不执行超时恢复');
return; return;
} }
@ -487,6 +525,11 @@ class _WebShellPageState extends State<WebShellPage>
final maxRetryCount = _androidCompatibilityPlan.prefersAggressiveRecovery final maxRetryCount = _androidCompatibilityPlan.prefersAggressiveRecovery
? 2 ? 2
: 1; : 1;
_debugLog(
'🔍 [_handleStartupTimeout] switchedRenderMode=$switchedRenderMode, '
'maxRetryCount=$maxRetryCount, '
'currentRetryCount=$_startupRetryCount',
);
if (switchedRenderMode || _startupRetryCount < maxRetryCount) { if (switchedRenderMode || _startupRetryCount < maxRetryCount) {
final nextRetryCount = _startupRetryCount + 1; final nextRetryCount = _startupRetryCount + 1;
@ -505,6 +548,7 @@ class _WebShellPageState extends State<WebShellPage>
return; return;
} }
_debugLog('🔍 [_handleStartupTimeout] 所有重试已耗尽,显示错误页面');
_setMainFrameError( _setMainFrameError(
title: '页面启动超时', title: '页面启动超时',
message: <String>[ message: <String>[
@ -516,6 +560,10 @@ class _WebShellPageState extends State<WebShellPage>
} }
void _setMainFrameError({required String title, required String message}) { void _setMainFrameError({required String title, required String message}) {
_debugLog(
'🔍 [_setMainFrameError] title=$title, '
'message=$message',
);
_cancelStartupWatchdog(); _cancelStartupWatchdog();
if (!mounted) { if (!mounted) {
return; return;
@ -528,6 +576,12 @@ class _WebShellPageState extends State<WebShellPage>
_errorMessage = message; _errorMessage = message;
_progress = 0; _progress = 0;
}); });
_debugLog(
'🔍 [_setMainFrameError] 状态已更新: '
'isLoading=$_isLoadingPage, '
'hasMainFrameError=$_hasMainFrameError, '
'→ 应显示 ErrorOverlay',
);
} }
Future<void> _recoverFromBrokenStartupState({bool deepReset = false}) async { Future<void> _recoverFromBrokenStartupState({bool deepReset = false}) async {
@ -1023,6 +1077,15 @@ class _WebShellPageState extends State<WebShellPage>
final showProgressBar = final showProgressBar =
_isLoadingPage && (!_hasMeasuredProgress || _progress < 100); _isLoadingPage && (!_hasMeasuredProgress || _progress < 100);
final showLaunchOverlay = !_hasBootstrapped && !_hasMainFrameError; final showLaunchOverlay = !_hasBootstrapped && !_hasMainFrameError;
_debugLog(
'🔍 [build] showProgressBar=$showProgressBar, '
'showLaunchOverlay=$showLaunchOverlay, '
'showErrorOverlay=$_hasMainFrameError, '
'showWebView=${!_hasMainFrameError}, '
'isLoading=$_isLoadingPage, '
'hasBootstrapped=$_hasBootstrapped, '
'progress=$_progress',
);
return PopScope<void>( return PopScope<void>(
canPop: false, canPop: false,
@ -1041,39 +1104,41 @@ class _WebShellPageState extends State<WebShellPage>
end: Alignment.bottomCenter, end: Alignment.bottomCenter,
), ),
), ),
child: Stack( child: SizedBox.expand(
children: <Widget>[ child: Stack(
Visibility( children: <Widget>[
visible: !_hasMainFrameError, Visibility(
child: _webViewWidget, visible: !_hasMainFrameError,
), child: _webViewWidget,
if (showProgressBar)
Positioned(
top: 0,
left: 0,
right: 0,
child: TopProgressBar(
progress: _progress,
hasMeasuredProgress: _hasMeasuredProgress,
),
), ),
if (showLaunchOverlay) if (showProgressBar)
Positioned.fill( Positioned(
child: LaunchOverlay( top: 0,
progress: _progress, left: 0,
hasMeasuredProgress: _hasMeasuredProgress, right: 0,
child: TopProgressBar(
progress: _progress,
hasMeasuredProgress: _hasMeasuredProgress,
),
), ),
), if (showLaunchOverlay)
if (_hasMainFrameError) Positioned.fill(
Positioned.fill( child: LaunchOverlay(
child: ErrorOverlay( progress: _progress,
title: _errorTitle, hasMeasuredProgress: _hasMeasuredProgress,
message: _errorMessage, ),
currentUrl: _currentUrl,
onRetry: _reloadPage,
), ),
), if (_hasMainFrameError)
], Positioned.fill(
child: ErrorOverlay(
title: _errorTitle,
message: _errorMessage,
currentUrl: _currentUrl,
onRetry: _reloadPage,
),
),
],
),
), ),
), ),
), ),