[英]How to handle visibility of a Text in Jetpack Compose?
I have this Text:我有这个文本:
Text(
text = stringResource(id = R.string.hello)
)
How can I show and hide this component?如何显示和隐藏此组件?
I'm using Jetpack Compose version '1.0.0-alpha03'我正在使用 Jetpack Compose 版本“1.0.0-alpha03”
As CommonsWare stated, compose being a declarative toolkit you tie your component to a state (for ex: isVisible
), then compose will intelligently decide which composables depend on that state and recompose them.正如 CommonsWare 所说,compose 是一个声明性工具包,您将组件绑定到一个状态(例如: isVisible
),然后 compose 将智能地决定哪些可组合项依赖于该状态并重新组合它们。 For ex:例如:
@Composable
fun MyText(isVisible: Boolean){
if(isVisible){
Text(text = stringResource(id = R.string.hello))
}
}
Also you could use the AnimatedVisibility()
composable for animations.您也可以使用AnimatedVisibility()
可组合的动画。
如上所述,您可以使用 AnimatedVisibility ,例如:
AnimatedVisibility(visible = yourCondition) { Text(text = getString(R.string.yourString)) }
/**
* @param visible if false content is invisible ie. space is still occupied
*/
@Composable
fun Visibility(
visible: Boolean,
content: @Composable () -> Unit
) {
val contentSize = remember { mutableStateOf(IntSize.Zero) }
Box(modifier = Modifier
.onSizeChanged { size -> contentSize.value = size }) {
if (visible || contentSize.value == IntSize.Zero) {
content()
} else {
Spacer(modifier = Modifier.size(contentSize.value.width.pxToDp(), contentSize.value.height.pxToDp()))
}
}
}
fun Int.pxToDp(): Dp {
return (this / getSystem().displayMetrics.density).dp
}
usage:用法:
Visibility(text.value.isNotEmpty()) {
IconButton(
onClick = { text.value = "" },
modifier = Modifier
.padding(bottom = 8.dp)
.height(30.dp),
) {
Icon(Icons.Filled.Close, contentDescription = "Clear text")
}
}
With 1.0.0
(tested with 1.0.0-rc02
) you can simply add a condition like:使用1.0.0
(使用1.0.0-rc02
测试)您可以简单地添加一个条件,如:
if(isVisible){
Text("....")
}
Something like:就像是:
var visible by remember { mutableStateOf(true) }
Column() {
Text("Text")
Button(onClick = { visible = !visible }) { Text("Toggle") }
}
If you want to animate the appearance and disappearance of its content you can use the AnimatedVisibility
如果要为其内容的外观和消失设置动画,可以使用AnimatedVisibility
var visible by remember { mutableStateOf(true) }
Column() {
AnimatedVisibility(
visible = visible,
enter = fadeIn(
// Overwrites the initial value of alpha to 0.4f for fade in, 0 by default
initialAlpha = 0.4f
),
exit = fadeOut(
// Overwrites the default animation with tween
animationSpec = tween(durationMillis = 250)
)
) {
// Content that needs to appear/disappear goes here:
Text("....")
}
Button(onClick = { visible = !visible }) { Text("Toggle") }
}
I am using the following method: AnimatedVisibility https://developer.android.com/jetpack/compose/animation#animatedvisibility我使用以下方法: AnimatedVisibility https://developer.android.com/jetpack/compose/animation#animatedvisibility
// Create a MutableTransitionState<Boolean> for the AnimatedVisibility.
val state = remember {
MutableTransitionState(false).apply {
// Start the animation immediately.
targetState = true
}
}
Column {
AnimatedVisibility(visibleState = state) {
Text(text = "Hello, world!")
}
// Use the MutableTransitionState to know the current animation state
// of the AnimatedVisibility.
Text(
text = when {
state.isIdle && state.currentState -> "Visible"
!state.isIdle && state.currentState -> "Disappearing"
state.isIdle && !state.currentState -> "Invisible"
else -> "Appearing"
}
)
}
It is also useful for observing the animation state.它对于观察动画状态也很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.