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

滚动时实现AppBar被内容覆盖,规避SliverAppBar的收缩特性

实现滚动时AppBar被内容覆盖的效果

你要的是滚动页面时内容从上方覆盖AppBar的效果,不用SliverAppBar的话,可以直接调整你现有Stack的布局逻辑:

  • 把CustomAppBar放在Stack的下层,保持位置在顶部
  • 让ListView占据整个Stack空间,同时给ListView设置顶部内边距(等于AppBar的高度),确保初始状态下内容在AppBar下方
  • 滚动时,ListView的内容会自然向上滚动,覆盖下层的AppBar

修改后的代码如下:

Stack(
  children: [
    // 下层:AppBar,始终固定在顶部
    Positioned.fill(
      top: 0,
      child: Align(
        alignment: Alignment.topCenter,
        child: SizedBox(
          height: kToolbarHeight + context.padding.top,
          child: CustomAppBar(
            title: TranslationConstants.dashboard,
            hasDrawer: true,
            onLeadingTap: () => setState(() {
              print("Leading");
              zoomControllerIconChanger = true;
              drawerController.toggle?.call();
            }),
          ),
        ),
      ),
    ),
    // 上层:ListView,初始内容避开AppBar,滚动时覆盖AppBar
    Positioned.fill(
      top: 0, // 让ListView占满整个Stack区域
      child: ListView.builder(
        padding: EdgeInsets.only(
          top: kToolbarHeight + context.padding.top, // 设置顶部内边距,避开AppBar
        ),
        itemCount: 100,
        itemBuilder: (ctx, index) {
          return ListTile(title: Text('ELEMENT $topPosition -> $index'));
        },
      ),
    ),
  ],
),

关键改动说明:

  1. 将ListView的top属性改为0,让它占据整个Stack空间
  2. 给ListView添加顶部内边距,保证初始状态下列表内容不会被AppBar遮挡
  3. 借助Stack的层级关系,ListView在上层,滚动时内容会自然覆盖下层的AppBar,完全匹配你想要的交互效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:52:08