0

Im working with Jetpack Compose and having a ModalBottomSheetLayout with a TextField. While Closing the bottom sheet, the keyboard it not going off.

Keyboard should hide, when ModalBottomSheetLayout dismisses.

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ModalBottomSheetSample() {
    val state = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden,)
    val scope = rememberCoroutineScope()

    BackHandler(state.isVisible) {
        scope.launch {state.hide()}
    }

    ModalBottomSheetLayout(sheetState = state, sheetContent = {
        TextField(value = "",
            onValueChange = {},
            placeholder = { Text(text = "Search") },
            modifier = Modifier
                .padding(10.dp)
                .fillMaxWidth()
        )

        LazyColumn {items(50) {...}}
    }) {
            Button(onClick = { scope.launch { state.show() } }) {
                Text("Click to show sheet")
            }
    }
}

Demo

have referred this question, but this is not applicable for this scenario. As this is based on Composable.

SURYA S R
  • 320
  • 8
  • Maybe this can help: https://github.com/taehwandev/ComposeKeyboardState – c-an Jan 18 '23 at 14:36
  • I can't add an answer so I'll write my code to solve this issue here `LaunchedEffect(Unit) {` `snapshotFlow { state .currentValue }` `.collect { if (it == ModalBottomSheetValue.Hidden) { focusManager.clearFocus()` `keyboardController?.hide() } } }` – tasjapr Jan 19 '23 at 12:58
  • 1
    I solved the issue with this code... ```LaunchedEffect(key1 = state.currentValue) { // For hiding the keyboard & removing the Focus on hiding the sheet. if (state.currentValue == ModalBottomSheetValue.Hidden) { keyboardController?.hide() focusManager.clearFocus() } }``` – SURYA S R Jan 19 '23 at 13:21

0 Answers0