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.
- 737
- 2
- 8
- 22
7 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!
- 28,663
- 8
- 86
- 76
-
4Thanks a lot for the answer, but do you know how to get the keyboard height when the keyboard is not onscreen? I find many native apps can show a container which has the same height of the keyboard in advance when the keyboard is not onscreen. Cheers. – sgon00 May 05 '19 at 16:51
-
@sgon00 yes you are right. did you find how to find the height of the keyboard before its open? – Isuru Bandara May 01 '21 at 06:04
-
1NOT WORKED FOR ME. THANKS A LOT. – Kamlesh Sep 06 '21 at 08:11
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.
- 13,130
- 3
- 38
- 64
-
3how can we get keyboard height without opening keyboard. I requirement is related to open a emojis box as overlay of keyboard height which should be same as keyboard height. Any suggestion, please share. Thanks. – Kamlesh Mar 10 '21 at 07:59
-
1
-
1100% This is the solution to use! Other solutions occasionally said 0 when the keyboard was open for me, weirdly unreliable. – BeniaminoBaggins Dec 28 '21 at 05:03
-
user it as margin or padding like this > margin: EdgeInsets.fromWindowPadding(WidgetsBinding.instance!.window.viewInsets,WidgetsBinding.instance!.window.devicePixelRatio), – Go Goal Mar 28 '22 at 18:47
-
This one work for me: https://pub.dev/packages/keyboard_utils
- 922
- 1
- 9
- 12
-
Unfortunately, this does not work for me. I always get an incorrect value which leads to an ugly offset. – DarkMath Mar 13 '22 at 12:33
In case of complicated widget tree MediaQuery.of(context).viewInsets.bottom gives null even if the keyboard is open. So, we have to mutate values down the tree.
I made the package that provides all needed info down the tree https://pub.dev/packages/flutter_keyboard_size
Welcome to use and in case you find bugs or want to extend functionality please add the issue https://github.com/awaik/flutter_keyboard_size/issues
- 7,222
- 1
- 34
- 40
In case you need to get the keyboard height even when the keyboard is not open, you can use the flutter_persistent_keyboard_height package (note: it was created by me).
First thing you need to do is wrap a widget from children of which you want to get the keyboard height with PersistentKeyboardHeightProvider. Wrap your app widget (perhaps MaterialApp) if you want to get keyboard height from all widgets.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Persistent Keyboard Height Example',
home: const FlutterPersistentKeyboardHeightExample(),
builder: (context, child) => PersistentKeyboardHeightProvider(
child: child!,
),
);
}
}
And after that you can use the PersistentKeyboardHeight.of(context).keyboardHeight to get the height.
- 167
- 3
- 12
-
1Thanks, works like a charm! Do note that when using another builder as a child e.g. FutureBuilder, rename the "context" variable to prevent a duplicate – Tox May 14 '22 at 14:52
If MediaQuery.of(context).viewInsets.bottom shows 0.0, this should work:
First, go into your Scaffold and set this:
resizeToAvoidBottomInset: false,
THEN you can check the height of the keyboard in this way:
MediaQuery.of(context).viewInsets.bottom,
- 1,102
- 6
- 14
In my case nothing worked, but I needed screen height without keyboard height and LayoutBuilder work perfect.
LayoutBuilder(
builder: (context, constraints) => Container(
//maxHeight will change depending on your keyboard visible or not
height:constraints.maxHeight,
),
);
In theory you can do this
LayoutBuilder(
builder: (context, constraints) {
double keyboardHeight = MediaQuery.of(context).size.height - constrains.maxHeight;
}
);
- 37
- 2
-
what is you have unconstrainted layout, How would you deal with it? – Hammad Pervez Nov 18 '21 at 17:22
-
Just make it constrained. It is meant to be used on higher widget tree levels of the screen. And it is fairly easy to make those constrained with device height. – Roman Lychagin Jan 13 '22 at 15:00