admin管理员组

文章数量:1224997

I'm working on a Jetpack Compose screen where users enter a title in a TextField, and a button is enabled only when the title is not empty. The issue is that even though the title state updates (as confirmed by Logcat), the UI does not reflect the changes—the text in the TextField is not displayed, and the button remains disabled.

@Composable
fun TitleScreen(
    modifier: Modifier = Modifier,
    navigateToInitial: () -> Unit,
    navigateToHelp: () -> Unit,
    navigateToUpload: (Uri, String) -> Unit,
    fileUri: Uri
) {
    var title by rememberSaveable { mutableStateOf("") }

    Column(
        modifier = modifier
            .fillMaxSize()
            .background(White)
            .verticalScroll(rememberScrollState()),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        TextField(
            value = title,
            onValueChange = {
                title = it
                Log.d("TitleScreen", "New title: $title") // Log shows correct updates
            },
            label = { Text("Title") },
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
                .padding(bottom = 16.dp, start = 16.dp, end = 16.dp)
                .clip(CircleShape),
            colors = TextFieldDefaults.colors(
                unfocusedContainerColor = TextFieldColor,
                focusedContainerColor = TextFieldColor,
                focusedTextColor = DarkBlack,
                unfocusedTextColor = DarkBlack,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            ),
            maxLines = 1
        )

        Button(
            onClick = { navigateToUpload(fileUri, title) },
            colors = buttonColors(containerColor = Blue, disabledContainerColor = LightGray),
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp, vertical = 24.dp)
                .height(50.dp),
            enabled = title.isNotBlank() // Button remains disabled
        ) {
            Text(text = "Next", fontSize = 25.sp, color = White, fontWeight = FontWeight.Bold)
        }
    }
}

Debugging Attempts:

  1. Checked Logcat – The title state updates correctly.
  2. Replaced rememberSaveable with remember – No effect.
  3. Used BasicTextField instead of TextField – Still no UI update.
  4. Tried displaying title in a Text Composable – The Text updates correctly, but TextField remains blank.
  5. Used LaunchedEffect(title) { Log.d("TitleScreen", title) } – Shows updates, but UI doesn't change.

Why is the TextField not updating with the new value of title, and why does the button remain disabled even though title is updated in the state?

本文标签: androidJetpack Compose TextField state not updating UI when typingStack Overflow