修复webdav重新登录后,userID位置错位与页面跳转逻辑错误的重大bug

This commit is contained in:
2026-08-27 22:04:59 +08:00
parent bca1288bd1
commit ad6617592e
2 changed files with 37 additions and 35 deletions
+30 -29
View File
@@ -208,7 +208,7 @@ class _HomePageState extends BaseState<HomePage> {
if (renderBox != null) { if (renderBox != null) {
final height = renderBox.size.height; final height = renderBox.size.height;
if (height > 0 && height != _mediaLibraryHeight) { if (height > 0 && height != _mediaLibraryHeight) {
setState(() { safeSetState(() {
_mediaLibraryHeight = height; _mediaLibraryHeight = height;
_heightMeasureRetryCount = 0; _heightMeasureRetryCount = 0;
}); });
@@ -221,7 +221,7 @@ class _HomePageState extends BaseState<HomePage> {
const Duration(milliseconds: 200), _measureMediaLibraryHeight); const Duration(milliseconds: 200), _measureMediaLibraryHeight);
} else { } else {
print('⚠️ 媒体库高度测量失败,使用默认值 280px'); print('⚠️ 媒体库高度测量失败,使用默认值 280px');
setState(() { safeSetState(() {
_mediaLibraryHeight = 280.0; _mediaLibraryHeight = 280.0;
}); });
} }
@@ -229,18 +229,18 @@ class _HomePageState extends BaseState<HomePage> {
} }
void _onScroll() { void _onScroll() {
if (mounted) setState(() {}); if (mounted) safeSetState(() {});
} }
Future<void> _initWebDAV() async { Future<void> _initWebDAV() async {
setState(() => _isLoading = true); safeSetState(() => _isLoading = true);
try { try {
await WebDAVService.instance.loadCredentials(); await WebDAVService.instance.loadCredentials();
_favorites = []; _favorites = [];
} catch (e) { } catch (e) {
_favorites = []; _favorites = [];
} finally { } finally {
if (mounted) setState(() => _isLoading = false); if (mounted) safeSetState(() => _isLoading = false);
} }
} }
@@ -306,12 +306,13 @@ class _HomePageState extends BaseState<HomePage> {
onPressed: () async { onPressed: () async {
Navigator.pop(context); Navigator.pop(context);
await WebDAVService.instance.clearCredentials(); await WebDAVService.instance.clearCredentials();
setState(() { safeSetState(() {
_favorites = []; _favorites = [];
_isLoading = false; _isLoading = false;
}); });
if (mounted) { if (mounted) {
Navigator.pushReplacement( // ⭐ 修复:用 push 保留 HomePage 在栈底
Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (_) => const WebDAVSetupPage(), builder: (_) => const WebDAVSetupPage(),
@@ -333,7 +334,7 @@ class _HomePageState extends BaseState<HomePage> {
if (_scrollController.hasClients) { if (_scrollController.hasClients) {
_scrollController.jumpTo(0.0); _scrollController.jumpTo(0.0);
} }
setState(() {}); safeSetState(() {});
} }
@override @override
@@ -406,7 +407,7 @@ class _HomePageState extends BaseState<HomePage> {
), ),
); );
_resetSnapState(); _resetSnapState();
setState(() {}); safeSetState(() {});
} else { } else {
final result = await Navigator.push( final result = await Navigator.push(
context, context,
@@ -414,17 +415,18 @@ class _HomePageState extends BaseState<HomePage> {
builder: (_) => const WebDAVSetupPage(), builder: (_) => const WebDAVSetupPage(),
), ),
); );
setState(() {}); safeSetState(() {});
if (result == true) { if (result == true) {
await WebDAVService.instance await WebDAVService.instance
.loadCredentials(); .loadCredentials();
setState(() {}); safeSetState(() {});
} }
} }
}, },
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// 左侧云朵图标(固定 56px
const Padding( const Padding(
padding: EdgeInsets.only(left: 8.0), padding: EdgeInsets.only(left: 8.0),
child: Icon( child: Icon(
@@ -434,11 +436,14 @@ class _HomePageState extends BaseState<HomePage> {
), ),
), ),
const SizedBox(width: 16), const SizedBox(width: 16),
// 右侧所有内容
Expanded( Expanded(
child: Column( child: Column(
crossAxisAlignment: crossAxisAlignment:
CrossAxisAlignment.start, CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [ children: [
// 第一行:WebDAV + 状态 + 退出按钮
Row( Row(
children: [ children: [
const Text( const Text(
@@ -460,28 +465,24 @@ class _HomePageState extends BaseState<HomePage> {
fontWeight: FontWeight.w400, fontWeight: FontWeight.w400,
), ),
), ),
// ⭐ 退出按钮(已连接时显示) const Spacer(),
if (isConnected) ...[ if (isConnected)
const Spacer(), Padding(
IconButton( padding:
icon: const Icon( const EdgeInsets.only(top: 2),
Icons.logout, child: GestureDetector(
color: Colors.grey, onTap: _showReauthDialog,
size: 18, child: const Icon(
), Icons.logout,
tooltip: '重新登录', color: Colors.grey,
onPressed: _showReauthDialog, size: 18,
padding: EdgeInsets.zero, ),
constraints: const BoxConstraints(
minWidth: 32,
minHeight: 32,
), ),
), ),
const SizedBox(width: 4),
],
], ],
), ),
const SizedBox(height: 4), const SizedBox(height: 4),
// 第二行:用户名
Text( Text(
isConnected ? username : '点击连接', isConnected ? username : '点击连接',
style: TextStyle( style: TextStyle(
@@ -706,7 +707,7 @@ class _HomePageState extends BaseState<HomePage> {
size: 20, size: 20,
), ),
onPressed: () { onPressed: () {
setState(() { safeSetState(() {
_favorites.removeAt(index); _favorites.removeAt(index);
}); });
}, },
+7 -6
View File
@@ -35,7 +35,7 @@ class _WebDAVSetupPageState extends BaseState<WebDAVSetupPage> {
Future<void> _loadSavedCredentials() async { Future<void> _loadSavedCredentials() async {
final hasCred = await WebDAVService.instance.loadCredentials(); final hasCred = await WebDAVService.instance.loadCredentials();
setState(() { safeSetState(() {
_isConnected = hasCred; _isConnected = hasCred;
_statusText = hasCred ? '已连接' : '未连接'; _statusText = hasCred ? '已连接' : '未连接';
if (hasCred) { if (hasCred) {
@@ -47,7 +47,7 @@ class _WebDAVSetupPageState extends BaseState<WebDAVSetupPage> {
Future<void> _connect() async { Future<void> _connect() async {
if (!_formKey.currentState!.validate()) return; if (!_formKey.currentState!.validate()) return;
setState(() => _isLoading = true); safeSetState(() => _isLoading = true);
try { try {
await WebDAVService.instance.saveCredentials( await WebDAVService.instance.saveCredentials(
@@ -59,7 +59,7 @@ class _WebDAVSetupPageState extends BaseState<WebDAVSetupPage> {
// 验证连接 // 验证连接
await WebDAVService.instance.getMusicFiles(); await WebDAVService.instance.getMusicFiles();
setState(() { safeSetState(() {
_isConnected = true; _isConnected = true;
_statusText = '已连接'; _statusText = '已连接';
_isLoading = false; _isLoading = false;
@@ -72,7 +72,8 @@ class _WebDAVSetupPageState extends BaseState<WebDAVSetupPage> {
backgroundColor: Color(0xFF4CAF50), backgroundColor: Color(0xFF4CAF50),
), ),
); );
// ✅ 登录成功 → 直接跳转到文件列表页 // ⭐ 用 pushReplacement 替换当前页面(WebDAVSetupPage
// 此时栈底是 HomePage,替换后栈变为 [HomePage, WebDAVFileListPage]
Navigator.pushReplacement( Navigator.pushReplacement(
context, context,
MaterialPageRoute( MaterialPageRoute(
@@ -81,7 +82,7 @@ class _WebDAVSetupPageState extends BaseState<WebDAVSetupPage> {
); );
} }
} catch (e) { } catch (e) {
setState(() { safeSetState(() {
_isConnected = false; _isConnected = false;
_statusText = '连接失败'; _statusText = '连接失败';
_isLoading = false; _isLoading = false;
@@ -99,7 +100,7 @@ class _WebDAVSetupPageState extends BaseState<WebDAVSetupPage> {
Future<void> _disconnect() async { Future<void> _disconnect() async {
await WebDAVService.instance.clearCredentials(); await WebDAVService.instance.clearCredentials();
setState(() { safeSetState(() {
_isConnected = false; _isConnected = false;
_statusText = '未连接'; _statusText = '未连接';
}); });