简体   繁体   English

如何在 Jetpack Compose 中使小部件不可见?

[英]How to make a widget invisible in Jetpack Compose?

I'm trying to show and hide ProgressIndicator in a column.我正在尝试在列中显示和隐藏ProgressIndicator the problem is when I want to hide the ProgressIndicator , the space between other widgets will be removed too (like View.GONE ) but I want to keep widget size (like View.INVISIBLE )问题是当我想隐藏ProgressIndicator时,其他小部件之间的空间也将被删除(如View.GONE )但我想保持小部件大小(如View.INVISIBLE

example:例子:

@Composable
fun Main(isLoading: Boolean) {
    Column {
        Text(text = "Text")

        if (isLoading) {
            CircularProgressIndicator()
        }

        Button(onClick = { /*clicked*/ }, content = { Text(text = "Button") })    
    }
}

I found a solution but I'm not sure if it's the right way.我找到了一个解决方案,但我不确定它是否是正确的方法。

if (isLoading) {
    CircularProgressIndicator()
} else {
    Spacer(modifier = Modifier.height(40.dp))
}

Is there any other way for making the widget invisible like View.INVISIBLE ?有没有其他方法可以像View.INVISIBLE一样使小部件不可见?

How can I get widget size to set Spacer size?如何获取小部件大小以设置Spacer大小?

Thanks谢谢

Your solution is correct, but you could also wrap your progress indicator in a Box of the expected size您的解决方案是正确的,但您也可以将进度指示器包装在预期大小的 Box 中

Box(modifier = Modifier.height(40.dp) {
    if (condition) {
        CircularProgressIndicator()
    }
}

Use Alpha Zero , this is mentioned in the comments by @commonsware, as you do not need to know the size about the space size, unlike to Spacer() composable which needs a specific size and in some cases this may be hard to know.使用 Alpha Zero ,@commonsware 在评论中提到了这一点,因为您不需要知道空间大小的大小,不像 Spacer() 需要特定大小的可组合,在某些情况下这可能很难知道。

val commentsAlpha = if (condition) 1f else 0f
modifier = Modifier
            .alpha(commentsAlpha)

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.它对于观察动画状态也很有用。

利用:

Spacer(modifier = Modifier.height(24.dp))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM