Listen keyboard visibility state

Flutter tips Published on

What's the problem?

Sometimes, when you type in an app, the keyboard pops up and can cover things on the screen, like buttons at the bottom. This can be annoying because you might want to see these things, or maybe you want to hide them on purpose so they don't get in the way of typing.

The Simple Solution

Flutter gives you a way to know if the keyboard is showing or not without adding any extra libraries or plugins. You can build a special widget that watches for these changes.

How It Works (Without Plugins)

We use something called WidgetsBindingObserver. This is like a helper that tells your widget when certain things happen in the app's layout, like when the keyboard appears or disappears.

Key Idea

By checking the bottom space used by the screen (viewInsets.bottom), you can tell if the keyboard is open. If viewInsets.bottom is greater than 0, it means the keyboard (or other system UI like a navigation bar) is taking up space at the bottom. For keyboards, this value changes.

Building the Listener Widget

We create a StatefulWidget called KeyboardVisibility.

Inside its state, we mix in WidgetsBindingObserver.

Important Steps:

How to Use It

Wrap the part of your app that needs to react to the keyboard state changes with this KeyboardVisibility widget. You provide a function to onKeyboardStateChanged that will run whenever the keyboard shows or hides.

Example Code

KeyboardVisibility(
  onKeyboardStateChanged: (state) => _handleKeyboardStateChange(state), // Your function
  child: YourMainContentWidget(), // The part of your UI that needs watching
)

This makes it easy to show or hide elements, resize layouts, or do anything else you need when the keyboard state changes.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why would I want to know if the keyboard is visible?

Sometimes the keyboard covers important UI elements, like buttons at the bottom of the screen. Knowing if it's open allows you to adjust your layout, hide covered items, or scroll to keep input fields visible.

Do I need to use a special plugin for this?

No, the method described uses built-in Flutter features like `WidgetsBindingObserver` and `viewInsets`, so no external plugins are needed.

How does the code detect if the keyboard is open?

It listens for screen layout changes using `WidgetsBindingObserver` and checks the `viewInsets.bottom` property. If this value is greater than zero, it indicates that system UI, typically the software keyboard, is taking up space at the bottom.

What is the `onKeyboardStateChanged` function for?

This is a function you provide to the `KeyboardVisibility` widget. It gets called whenever the keyboard shows or hides, giving you the current state (visible or hidden) so you can update your UI accordingly.

Read more
You may also be interested in
Check user subcription renewal status  blog card image
Check user subcription renewal status
Published on 2025-05-12T09:14:20.491Z
Replace type code with Union type  blog card image
Replace type code with Union type
Published on 2025-05-12T09:03:17.217Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved