Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get keyboard height in Flutter

Tags:

flutter

dart

Is there any way to get an open keyboard's height in Flutter? I'm trying to pad a bottomSheet widget by the height of the keyboard while it's open.

like image 289
Alec Sibilia Avatar asked Mar 14 '18 03:03

Alec Sibilia


People also ask

How do you get the keyboard height in flutter?

To know about the keyboard height, you can just check for the bottom property of viewInsets , when the keyboard is onscreen, this will hold the height of keyboard else zero. Note: The bottom property may have value even if some other system ui obscures the flutter ui from bottom. Hope that helps!

How do you define height in flutter?

To modify the width or height of a card you can wrap it in a Container Widget and provide height and/or width properties to it.

What is resizeToAvoidBottomInset?

Use resizeToAvoidBottomInset to specify if the body should resize when the keyboard appears. Follow this answer to receive notifications. edited Jul 15, 2021 at 15:55.


2 Answers

Usually viewInsets provides data about any system ui that obscures the flutter ui. To know about the keyboard height, you can just check for the bottom property of viewInsets, when the keyboard is onscreen, this will hold the height of keyboard else zero.

You can check for the viewInsets with MediaQuery like:

MediaQuery.of(context).viewInsets.bottom

Note: The bottom property may have value even if some other system ui obscures the flutter ui from bottom.

Hope that helps!

like image 75
Hemanth Raj Avatar answered Oct 01 '22 16:10

Hemanth Raj


The MediaQuery.of(context).viewInsets solution does not work for me. It always says zero even if keyboard is open. Moreover, looking at the highest-upvoted comment in this answer, it is a bad idea to use it as keyboard indicator.

Thus, here is a one-line solution:

final viewInsets = EdgeInsets.fromWindowPadding(WidgetsBinding.instance.window.viewInsets,WidgetsBinding.instance.window.devicePixelRatio);

Then do whatever you want (e.g. viewInsets.bottom is keyboard height) :)


EDIT: https://api.flutter.dev/flutter/dart-ui/FlutterView-class.html is a good source to see how keyboard affects various kinds of padding.

like image 24
ch271828n Avatar answered Oct 01 '22 16:10

ch271828n