kc_dev
kc_dev

Reputation: 790

Hide soft Keyboard on Drawer open - Jetpack Compose

I have a TextField and a ModalDrawer in a compose screen. I would like to close the soft keyboard when the user opens the drawer, but I haven't been able to figure out how. There is no onOpened lifecycle event that gets triggered in ModalDrawer afaik.

Upvotes: 3

Views: 1594

Answers (1)

Aburg
Aburg

Reputation: 282

You can use the confirmStateChange parameter in rememberDrawerState() and call keyboardController.hide() when the drawerValue becomes DrawerValue.open like this:

val keyboardController = LocalSoftwareKeyboardController.current
val state = rememberDrawerState(
  initialValue = DrawerValue.Closed,
    confirmStateChange = {
       if (it == DrawerValue.Open) {
           keyboardController?.hide()
       }
       true
    }
)

ModalDrawer(
   drawerState = state,
   ...
) {
  ...
}

Upvotes: 4

Related Questions