You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中MediaQuery.of(context).viewInsets在Redmi 7设备上失效导致输入框被键盘遮挡的问题求助

Flutter中MediaQuery.of(context).viewInsets在Redmi 7设备上失效导致输入框被键盘遮挡的问题求助

各位Flutter开发者好,我最近遇到一个头疼的设备兼容性问题,实在搞不定了,想请教下大家的解决方案:

我在做一个聊天类的Flutter应用,需要实现一个始终固定在页面底部的输入框,当键盘弹出时,输入框能自动上移到键盘上方,避免被遮挡。我写的代码逻辑如下:

Padding(
  padding: EdgeInsets.only(
    left: AppSize.s8,
    right: AppSize.s8,
    bottom: MediaQuery.of(context).viewInsets.bottom > 0 
        ? MediaQuery.of(context).viewInsets.bottom 
        : MediaQuery.of(context).padding.bottom + AppSize.s8,
  ),
  child: ResponseInput(
    messageController: model.messageController,
    onPressed: () => viewModel.sendMessage(() => _scrollToChatSection(context)),
  ),
),

这个逻辑在绝大多数设备上都能正常工作,但在Redmi 7这类老设备上出现了问题:当键盘弹出时,输入框直接被键盘遮挡,没有按照预期上移。

我特意打了日志来排查键盘状态变化,日志显示系统确实检测到了键盘高度的变化,但MediaQuery.of(context).viewInsets.bottom好像没有同步更新,导致padding规则失效。日志内容如下:

D/InputConnectionAdaptor( 7052): The input method toggled cursor monitoring on
D/InputConnectionAdaptor( 7052): The input method toggled cursor monitoring off
D/InputConnectionAdaptor( 7052): The input method toggled cursor monitoring on
D/KeyboardChannel( 7052): Keyboard height changed: 525
I/flutter ( 7052): Native keyboard height: 525
I/flutter ( 7052): TextField focus: false
D/InputConnectionAdaptor( 7052): The input method toggled cursor monitoring off
D/KeyboardChannel( 7052): Keyboard height changed: 0
I/flutter ( 7052): Native keyboard height: 0

有没有大佬遇到过类似的老设备兼容性问题?或者有没有其他更可靠的方法来获取键盘高度,确保输入框在键盘弹出时能正确上移?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 13:19:31