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.
This commit is contained in:
meyou 2026-05-16 15:58:12 +08:00
parent b0b97650ec
commit 16b73c4373
2 changed files with 14 additions and 11 deletions

View File

@ -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()

View File

@ -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 ->