if I press the start button isStartButtonVisible s...
# compose
t
if I press the start button isStartButtonVisible state is changed from true to false and when I go to another tab compose doesn't remember that. go to reply if you want to see the full code
i
Sounds like you aren't using
rememberSaveable
t
OptIn(ExperimentalAnimationApi::class, ExperimentalTime::class, ExperimentalTransitionApi::class) @Composable fun StopwatchScreen( modifier: Modifier = Modifier, lapItems: List<Lap>, onPause: () -> Unit, onStop: () -> Unit, onStart: () -> Unit, onLap: () -> Unit, onClear: () -> Unit, isPlaying: Boolean ) { var isStartButtonVisible by remember { mutableStateOf(true) } Surface(modifier = modifier) { Box(modifier = Modifier.fillMaxSize()) { Column( Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { Buttons( isPlaying = isPlaying, onPause = onPause, onStart = onStart, onStop = onStop, onLap = onLap , onClear = onClear , onLapHeadLine = { lapHeaderLine = it }, scrollState = scrollState, scope = scope, lapsItems = lapItems, isStartButtonVisible = isStartButtonVisible, onChangeStartVisible = { isStartButtonVisible = it} ) } } } } @OptIn(ExperimentalAnimationApi::class) @Composable private fun Buttons( isPlaying: Boolean, onPause: () -> Unit, onStop: () -> Unit, onStart: () -> Unit, onLap: () -> Unit, onClear: () -> Unit, onLapHeadLine: (Boolean) -> Unit, scrollState: LazyListState, scope: CoroutineScope, lapsItems: List<Lap>, isStartButtonVisible: Boolean, onChangeStartVisible: (Boolean) -> Unit, ) { val transition = updateTransition(isStartButtonVisible) Box { transition .AnimatedVisibility( visible = { targetSelected -> targetSelected } ) { ClockButton( onClick = { onStart() onChangeStartVisible(false) }, textButton = "Start", color = MaterialTheme.colorScheme.primary ) } transition .AnimatedVisibility( visible = { targetSelected -> !targetSelected } ) { Row ( horizontalArrangement = Arrangement.spacedBy(32.dp) ){ if (isPlaying) { ClockButton( textButton = "Stop", onClick = onPause, color = Red100 ) ClockButton( textButton = "Lap", onClick = { onLapHeadLine(true) onLap() scope.launch { scrollState.animateScrollToItem(index = lapsItems.lastIndex) } }, color = MaterialTheme.colorScheme.onSurface ) } else { ClockButton( textButton = "Resume", onClick = onStart, color = MaterialTheme.colorScheme.primary ) ClockButton( textButton = "Reset", onClick = { onStop() onLapHeadLine(false) onChangeStartVisible(true) onClear() }, color = MaterialTheme.colorScheme.onSurface ) } } } } }
Copy code
@Composable
fun ClockButton(
    textButton: String,
    onClick: () -> Unit,
    color: Color = MaterialTheme.colorScheme.primary,
) {
    Button(
        onClick = onClick,
        colors = ButtonDefaults.buttonColors(
            containerColor = color
        ),
        contentPadding = PaddingValues(
            start = 40.dp,
            top = 12.dp,
            end = 40.dp,
            bottom = 12.dp
        )
    ) {
        Text(text = textButton,
            style = MaterialTheme.typography.titleMedium)
    }
}
I've tried rememberSaveable and it doesn't work
🤔 1