简体   繁体   English

未检测到键盘。 MediaQuery.of(context).viewInsets.bottom 总是返回 0.0

[英]Keyboard not being detected. MediaQuery.of(context).viewInsets.bottom always returns 0.0

I have a scaffold with a TextField inside.我有一个里面有一个 TextField 的脚手架。 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.我已将 MediaQuery.of(context).viewInsets.bottom 添加到我的构建方法中,它始终返回 0.0。 When the keyboard comes up there is no rebuild.当键盘出现时,没有重建。 I've tried settings resizeToAvoidBottomInset to true and false with no change.我已经尝试将 resizeToAvoidBottomInset 设置为 true 和 false 而没有任何变化。 I've tried wrapping it in a Scrollable widget, no change.我试过把它包装在一个 Scrollable 小部件中,没有改变。

Everything works fine in IOS, this only affects android build.在 IOS 中一切正常,这只影响 android 构建。

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)

I've had this issue recently.我最近遇到了这个问题。 Do you have fullscreen set to true?您是否将全屏设置为true? If fullscreen is enabled, MediaQuery.viewInsets.bottom will return 0.0.如果启用全屏,MediaQuery.viewInsets.bottom 将返回 0.0。 Since you mentioned the problem only occurs in android, It's possible you set the fullscreen property in the native android files.由于您提到该问题仅发生在 android 中,因此您可以在本机 android 文件中设置 fullscreen 属性。 Check styles.xml检查styles.xml

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

Other way to remove fullscreen from the app is SystemChrome.setEnabledSystemUIOverlays([]) in flutter page.从应用程序中删除全屏的其他方法是 Flutter 页面中的SystemChrome.setEnabledSystemUIOverlays([])

One more thing you might want to check is resizeToAvoidBottomPadding: to false in Scaffold.您可能要检查的另一件事是resizeToAvoidBottomPadding:在 Scaffold 中设置为 false。

The issue has been raised in Flutter issues as well: https://github.com/flutter/flutter/issues/25050 Flutter 问题中也提出了该问题: https ://github.com/flutter/flutter/issues/25050

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.当控件在 Scaffold 中时,键盘的出现和消失不会在 Scaffold 中重建 body。 You can try to replace Scaffold with Material to observe.您可以尝试将 Scaffold 替换为 Material 来观察。 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;

In case of complicated widget tree MediaQuery.of(context).viewInsets.bottom gives null even if the keyboard is open.如果是复杂的小部件树 MediaQuery.of(context).viewInsets.bottom 即使键盘打开也会给出 null 。 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这个包提供了简单的使用提供程序,用于从树中获取信息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).或者,您可以自己更改有关键盘大小的信息(您可以查看包中的代码)。

When the keyboard opens, MediaQuery.of(context).viewInsets.bottom changes to reflect the new bottom (which is now at the top of the keyboard).当键盘打开时,MediaQuery.of(context).viewInsets.bottom 会更改以反映新的底部(现在位于键盘的顶部)。

However, the Android app may be in fullscreen:但是,Android 应用程序可能处于全屏模式:

<?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,在这种情况下, viewInsets.bottom将始终为0.0 ,即使键盘处于打开状态,

in AndroidManifest.xml为托管 Flutter 屏幕的 Activity 设置android:windowSoftInputMode="adjustResize"将解决此问题。

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.我需要混合使用flutter和Android,情况可能更复杂, <item name="android:windowFullscreen">false</item>SystemChrome.setEnabledSystemUIOverlays([])对我不起作用。

Finally the setting android:windowSoftInputMode="adjustResize" in AndroidManifest.xml worked for me.最后, AndroidManifest.xml中的设置android:windowSoftInputMode="adjustResize"对我有用。

try MediaQuery.of(context).viewInsets.bottom with scaffold context not child widgets one.使用脚手架上下文而不是子小部件之一尝试 MediaQuery.of(context).viewInsets.bottom。

It works for me.这个对我有用。

See if you have useInheritedMediaQuery set to true in MaterialApp .查看您是否在MaterialApp useInheritedMediaQuery设置为true Set it to false if so.如果是这样,请将其设置为false 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.因此,例如,如果您在应用程序启动时立即弹出键盘, MediaQuery.of(context).viewInsets.bottom的值与键盘的大小相同,但由于该上下文现在适用于儿童,它永远不会变化,即使键盘折叠。 So, my simple solution when I faced the issue was to set useInheritedMediaQuery to false , which is the default in flutter apps these days.因此,当我遇到问题时,我的简单解决方案是将useInheritedMediaQuery设置为false ,这是目前 Flutter 应用程序中的默认设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter MediaQuery.of(context).size.width 检查设备大小总是在 iOS 模拟器中返回相同的错误值 - Flutter MediaQuery.of(context).size.width to check device-size always returns same wrong value in the iOS Simulator 当我将 MediaQuery.of(context).padding.top 与 appBar 一起使用时,它返回 0.0。 这是为什么? - MediaQuery.of(context).padding.top is returning 0.0 when I'm using it with appBar. Why is that? 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error Flutter Get.width 或初始化 MediaQuery.of(context).size 在 Release 模式下不起作用 - Flutter Get.width or initialize MediaQuery.of(context).size not working in Release mode Android GPS追踪器始终返回0.0 - Android GPS tracker returns 0.0 always Android获取经度和纬度始终返回0.0 - Android getting Longitude and latitude always returns 0.0 MediaRecorder.getMaxAmplitude()始终返回0.0 - MediaRecorder.getMaxAmplitude() always returns 0.0 未检测到Android软键盘字母 - Android soft keyboard letters not being detected 在 Android 模拟器上,Android Location getSpeed 始终返回 0.0 - Android Location getSpeed always returns 0.0 when on Android Emulator 在Android应用中使用LocationListener将GPS坐标始终设置为0.0-0.0 - GPS Coords being always 0.0-0.0 using LocationListener in a Android App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM