'Keyboard not being detected. MediaQuery.of(context).viewInsets.bottom always returns 0.0
I have a scaffold with a TextField inside. The keyboard always covers this field when it previously would move the field above the keyboard. This is happening on all the pages in my project.
I've added MediaQuery.of(context).viewInsets.bottom to my build method and it always returns 0.0. When the keyboard comes up there is no rebuild. I've tried settings resizeToAvoidBottomInset to true and false with no change. I've tried wrapping it in a Scrollable widget, no change.
Everything works fine in IOS, this only affects android build.
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel unknown, v1.10.15, on Mac OS X 10.15.1 19B88, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
[✓] Android Studio
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.40.2)
[✓] Connected device (1 available)
Solution 1:[1]
I found the reason for this problem, but I don't know why. When the control is in Scaffold, the appearance and disappearance of the keyboard will not rebuild the body in Scaffold. You can try to replace Scaffold with Material to observe. If you don’t want to replace Scaffold, one way I can find is:
final bottom= EdgeInsets.fromWindowPadding(
WidgetsBinding.instance.window.viewInsets,
WidgetsBinding.instance.window.devicePixelRatio).bottom;
Solution 2:[2]
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.
This package gives simple in use provider for getting info down the tree https://pub.dev/packages/flutter_keyboard_size
Or, you can mutate info about keyboard size down the tree by yourselves (you can look the code in the package).
Solution 3:[3]
When the keyboard opens, MediaQuery.of(context).viewInsets.bottom changes to reflect the new bottom (which is now at the top of the keyboard).
However, the Android app may be in fullscreen:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
<item name="android:windowFullscreen">true</item>
</style>
<color name="xxx">#FFFF00</color>
</resources>
When this is the case, viewInsets.bottom will always be 0.0, even when the keyboard is open,
Solution 4:[4]
Setting android:windowSoftInputMode="adjustResize" for the Activity hosting the Flutter screen in AndroidManifest.xml will fix the issue.
Solution 5:[5]
I need to mix flutter and Android, the situation may be more complicated, <item name="android:windowFullscreen">false</item> and SystemChrome.setEnabledSystemUIOverlays([]) do not work for me.
Finally the setting android:windowSoftInputMode="adjustResize" in AndroidManifest.xml worked for me.
Solution 6:[6]
try MediaQuery.of(context).viewInsets.bottom with scaffold context not child widgets one.
It works for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? |
| Solution 2 | awaik |
| Solution 3 | CHENNAKAMPALLI CHETHAN REDDY |
| Solution 4 | |
| Solution 5 | Nullable |
| Solution 6 | Vinitha |
