Created
November 25, 2021 11:25
-
-
Save moskvax/636628e6cabab6744e7bee53d1b95f52 to your computer and use it in GitHub Desktop.
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
fun Modifier.clearFocusOnKeyboardDismiss(): Modifier = composed { | |
var isFocused by remember { mutableStateOf(false) } | |
var keyboardAppearedSinceLastFocused by remember { mutableStateOf(false) } | |
if (isFocused) { | |
val imeIsVisible = LocalWindowInsets.current.ime.isVisible | |
val focusManager = LocalFocusManager.current | |
LaunchedEffect(imeIsVisible) { | |
if (imeIsVisible) { | |
keyboardAppearedSinceLastFocused = true | |
} else if (keyboardAppearedSinceLastFocused) { | |
focusManager.clearFocus() | |
} | |
} | |
} | |
onFocusEvent { | |
if (isFocused != it.isFocused) { | |
isFocused = it.isFocused | |
if (isFocused) { | |
keyboardAppearedSinceLastFocused = false | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment