kotlin 在Android Jetpack Compose中有效地消除和处理Popup组件

9wbgstp7  于 2023-05-29  发布在  Kotlin
关注(0)|答案(1)|浏览(346)

如何通过事件有效地处理和关闭Android Jetpack Compose中的Popup widget?通过布尔标志使弹出窗口不可见会抛出不正确解除的警告。

Input channel object '5668e46 Pop-Up Window (client)' was disposed without first being removed with the input manager!
if (showOverlay) {
            println(buttonPosition)
           val popup  = Popup(
             
                onDismissRequest = {
                    showOverlay = false
                },

                offset = IntOffset(
                    LocalConfiguration.current.screenWidthDp, buttonPosition.y.toInt() + 48
                )
            ) {
                Box(
                    modifier = Modifier
                        .background(Color.LightGray.copy(alpha = 0.7F))
                        .padding(16.dp)

                ) {
                  // Content
                }
            }

        }
azpvetkf

azpvetkf1#

你得到警告的原因是因为弹出窗口没有经过必要的清理过程就被释放了。要正确地释放弹出窗口,你需要使用在重组过程中记住的状态。以下是如何在jetpack compose中实现sate:

var showOverlayState by remember { //make sure to use by delegate 
   mutableStateOf(false)
}

创建状态后:

if (showOverlay) {
    Popup(onDismissRequest = { showOverlay = false }) {
        Box(
            modifier = Modifier
                .background(Color.LightGray.copy(alpha = 0.7F))
                .padding(16.dp)
        ) {
            // Content
        }
    }
}

相关问题