Search code examples
androidflutterdartflutter-layout

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

  • I've had this issue recently. Do you have fullscreen set to true? If fullscreen is enabled, MediaQuery.viewInsets.bottom will return 0.0. Since you mentioned the problem only occurs in android, It's possible you set the fullscreen property in the native android files. Check styles.xml

        <item name="android:windowFullscreen">false</item>
    

    Other way to remove fullscreen from the app is SystemChrome.setEnabledSystemUIOverlays([]) in flutter page.

    One more thing you might want to check is resizeToAvoidBottomPadding: to false in Scaffold.

    The issue has been raised in Flutter issues as well: https://github.com/flutter/flutter/issues/25050