gpt4 book ai didi

Flutter - 键盘高度的媒体查询始终返回零

转载 作者:行者123 更新时间:2023-12-03 02:42:59 27 4
gpt4 key购买 nike

我正在尝试从屏幕的高度中减去键盘的高度,以便我可以将该值应用于我的 listview 容器的 height 属性。这样我的键盘就不会与我的 ListView 重叠。

double sheetHeight;
double keyboardHeight;

我使用 MediaQuery,它为屏幕高度返回 683,但始终为键盘高度返回 0。
sheetHeight = MediaQuery.of(context).size.height;
keyboardHeight = MediaQuery.of(context).viewInsets.bottom;

这总是使我的 ListView 覆盖整个屏幕,与我想要的相反。为什么 MediaQuery.of(context).viewInsets.bottom 总是返回 0?我认为这可能与上下文有关,但我不确定。还值得注意的是,当键盘最初出现时,我的断点没有被触发。我认为这也是问题的一部分。非常感谢任何输入或资源。
return Card(
child: Container(
height: sheetHeight - keyboardHeight,

为了确定键盘是否可见,我使用了一个 keyboard_visibility 包。如果键盘可见,我使用 MediaQuery 设置高度。如果没有,我将它设置为 0。虽然它返回 true,但键盘高度仍然保持值为 0。这是错误的方法吗?
super.initState();
WidgetsBinding.instance.addObserver(this);
KeyboardVisibilityNotification().addNewListener(
onChange: (bool visible) {
print(visible);
if (visible == true) {
keyboardHeight = MediaQuery.of(context).viewInsets.bottom;
} else {
keyboardHeight = 0.0;
}
}
);

最佳答案

MediaQuery.of(context).viewInsets.bottom

仅当您的键盘在屏幕上可见时才返回值,因此如果您在没有键盘的情况下调用它,它只会返回 0 .

因此,请确保仅在键盘向上时才查询底部。

编辑:

运行这个最小的可重现代码
Widget build(BuildContext context) {
var bottom = MediaQuery.of(context).viewInsets.bottom;
return Scaffold(
appBar: AppBar(),
body: Center(child: TextField(decoration: InputDecoration(hintText: " ViewInsets.bottom = $bottom"))),
);
}

输出:

enter image description here

关于Flutter - 键盘高度的媒体查询始终返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58508873/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com