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

如何去除Flutter中SliverAppBar的空白区域?

如何去除SliverAppBar下方的空白区域?

SliverAppBar下方空白

是否有办法去除该空白区域?我已查找相关解决方案但尚未找到,若您知晓方法恳请帮忙。

以下是相关代码:

SliverAppBar(
  shape: const ContinuousRectangleBorder(
      borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(20),
          bottomRight: Radius.circular(20))),
  pinned: pinned,
  snap: snap,
  floating: floating,
  expandedHeight: 40.0,
  backgroundColor: Color(0xFF2d3b96),
  flexibleSpace: const FlexibleSpaceBar(
    title: Text(
      "Presensi Pegawai",
      maxLines: 1,
      textAlign: TextAlign.center,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
        fontWeight: FontWeight.bold,
        color: Colors.white,
        fontSize: 22.0,
        fontFamily: "IBM-Plex-Sans",
      ),
    ),
  ),
),

解决方法

可以通过调整SliverAppBar的布局参数消除空白,推荐以下几种方案:

  • 移除FlexibleSpaceBar,直接设置title
    FlexibleSpaceBar自带默认布局间距,若不需要展开动画,直接把标题移到SliverAppBar根参数中:

    SliverAppBar(
      shape: const ContinuousRectangleBorder(
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(20),
              bottomRight: Radius.circular(20))),
      pinned: pinned,
      snap: snap,
      floating: floating,
      expandedHeight: 40.0,
      backgroundColor: Color(0xFF2d3b96),
      title: const Text(
        "Presensi Pegawai",
        maxLines: 1,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          color: Colors.white,
          fontSize: 22.0,
          fontFamily: "IBM-Plex-Sans",
        ),
      ),
    ),
    
  • 显式设置toolbarHeight与expandedHeight匹配
    expandedHeight设置为40时,默认工具栏高度不匹配会导致留白,添加toolbarHeight参数对齐高度:

    SliverAppBar(
      shape: const ContinuousRectangleBorder(
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(20),
              bottomRight: Radius.circular(20))),
      pinned: pinned,
      snap: snap,
      floating: floating,
      expandedHeight: 40.0,
      toolbarHeight: 40.0,
      backgroundColor: Color(0xFF2d3b96),
      flexibleSpace: const FlexibleSpaceBar(
        title: Text(
          "Presensi Pegawai",
          maxLines: 1,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.white,
            fontSize: 22.0,
            fontFamily: "IBM-Plex-Sans",
          ),
        ),
      ),
    ),
    
  • 调整FlexibleSpaceBar的内边距
    若必须保留FlexibleSpaceBar,通过titlePadding调整内边距消除空白:

    flexibleSpace: const FlexibleSpaceBar(
      centerTitle: true,
      titlePadding: EdgeInsets.only(bottom: 8.0),
      title: Text(
        "Presensi Pegawai",
        maxLines: 1,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          color: Colors.white,
          fontSize: 22.0,
          fontFamily: "IBM-Plex-Sans",
        ),
      ),
    ),
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 05:45:33