From 16b73c4373f55d5e495be03b8d511ec4198a90ec Mon Sep 17 00:00:00 2001 From: meyou <2636699780@qq.com> Date: Sat, 16 May 2026 15:58:12 +0800 Subject: [PATCH] Fix flash when expanding after navigating months in collapsed state Sync CalendarPager's pagerState to selectedDate in CalendarMonthView via LaunchedEffect(selectedDate), so the page is already correct when CalendarPager re-enters composition during expand. Remove the now- redundant LaunchedEffect(Unit) sync in CalendarPager. --- .../plus/rua/project/ui/CalendarMonthView.kt | 14 ++++++++++++++ .../kotlin/plus/rua/project/ui/CalendarPager.kt | 11 ----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt index ce3c726..5e66966 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarMonthView.kt @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf @@ -61,6 +62,19 @@ fun CalendarMonthView( val pagerState = rememberPagerState(initialPage = START_PAGE, pageCount = { Int.MAX_VALUE }) + // 折叠态 WeekPager 切月时,持续同步 CalendarPager 的 pagerState, + // 避免展开时 CalendarPager 首帧显示旧月份导致闪白 + LaunchedEffect(viewModel.selectedDate) { + @Suppress("DEPRECATION") // monthNumber 无替代 API + val targetPage = yearMonthToPage( + viewModel.selectedDate.year, viewModel.selectedDate.month.number, + today.year, today.month.number + ) + if (targetPage != pagerState.currentPage) { + pagerState.scrollToPage(targetPage) + } + } + val collapseProgress = viewModel.collapseProgress val headerHeightPx = monthHeaderHeightPx + weekdayHeaderHeightPx val rowPaddingPx = with(density) { ROW_PADDING_DP.dp.toPx() }.toInt() diff --git a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt index 6d96511..1d29e21 100644 --- a/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt +++ b/shared/src/commonMain/kotlin/plus/rua/project/ui/CalendarPager.kt @@ -52,17 +52,6 @@ fun CalendarPager( val initialMonth = remember { today.month.number } val coroutineScope = rememberCoroutineScope() - // 展开后同步页面到 selectedDate 所在月份(修复折叠态切月后展开闪跳) - LaunchedEffect(Unit) { - @Suppress("DEPRECATION") // monthNumber 无替代 API - val targetPage = yearMonthToPage( - selectedDate.year, selectedDate.month.number, initialYear, initialMonth - ) - if (targetPage != pagerState.currentPage) { - pagerState.scrollToPage(targetPage) - } - } - // 跳过初始发射,保留首次渲染时的"今天"选中状态 LaunchedEffect(pagerState) { snapshotFlow { pagerState.settledPage }.drop(1).collect { page ->