keyboardShowingIn static method
Returns whether any software keyboard is showing in the given context
by depending on a MathKeyboardViewInsetsQuery and consulting the
WidgetsBinding window.
This is useful to you when you want to take an action whenever any keyboard is currently opened up. The math keyboard package and sadly not change the view insets on the WidgetsBinding instance's Window, which means that you need to use this helper for checking if a math keyboard is currently opened up.
Note that we cannot consult the MediaQuery for regular software keyboards as widgets like Scaffold consume the bottom inset. We can, however, safely depend on MathKeyboardViewInsetsQuery as we know that the bottom inset will not be consumed going down the tree.
See mathKeyboardShowingIn for a method that reports only whether a math keyboard is showing, ignoring other software keyboards.
Example
@override
Widget build(BuildContext context) {
final isAnyKeyboardShowing =
MathKeyboardViewInsetsQuery.keyboardShowingIn(context);
}
isAnyKeyboardShowing
will now tell you whether there any software
keyboard is showing, i.e. the default software keyboard on Android and
iOS or a math keyboard.
Of course, you need to make sure that there is a MathKeyboardViewInsets
widget as a parent of the content
.
By default, this will only notify you about changes to the
MathKeyboardViewInsetsQuery and not about changes to the other software
keyboards. If you want to be notified about changes to the other software
keyboards also , you will have to register a WidgetsBindingObserver and
set state on WidgetsBindingObserver.didChangeMetrics.
See the official WidgetsBindingObserver
example
and make sure to set state on didChangeMetrics
instead of
didChangeAppLifecycleState
:)
Implementation
static bool keyboardShowingIn(BuildContext context) {
final maxInset = max(
of(context).bottomInset,
View.of(context).viewInsets.bottom /
// Note that we obviously do not care about the pixel ratio for our
// > 0 comparison, however, I do want to prevent any future mistake,
// where someone forgets the pixel ratio on the window.
View.of(context).devicePixelRatio,
);
return maxInset > 0;
}