Christopher Mederos
05/10/2023, 7:32 AMChristopher Mederos
05/10/2023, 7:40 AMephemient
05/10/2023, 8:53 AMStylianos Gakis
05/10/2023, 9:19 AMclearFocusOnTap
modifier https://kotlinlang.slack.com/archives/CJLTWPH7S/p1682437025136049?thread_ts=1682337217.318679&cid=CJLTWPH7S and I tried it out in our app and it works perfectly fine. Just need to decide which component will be the one that bears that behavior, for us I slapped it into the background of the screen where I needed this behavior and it simply worked.Christopher Mederos
05/12/2023, 5:23 AMjonathan olds
09/16/2023, 5:01 PMephemient
09/16/2023, 5:02 PMjonathan olds
09/16/2023, 5:04 PMjonathan olds
09/16/2023, 5:05 PMjonathan olds
09/16/2023, 5:06 PMChristopher Mederos
09/18/2023, 3:14 AMmodifier = Modifier.pointerInput(Unit) { detectTapGestures(onTap = { focusManager.clearFocus() }) }
jonathan olds
09/18/2023, 6:25 AMOleksandr Balan
09/18/2023, 2:41 PMdetectTapGestures
and clickable modifier use main pass, which is handled after children consume their gestures.
However according to the Event propagation we could use initial pass, which allows a parent to intercept an event before the child can consume it.
So in theory something like this could work:
fun Modifier.clearFocusOnTap(): Modifier = composed {
val focusManager = LocalFocusManager.current
Modifier.pointerInput(Unit) {
awaitEachGesture {
awaitFirstDown(pass = PointerEventPass.Initial)
val upEvent = waitForUpOrCancellation(pass = PointerEventPass.Initial)
if (upEvent != null) {
focusManager.clearFocus()
}
}
}
}
I have tested it with button & text-field, but I am not sure how it will behave with scrolling and with other elements. Could you please check how it will work for you?
Btw, ExposedDropdownMenu uses the same trick 👀Christopher Mederos
09/18/2023, 11:07 PM