https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
z

zfan93

10/12/2023, 7:23 AM
Input number is incorrect,Who knows what the problem is,thanks
The cursor is chaotic, pay attention to the first digit 0 entered
👍🏻 1
j

Jan

10/12/2023, 7:28 AM
love the music 😉
🦜 2
z

zfan93

10/12/2023, 8:04 AM
😄
e

Elijah Semyonov

10/12/2023, 8:31 AM
Hey! Thanks for the report! Does the bug arise, if you set English language layout as a default one? Then we will know better, which direction to delve in to resolve it.
Created an issue, feel free to add more context in the thread there
a

Alexander Maryanovsky

10/12/2023, 8:54 AM
Are you using a StateFlow or some other delayed mechanism when updating the TextFieldValue?
z

zfan93

10/12/2023, 11:39 AM
the state:
Copy code
class PhoneScreenModel(private val navigator: Navigator) : MyViewModel() {
    val phone = MutableStateFlow("")
    fun updatePhone(newPhone: String) {
        if (newPhone.length <= 11 && newPhone.all { it.isDigit() }) {
            phone.value = newPhone
        }
    }
the textfield:
Copy code
TextField(
                        modifier = Modifier.fillMaxWidth().padding(bottom = 16.dp).border(
                            1.dp,
                            color = MaterialTheme.colorScheme.primary,
                            shape = RoundedCornerShape(8.dp)
                        ),
                        value = phone,
                        onValueChange = { newPhone ->
                            if (newPhone.length <= 11 && newPhone.all { it.isDigit() } && (newPhone.startsWith(
                                    "1"
                                ) || newPhone.isEmpty())) {
                                phoneScreenModel.updatePhone(newPhone)
                                if (newPhone.length == 11) {
                                    keyboardController?.hide()
                                    navigator.push(CodeScreen(newPhone))
                                }
                            }

                        }, colors = TextFieldDefaults.colors(
                            unfocusedContainerColor = MaterialTheme.colorScheme.background,
                            focusedContainerColor = MaterialTheme.colorScheme.background,
                            unfocusedIndicatorColor = Color.Transparent,
                            focusedIndicatorColor = Color.Transparent,
                            focusedTextColor = MaterialTheme.colorScheme.primary,
                            unfocusedTextColor = MaterialTheme.colorScheme.primary,
                        ),
                        shape = RoundedCornerShape(8.dp),
                        placeholder = {
                            Box(
                                modifier = Modifier.fillMaxWidth().height(26.dp),
                                contentAlignment = Alignment.Center
                            ) {
                                Text(
                                    text = "请输入手机号",
                                    color = MaterialTheme.colorScheme.primary
                                )
                            }

                        },
                        textStyle = TextStyle(textAlign = TextAlign.Center, fontSize = 20.sp),
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
                    )
a

Alexander Maryanovsky

10/12/2023, 11:48 AM
Yeah, this is a known issue on the Google side of Jetpack Compose. It should be fixed soon-ish, I believe.
z

zfan93

10/12/2023, 11:52 AM
我发现原生输入软件是没有问题的,我用的三方输入软件会有问题
I found that there is no problem with the native input software, but there may be issues with the third-party input software I use
a

Alexander Zhirkevich

10/12/2023, 11:56 AM
Just a coincidence i guess. You are managing your text field state in a little wrong way. Read the article above, should be helpful
👍🏻 1
e

Elijah Semyonov

10/12/2023, 11:56 AM
I close the issue then.
z

zfan93

10/13/2023, 12:57 AM
i use “mutablestateof” solve this problem,thanks a lot
👍 2
a

Arkadii Ivanov

10/13/2023, 4:12 PM
As already mentioned, this is a known issue: https://kotlinlang.slack.com/archives/C01403U1ZGW/p1672416240342609?thread_ts=1672414132.375969&cid=C01403U1ZGW A solution is to collect the Flow on Main.immediate dispatcher. I believe by default Compose uses just Main.
3 Views