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

लातों वाला भूत

11/10/2023, 7:47 AM
Hello People, I have added a TextField in Alert Dialog for user input. It works fine in Android. However in iOS on clicking the text field keyboard appears and disappears instantly. It seems like the keyboard scrolls the dialog upwards which in turn dismisses the keyboard again. Has anyone faced this? Adding code in thread.
Copy code
val inputText = mutableStateOf("")

    AlertDialog(
        title = {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                icon?.invoke()
                Text(text = title, fontSize = 20.sp)
            }
        },
        text = {
            MedialTextField(
                value = inputText.value,
                maxLines = maxLines,
                onValueChange = { inputText.value = it },
                label = hint ?: "",
                modifier = textFieldModifier.fillMaxWidth(),
                endIcon = trailingIcon?.let { { it.invoke() } }
            )
        },
        onDismissRequest = onDismissRequest,
        confirmButton = {
            TextButton(
                onClick = { onConfirmation(inputText.value) },
                enabled = canSubmit(inputText.value)
            ) {
                Text(positiveButtonText)
            }
        },
        dismissButton = {
            TextButton(onClick = onDismissRequest) {
                Text(negativeButtonText)
            }
        },
        modifier = modifier,
    )
Copy code
@Composable
fun MedialTextField(
    value: String,
    onValueChange: (String) -> Unit,
    label: String,
    singleLine: Boolean = true,
    maxLines: Int = 1,
    readOnly: Boolean = false,
    error: String? = null,
    interactionSource: MutableInteractionSource? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    endIcon: @Composable (() -> Unit)? = null,
    modifier: Modifier = Modifier
) {
    OutlinedTextField(
        value = value,
        onValueChange = onValueChange,
        label = { Text(text = label) },
        singleLine = singleLine,
        maxLines = maxLines,
        textStyle = MaterialTheme.typography.bodyMedium,
        shape = RoundedCornerShape(12.dp),
        trailingIcon = endIcon,
        leadingIcon = leadingIcon,
        interactionSource = interactionSource ?: remember { MutableInteractionSource() },
        modifier = modifier,
        readOnly = readOnly,
        isError = error != null,
        supportingText = error?.let {
            {
                Text(text = it, fontSize = 10.sp, color = MaterialTheme.colorScheme.error)
            }
        },
        colors = OutlinedTextFieldDefaults.colors(
            focusedLabelColor = MaterialTheme.colorScheme.primary,
            unfocusedLabelColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.60f),
            focusedBorderColor = MaterialTheme.colorScheme.primary,
            unfocusedBorderColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.60f),
        )
    )
}
k

Khanzada Kashif

11/10/2023, 11:12 AM
I can't seem to find it, but this issue was posted on issue tracker and as far as I remember it hasn't been fixed yet.
u

लातों वाला भूत

11/10/2023, 11:14 AM
Oh so for now how do we add a textfield in a dialog?
k

Khanzada Kashif

11/10/2023, 11:16 AM
you can try adding this configuration,
Copy code
ComposeUIViewController(
    configure = {
        onFocusBehavior = OnFocusBehavior.DoNothing
    }
) {}
it might solve it.
u

लातों वाला भूत

11/10/2023, 11:18 AM
I already have this in place
k

Khanzada Kashif

11/10/2023, 11:20 AM
Found the article, it has both the custom solution and the link to issue tracker. https://patrykkosieradzki.com/how-to-fix-keyboard-issues-introduced-in-the-latest-jetpack-compose-1-4-0/
u

लातों वाला भूत

11/10/2023, 11:21 AM
Thanks a lot
k

Khanzada Kashif

11/10/2023, 11:22 AM
Mention not.
u

लातों वाला भूत

11/10/2023, 11:23 AM
This talks about Android. But in my case Android works fine. Its iOS causing the issues.
4 Views