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.
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!
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With