This code works as expected. The println only prints when the text value changes or the user moves the cursor
val testText = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = testText.value,
onValueChange = {
// should only happen when you type or move cursor
println("onValueChange called")
testText.value = it.copy(
annotatedString = buildAnnotatedString {
append(it.text)
},
)
},
)
However, some strange things happen when I add a style span:
val testText = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = testText.value,
onValueChange = {
// should only happen when you type or move cursor
println("onValueChange called")
testText.value = it.copy(
annotatedString = buildAnnotatedString {
withStyle(SpanStyle(Color.Blue)) {
append(it.text)
}
},
)
},
)
As I type, if my cursor is located in a word that begins with a letter, then the println is continuously called. But if my cursor is in a word that doesn't start with a letter, then the println acts normal. For example, if my text looks like @john{cursor here} is a person or john 9{cursor here}9 test then it's normal. But if my text looks like john{cursor here} is a person then I get the continuous prints.
Why is this happening?