【问题标题】:Why is SliverFillRemaining expanding too much?为什么 SliverFillRemaining 膨胀太多?
【发布时间】:2021-07-25 19:08:26
【问题描述】:

我正在尝试制作一个 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 **
        ],
      ),

【问题讨论】:

    标签: flutter flutter-sliver flutter-sliverappbar


    【解决方案1】:

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

    SliverToBoxAdapter(
      child: SizedBox(
        height: 50,
      ),
    ),
    

    【讨论】:

    • 谢谢,我最终使用了这个解决方案,因为我无法让它与 SliverFillRemaining 一起使用
    【解决方案2】:

    使 hasScrollBody 值为 false 像这样:

     SliverFillRemaining(
                  hasScrollBody: false
    

    ),

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2019-01-06
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多