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

Flutter NestedScrollView中ScrollOnExpand触发时异常滚动至顶部问题

问题:NestedScrollView中ScrollOnExpand触发时异常滚动到顶部

在NestedScrollView中使用Expandable包的ScrollOnExpand组件,原本是为了让展开的部件超出屏幕范围时能自动滚动展示完整内容,但遇到一个问题:当需要展开的部件位于屏幕上方区域时,触发ScrollOnExpand会导致页面直接滚动到顶部。

相关代码如下:

NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return <Widget>[
          SliverAppBar(
            expandedHeight: 200.0,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('SliverAppBar'),
            ),
            pinned: true,
          ),
        ];
      },
      body: CustomScrollView(
        slivers: [
          SliverList.builder(
            itemCount: 20,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                key: ValueKey('test1 $index'),
                margin: EdgeInsets.all(30),
                color: Colors.grey,
                child: ExpandableNotifier(
                  key: ValueKey('test2 $index'),
                  child: ScrollOnExpand(
                    key: ValueKey('test3 $index'),
                    child: Column(
                      key: ValueKey('test4 $index'),
                      children: [
                        ExpandableTheme (
                          data:ExpandableThemeData(
                            animationDuration: Duration(milliseconds: 1000),
                          ),
                          child: ExpandablePanel(
                            key: ValueKey('test5 $index'),
                            header: Text('test $index'),
                            collapsed: Text('test collapsed $index'),
                            expanded: SizedBox(height:100,child: Text('test expanded $index')),
                          ),
                        ),
                        ExpandableButton(child: Text('button $index') ,)
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
        ],
      ),
    )

问题演示动图

曾在该包的代码仓库找到类似问题,但给出的解决方案无法解决当前问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 06:36:01