Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 922
DanG Avatar asked Dec 05 '19 14:12

DanG


3 Answers

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

like image 66
Gabriel Avatar answered Nov 18 '22 23:11

Gabriel


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;
like image 37
谢艺志 Avatar answered Nov 19 '22 00:11

谢艺志


See if you have useInheritedMediaQuery set to true in MaterialApp. Set it to false if so. This is caused because that property forces widgets to use an inherited context. So if you, for example, pop the keyboard immediately when the app starts, the value of MediaQuery.of(context).viewInsets.bottom is the same as the size of the keyboard, but since that context now works for children, it never changes, even when the keyboard collapses. So, my simple solution when I faced the issue was to set useInheritedMediaQuery to false, which is the default in flutter apps these days.

like image 1
Abhi Shake Avatar answered Nov 18 '22 23:11

Abhi Shake