Center a form Without hiding fields when keyboard is open

Flutter tips Published on

Okay, let's break down how to keep your form fields visible when the keyboard pops up in your Flutter app. It's a common headache, but there's a neat trick.

First, you need to make sure your form can actually move.

Enable Scrolling

Wrap your form content inside a SingleChildScrollView. This widget makes your form scrollable if it gets too big for the screen, especially when the keyboard appears and takes up space.

Set the Form Size

To make sure your form takes up the full available screen height (before the keyboard shows up), put it inside a SizedBox and give it the full screen height and width. You can get these using MediaQuery.of(context).size.

Center with Flexible Space

Inside your scrollable SizedBox, use a Column.

Column Setup

Adding Space

Here's the clever part for centering and keyboard handling. Add Spacer(flex: 1) widgets at the top and bottom of your list of form fields within the Column.

What Spacers Do

These Spacer widgets act like flexible empty space. They push your form fields towards the center. When the keyboard opens, the available vertical space for the column reduces. The Spacer widgets then shrink, taking up less room, allowing your form fields to stay visible and scrollable above the keyboard area.

That's it! By combining scrolling, sizing, and flexible spacing with Spacer, your form stays centered when there's room and scrolls nicely when the keyboard appears, preventing fields from being hidden.

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 do form fields sometimes get hidden by the keyboard?

The on-screen keyboard takes up space at the bottom of the screen. If your layout doesn't adjust or scroll, parts of your content, like form fields, can be covered up.

How does SingleChildScrollView help prevent fields from being hidden?

It makes the content inside it scrollable. When the keyboard appears and reduces the available screen space, you can simply scroll the form content to see all the fields.

What role do the Spacer widgets play in centering?

Spacer widgets are flexible empty spaces. By putting them at the top and bottom inside a Column, they push the content (your form fields) towards the center. When the keyboard appears, these spacers shrink, making room for the form fields above the keyboard.

Is resizeToAvoidBottomInset important for this?

Yes, `resizeToAvoidBottomInset` (usually true by default in Scaffold) allows the body to resize or shift up to avoid being covered by the keyboard. This works together with SingleChildScrollView to make the content scrollable when needed.

Read more
You may also be interested in
Change flutter version  blog card image
Change flutter version
Published on 2025-05-12T12:23:31.879Z
Flutter Tip: Make Bottom Popups Fit Your Content (No Extra Space!)  blog card image
Flutter Tip: Make Bottom Popups Fit Your Content (No Extra Space!)
Published on 2025-05-12T08:34:54.520Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved