https://kotlinlang.org logo
#compose
Title
# compose
t

the great warrior

04/14/2022, 3:45 PM
Hi guys I have Transition that manages AnimatedVisibility of the buttons and runs them simultaneously if isStartButtonVisible changes, but the problem is that isStartButtonVisible state is not saved and that makes AnimatedVisibility of the buttons also change Go to reply if you wanna see the full code
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                          )                      }                  }              }          } }
d

Doris Liu

04/14/2022, 11:57 PM
Could you elaborate what you mean by "isStartButtonVisible state is not saved"?
t

the great warrior

04/15/2022, 5:04 PM
3 Views