1

我正在尝试制作一个对其高度做出反应的 SliverAppBar,根据它是否扩展显示不同的内容。

我基于https://medium.com/flutter-community/flutter-sliverappbar-snap-those-headers-544e097248c0,但我遇到了一个问题:

列表末尾的空白区域太长了。

如果项目数量很多,则不需要删除 SliverFillRemaining,但是将其更改numberOfItems 为例如 3 个项目(而不是 30 个),则需要 SliverFillRemaining。

查看此 dartpad 的代码:https ://dartpad.dev/2c434ddf2d4d1e87bd4b421f0a673c2d

      CustomScrollView(
        physics: AlwaysScrollableScrollPhysics(),
        controller: _controller,
        slivers: [
          SliverAppBar(
            pinned: true,
            backgroundColor: Colors.grey[100],
            stretch: true,
            automaticallyImplyLeading: false,
            flexibleSpace: Header(
              maxHeight: maxHeight,
              minHeight: minHeight,
            ),
            collapsedHeight: minimizedHeight,
            expandedHeight: maxHeight - MediaQuery.of(context).padding.top,
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return _buildCard(index);
              },
              childCount: numberOfItems,
            ),
          ),
          SliverFillRemaining(
              hasScrollBody: true), // ** <-- this will add space, but too much **
        ],
      ),
4

2 回答 2

3

如果您只是添加一定高度的空间,我建议您使用 SliverToBoxAdapter:

SliverToBoxAdapter(
  child: SizedBox(
    height: 50,
  ),
),
于 2021-05-04T02:37:16.900 回答
1

将 hasScrollBody 值设为 false,如下所示:

 SliverFillRemaining(
              hasScrollBody: false

),

于 2021-11-17T14:16:19.067 回答