记得设定ImeVisibility时,可以使用Weikun's方法:使用onChange modifier、mutableStateOf和rememberSystemUiController()函数来实现ImeVisibility的变化。
代码示例:
@Composable
fun MyScaffold(content: @Composable () -> Unit) {
val systemUiController = rememberSystemUiController()
val useDarkIcons = MaterialTheme.colors.isLight
val (imeVisible, setImeVisible) = remember { mutableStateOf(false) }
val currentImeHeight = LocalWindowInsets.current.ime
val imePadding =
if (currentImeHeight > 0.dp) with(LocalDensity.current) { currentImeHeight.toDp() } else 0.dp
Box(modifier = Modifier.fillMaxSize()) {
Scaffold(
topBar = { },
content = {
Box(Modifier.padding(bottom = imePadding)) {
content()
}
}
)
// 具体实现ImeVisibility的变化
DisposableEffect(Unit) {
val visibility = remember { mutableStateOf(false) }
val callbacks = object : WindowInsetsAnimation.Callbacks {
override fun onUnapplied() {
visibility.value = false
}
override fun onProgress(
p0: WindowInsets,
p1: MutableList,
): WindowInsets {
visibility.value = true
setImeVisible(p0.isVisible(WindowInsets.Type.ime()))
return p0
}
}
val animation = systemUiController.setImeVisibility(visibility.value)
animation?.registerCallback(callbacks)
onDispose {
animation?.unregisterCallback(callbacks)
}
}
}
}