flutter 为什么SliverFillRemaining膨胀太多?

pkwftd7m  于 2023-04-22  发布在  Flutter
关注(0)|答案(3)|浏览(160)

我试图使一个SliverAppBar的React,其高度,显示不同的东西,如果它的扩大或不。
我基于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 **
        ],
      ),
b1payxdu

b1payxdu1#

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

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

83qze16e2#

使hasScrollBody值为false,如下所示:

SliverFillRemaining(
        hasScrollBody: false
     ),
lx0bsm1f

lx0bsm1f3#

This Package做的正是你所期望的,我已经尝试了几天了,最后用这个,它的工作方式与SliverToBoxAdapter相同,但扩展足够,不会导致过度滚动

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,
        ),
      ),
      SliverFillRemainingBoxAdapter(
          child: // Your content goes here), 
    ],
  ),

相关问题