Created
August 14, 2024 09:37
-
-
Save KlassenKonstantin/f86fe498b41c013e156aaff0f1f8424d to your computer and use it in GitHub Desktop.
AnimatedVisibility overload with target state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
TheTheme { | |
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> | |
val helloWorld = "Hello World!" | |
var textOrNull by remember { mutableStateOf<String?>(helloWorld) } | |
Column( | |
modifier = Modifier | |
.padding(innerPadding) | |
.fillMaxSize(), | |
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Row( | |
Modifier | |
.fillMaxWidth() | |
.padding(horizontal = 16.dp), | |
horizontalArrangement = Arrangement.spacedBy(16.dp), | |
verticalAlignment = Alignment.Bottom | |
) { | |
Tile( | |
title = "AnimatedVisibility", | |
Modifier.weight(1f) | |
) { | |
AnimatedVisibility( | |
modifier = Modifier, | |
visible = textOrNull != null | |
) { | |
textOrNull?.let { | |
Text(it) | |
} | |
} | |
} | |
Tile( | |
title = "AnimatedContent", | |
Modifier.weight(1f) | |
) { | |
AnimatedContent( | |
modifier = Modifier, | |
targetState = textOrNull, | |
transitionSpec = { | |
fadeIn() + expandVertically() togetherWith fadeOut() + shrinkVertically() | |
}, | |
contentAlignment = Alignment.Center | |
) { textOrNull -> | |
when { | |
textOrNull == null -> Box(Modifier.fillMaxWidth()) | |
else -> Text(textOrNull) | |
} | |
} | |
} | |
Tile( | |
title = "New overload", | |
Modifier.weight(1f) | |
) { | |
AnimatedVisibility( | |
modifier = Modifier, | |
value = textOrNull | |
) {text -> | |
Text(text) | |
} | |
} | |
} | |
Button(onClick = { | |
textOrNull = if (textOrNull == null) helloWorld else null | |
}) { | |
Text("Toggle visibility") | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Tile( | |
title: String, | |
modifier: Modifier, | |
content: @Composable ColumnScope.() -> Unit, | |
) { | |
Column( | |
modifier = modifier | |
) { | |
Text(title, style = MaterialTheme.typography.labelSmall) | |
Spacer(Modifier.height(4.dp)) | |
Card( | |
modifier = Modifier.aspectRatio(1f, false) | |
) { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
content() | |
Text("Second line") | |
} | |
} | |
} | |
} | |
@Composable | |
fun <T> ColumnScope.AnimatedVisibility( | |
value: T?, | |
modifier: Modifier = Modifier, | |
content: @Composable AnimatedVisibilityScope.(T) -> Unit, | |
) { | |
// null until non-null then never null again | |
var lastNonNullValueOrNull by remember { mutableStateOf(value) } | |
lastNonNullValueOrNull = value ?: lastNonNullValueOrNull | |
AnimatedVisibility( | |
visible = value != null, | |
modifier = modifier, | |
) { | |
lastNonNullValueOrNull?.let { | |
content(it) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment