You need to enable JavaScript to run this app. 如何仅在Floating SliverAppBar完全滑出时显示顶部AppBar阴影?
优惠活动
大模型
产品
解决方案
定价
更多

如何仅在Floating SliverAppBar完全滑出时显示顶部AppBar阴影?

实现双层叠加AppBar的解决方案

需求明确

  • 顶部AppBar设为pinned固定状态
  • 下方为可滑入顶部AppBar下方的Floating SliverAppBar
  • 始终显示组合栏下方的阴影
  • 当第二个SliverAppBar可见时,顶部AppBar不显示二者之间的分割阴影

实现方案与代码

核心思路是通过滚动监听控制顶部AppBar的阴影显示,同时给组合栏外层添加固定阴影容器,配合NestedScrollView完成布局:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const Scaffold(
        body: _DoubleAppBarPage(),
      ),
    );
  }
}

class _DoubleAppBarPage extends StatefulWidget {
  const _DoubleAppBarPage();

  @override
  State<_DoubleAppBarPage> createState() => _DoubleAppBarPageState();
}

class _DoubleAppBarPageState extends State<_DoubleAppBarPage> {
  bool _isBottomAppBarVisible = true;

  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: (notification) {
        if (notification is ScrollUpdateNotification) {
          setState(() {
            // 阈值根据下方AppBar高度调整,示例取默认折叠高度60
            _isBottomAppBarVisible = notification.metrics.pixels < 60;
          });
        }
        return false;
      },
      child: Stack(
        children: [
          NestedScrollView(
            headerSliverBuilder: (context, innerBoxIsScrolled) {
              return [
                SliverAppBar(
                  pinned: true,
                  // 下方AppBar可见时取消顶部阴影
                  elevation: _isBottomAppBarVisible ? 0 : 4,
                  title: const Text('顶部固定栏'),
                  backgroundColor: Colors.white,
                ),
                SliverAppBar(
                  floating: true,
                  elevation: 0,
                  title: const Text('可滑动栏'),
                  backgroundColor: Colors.white,
                  collapsedHeight: 60,
                ),
              ];
            },
            body: ListView.builder(
              itemCount: 50,
              itemBuilder: (context, index) {
                return ListTile(title: Text('列表项 $index'));
              },
            ),
          ),
          // 固定阴影容器,保证组合栏底部始终有阴影
          Positioned(
            top: 120, // 顶部AppBar高度(60) + 下方AppBar折叠高度(60)
            left: 0,
            right: 0,
            height: 4,
            child: Container(
              decoration: BoxDecoration(
                boxShadow: [
                  BoxShadow(
                    color: Colors.grey.withOpacity(0.3),
                    blurRadius: 4,
                    spreadRadius: 1,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

关键细节说明

  • 用NotificationListener监听滚动,动态切换顶部AppBar的elevation值,实现无分割阴影的效果
  • 给下方SliverAppBar设置elevation: 0,避免自身产生多余阴影
  • 通过Stack+Positioned添加固定阴影,确保组合栏底部阴影始终存在
  • 需根据实际AppBar高度调整Positioned的top值和滚动判断阈值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 22:12:15