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

Flutter使用webview_flutter时键盘弹出后底部内边距调整失效怎么修复

解决webview_flutter ^4.4.4键盘弹出后UI适配失效问题

问题根源

你的代码里Scaffold设置了resizeToAvoidBottomInset: false,直接禁用了Flutter系统默认的键盘弹出时UI自动调整逻辑;同时WebViewController缺少平台特定的键盘适配配置,导致WebView内部内容无法响应键盘变化。

具体修复步骤

1. 启用Scaffold的自动适配

将Scaffold的resizeToAvoidBottomInset改为true(也可根据业务需求动态控制):

return Scaffold(
  resizeToAvoidBottomInset: true, // 修改为true
  body: SafeArea(
    child: Obx(
      () => controller.isLoading.value
          ? const Center(child: LoadingWidget())
          : WebViewWidget(controller: controller.webController),
    ),
  ),
);

2. 配置WebViewController的平台适配参数

初始化WebViewController时,分别为Android和iOS添加键盘适配配置:

void controllerInitialize() {
  isLoading.value = true;
  webController = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(Get.theme.colorScheme.surface)
    ..enableZoom(false)
    // Android端:启用adjustResize模式,让WebView随键盘调整尺寸
    ..android.setWebViewSettings(WebViewSettings(
      softInputMode: AndroidSoftInputMode.adjustResize,
    ))
    // iOS端:自动调整滚动视图内边距适配键盘
    ..ios.setWebViewConfiguration(WKWebViewConfiguration()
      ..scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.automatic,
    )
    ..loadRequest(Uri.parse("https://www.google.com/"));
  isLoading.value = false;
}

3. 可选:手动动态适配(备用方案)

如果上述配置仍有问题,可通过MediaQuery监听键盘高度,给WebView添加动态底部padding:

Obx(
  () => controller.isLoading.value
      ? const Center(child: LoadingWidget())
      : Padding(
          padding: EdgeInsets.only(
            bottom: MediaQuery.of(context).viewInsets.bottom,
          ),
          child: WebViewWidget(controller: controller.webController),
        ),
),

效果验证

修改后重新运行,键盘弹出时WebView会自动向上调整,底部内容不会被键盘遮挡,符合预期适配效果。

内容的提问来源于stack exchange,提问作者Flip

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 07:26:35