You need to enable JavaScript to run this app. Flutter iOS端嵌套在CustomScrollView的WebView内容不跟随页面滚动问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS端嵌套在CustomScrollView的WebView内容不跟随页面滚动问题求助

问题描述

我在网上搜索了很久都没有找到解决方案,恳请大家帮忙提供实现思路。我不太好描述遇到的错误,查看相关图片即可清晰了解,该问题仅在iOS系统上出现。

相关截图

  • WebView异常截图:WebView异常
  • 滚动异常截图:滚动异常

相关代码

class xxx extends State<xxx> {

  ...

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [

        ...

        SizedBox(height: 8),
        Expanded(
          child: StreamBuilder<bool>(
            stream: bloc.loadingStream,
            initialData: false,
            builder: (context, snapshot) {
              Widget loadingWidget = SizedBox.shrink();
              if (snapshot.data) {
                loadingWidget = Container(
                  alignment: Alignment.center,
                  color: Colors.black45,
                  child: RefreshProgressIndicator(),
                );
              }

              return Stack(
                children: [
                  CustomScrollView(
                    slivers: [
                      SliverToBoxAdapter(
                        child: _buildContent(),
                      ),
                      SliverFillRemaining(
                        hasScrollBody: false,
                        child: _buildBottomButton(),
                      ),
                    ],
                  ),
                  loadingWidget,
                ],
              );
            },
          ),
        ),
      ],
    );
  }

  /// 中間內文
  Widget _buildContent() {
    return Container(
      padding: EdgeInsets.symmetric(
        vertical: 12,
        horizontal: 15,
      ),
      margin: EdgeInsets.only(
        left: 20,
        right: 20,
        top: 6,
        bottom: 18,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        border: Border.all(
          color: Color(0xFFf0f7fc),
          width: 1,
        ),
        boxShadow: [
          BoxShadow(
            color: Color(0xFFe6f3fa),
            spreadRadius: 2,
            blurRadius: 1,
          ),
          BoxShadow(
            color: Color(0xFFa9d5f5),
            spreadRadius: 1.5,
            blurRadius: 5,
          ),
        ],
      ),
      child: Column(
        children: [
          
          ...

          _buildRegulationInfo(),
          SizedBox(height: 20),
        ],
      ),
    );
  }

  ...
  
  /// 購物條款
  Widget _buildRegulationInfo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildTitle(S.of(context).Checkout_Regulation_Title),
        SizedBox(height: 12),
        _buildRegulationWeb(),
        SizedBox(height: 4),
        _buildAgreeCheckBox(),
      ],
    );
  }

  ...

  /// 購物條款WebView
  Widget _buildRegulationWeb() {
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        height: 300,
        margin: EdgeInsets.symmetric(horizontal: 8),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
          border: Border.all(
            color: Color(0xFFaaaaaa),
            width: 1,
          ),
        ),
        child: StreamBuilder<RegulationBean>(
          stream: bloc.regulationStream,
          builder: (context, snapshot) {
            if (!snapshot.hasData) return Container();
            var data = snapshot.data;

            String cssStyle = '';

            // IOS 文字過小
            if (Platform.isIOS) {
              cssStyle = """
                <head>
                  <meta name="viewport" content="width=device-width, initial-scale=1.0">
                </head>
                <style type="text/css">
                  * {
                    font-size: 13px;
                  }
                </style>
              """;
            }

            var url = Uri.dataFromString(
              "<!DOCTYPE html><html>$cssStyle<body>${data.content}</body></html>",
              mimeType: 'text/html',
              encoding: Encoding.getByName('utf-8'),
            ).toString();

            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: WebView(
                initialUrl: url,
                javascriptMode: JavascriptMode.unrestricted,
                gestureRecognizers: Set()
                  ..add(
                    Factory<VerticalDragGestureRecognizer>(
                      () => VerticalDragGestureRecognizer(),
                    ), // or null
                  ),
              ),
            );
          },
        ),
      ),
    );
  }

  ...

}

版本信息

  • Flutter版本:
• Flutter version 2.0.6 at /Volumes/Backup/flutter
• Framework revision 1d9032c7e1 (6 周前), 2021-04-29 17:37:58 -0700
• Engine revision 05e680e202
• Dart version 2.12.3
  • iOS系统版本:14.x、12.x

已执行的调试尝试

  • 无论将WebView放在ScrollView的任意位置,问题均无改善
  • 将ScrollView替换为ListView可解决该问题
  • 删除WebView的gestureRecognizers ...相关代码也可解决问题,但会导致WebView本身无法滚动

解决方案

该问题本质是iOS端WebView与CustomScrollView的垂直手势竞争优先级异常导致的,有3种可行修复方案:

方案1:替换手势识别器

将你当前使用的VerticalDragGestureRecognizer替换为EagerGestureRecognizer,可以让WebView的滚动手势优先级高于父布局,直接解决手势冲突问题,修改代码如下:

gestureRecognizers: Set()
  ..add(
    Factory<EagerGestureRecognizer>(
      () => EagerGestureRecognizer(),
    ),
  ),

方案2:动态手势分配

如果担心EagerGestureRecognizer会完全拦截父布局手势,可以搭配JS通道实现滚动状态联动:

  1. 给WebView添加JavascriptChannel,接收HTML页面的滚动位置回调
  2. 在HTML中添加滚动监听,滚动到顶部/底部时通知Flutter侧
  3. Flutter侧根据WebView的滚动位置,动态切换手势的响应方,WebView未滚动到边界时自行处理滚动,到边界后将手势交还给CustomScrollView

方案3:升级依赖

你当前使用的Flutter 2.0.6和对应版本的webview_flutter存在较多iOS端手势兼容问题,直接升级webview_flutter到最新稳定版,该问题大概率会被官方修复逻辑直接覆盖解决。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 14:54:02