Is anyone able to debug why doesn't my keyboard cl...
# compose
k
Is anyone able to debug why doesn't my keyboard close when I invoke
onDismissRequest
. Callback is called, however, keyboard doesn't clear. It does clear on back press. 🧵
Copy code
@Composable
fun OtpDialog(state: OtpDialogState) {

    val focusRequester = remember { FocusRequester() }
    var isFieldFocused by remember { mutableStateOf(false) }
    val content = state.content

    BackHandler { if (isFieldFocused) focusRequester.freeFocus() }

    if (state.visible && content != null) {
        OtpAutoFill(lifecycleOwner = LocalLifecycleOwner.current, otpDialogState = state)
        Dialog(
            onDismissRequest = {
                focusRequester.freeFocus()
            },
            properties = state.properties
        ) {
            Card(colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceContainerHigh)) {
                Column(
                    modifier = Modifier.padding(LargePadding),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(DefaultPadding)
                ) {
                    content.title?.let {
                        Text(
                            text = it,
                            style = MaterialTheme.typography.headlineSmall,
                            modifier = Modifier.fillMaxWidth()
                        )
                    }
                    OtpTextField(
                        modifier = Modifier
                            .padding(top = DefaultPadding)
                            .focusRequester(focusRequester)
                            .onFocusChanged { isFieldFocused = it.isFocused },
                        otpText = state.password,
                        onOtpTextChange = { password -> state.updateOneTimePassword(password) }
                    )
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = MediumPadding),
                        horizontalArrangement = Arrangement.End
                    ) {
                        content.negativeButtonText?.let {
                            TextButton(onClick = content.onNegativeButton ?: state::hide) { Text(text = it) }
                        }
                        state.content?.positiveButtonText?.let {
                            TextButton(onClick = content.onPositiveButton) { Text(text = it) }
                        }
                    }
                }
            }
            LaunchedEffect(Unit) { focusRequester.requestFocus() }
        }
    }
}