Michal Klimczak
03/22/2022, 4:42 PMMichal Klimczak
03/22/2022, 4:42 PMval interactionSource = remember { MutableInteractionSource() }
BasicTextField(
value = "",
onValueChange = {},
interactionSource = interactionSource,
decorationBox = {
Box(Modifier
.focusable(interactionSource = interactionSource)
.onFocusChanged { println(it) }
) {
}
}
)
Zach Klippenstein (he/him) [MOD]
03/22/2022, 4:43 PMfocusable
and onFocusChanged
should work.Zach Klippenstein (he/him) [MOD]
03/22/2022, 4:43 PMonFocusChanged
to the BasicTextField
itselfMichal Klimczak
03/22/2022, 6:21 PMCsaba Szugyiczki
03/22/2022, 7:09 PMcollectIsFocusedAsState
helper method like this val focused by interactionSource.collectIsFocusedAsState()
Csaba Szugyiczki
03/22/2022, 7:10 PMandroidx.compose.material.TextFieldImpl.kt
Michal Klimczak
03/22/2022, 7:47 PMval interactionSource = remember { MutableInteractionSource() }
val focusedState = interactionSource.collectIsFocusedAsState()
println("A $focusedState")
BasicTextField(
value = "",
onValueChange = {},
interactionSource = interactionSource,
decorationBox = {
MyDecorationBox(interactionSource)
}
)
@Composable fun MyDecorationBox(interactionSource: MutableInteractionSource){
val focusedState = interactionSource.collectIsFocusedAsState()
println("B $focusedState")
}
will only print A, not B, somehowZach Klippenstein (he/him) [MOD]
03/23/2022, 5:55 PMMichal Klimczak
03/23/2022, 7:36 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:24 PMBasicTextField
itself is what is focusable. Internally, there is a Box
around the decorationBox
function and that outer box is what has the focus modifier, which is what the MutableInteractionSource
is passed to. So it ends up looking like this:
BasicTextField
Box(Modifier.âŠ.focusable())
decorationBox
TextInputArea
Zach Klippenstein (he/him) [MOD]
03/23/2022, 8:25 PMdecorationBox
function focusable, since the text field assumes the outer box is focusable âleafâ.Zach Klippenstein (he/him) [MOD]
03/23/2022, 8:26 PMMichal Klimczak
03/23/2022, 8:32 PMMichal Klimczak
03/23/2022, 8:34 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:39 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:42 PM@Composable
fun YourTextField(/* ⊠*/) {
val interactionSource = remember { MutableInteractionSource() }
BasicTextField(
value = "",
onValueChange = {},
interactionSource = interactionSource,
decorationBox = { field ->
InfinitelyComplexDecorationBox(interactionSource, field)
}
)
}
@Composable
private fun InfinitelyComplexDecorationBox(
interactionSource: InteractionSource,
field: @Composable () -> Unit
) {
// have fun
}
Michal Klimczak
03/23/2022, 8:42 PMMichal Klimczak
03/23/2022, 8:43 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:43 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:44 PMMichal Klimczak
03/23/2022, 8:44 PMMichal Klimczak
03/23/2022, 8:44 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:47 PMtoString()
on a MutableState
, which doesnât perform a snapshot-aware read of the internal value. In real code, where youâd actually the state value itself, it would work.Zach Klippenstein (he/him) [MOD]
03/23/2022, 8:47 PMprintln("B ${focusedState.value}")
or val focusedState by interactionSource.collectâŠ
Michal Klimczak
03/23/2022, 8:50 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:55 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 8:55 PMMichal Klimczak
03/24/2022, 7:39 AMZach Klippenstein (he/him) [MOD]
03/24/2022, 3:28 PMlizzaraga
03/24/2022, 3:38 PMlizzaraga
03/24/2022, 3:39 PMZach Klippenstein (he/him) [MOD]
03/24/2022, 3:42 PM