代码之家  ›  专栏  ›  技术社区  ›  Bram Vanbilsen

底部有滚动页脚的Listview

  •  16
  • Bram Vanbilsen  · 技术社区  · 8 年前

    我正在尝试创建一个滚动列表小部件,它可以显示一些项目,并能够在底部添加一个页脚,以便滚动。

    如果滚动列表没有占据整个高度,例如只有2个项目,则页脚仍应显示在屏幕底部。下面是我试图实现的一些草图

    non page filling listview page filling listview

    我试图计算listview需要的垂直大小,但这意味着我需要知道构建时孩子们的身高。有更好的方法吗?

    编辑
    我正在努力实现与 here 当然是用颤栗。

    5 回复  |  直到 8 年前
        1
  •  23
  •   Rémi Rousselet    8 年前

    您需要为此创建一个自定义RenderBox。因为没有支持这种开箱即用的小部件。

    SliverFillRemaining 非常接近。但它的大小/滚动行为与您预期的不同。因为,如果存在,几乎总是 Scrollable ... 可滚动。

    相反,我们可以复制粘贴源 剩余SliverFill 。并进行一些编辑

    class SliverFooter extends SingleChildRenderObjectWidget {
      /// Creates a sliver that fills the remaining space in the viewport.
      const SliverFooter({
        Key key,
        Widget child,
      }) : super(key: key, child: child);
    
      @override
      RenderSliverFooter createRenderObject(BuildContext context) => new RenderSliverFooter();
    }
    
    class RenderSliverFooter extends RenderSliverSingleBoxAdapter {
      /// Creates a [RenderSliver] that wraps a [RenderBox] which is sized to fit
      /// the remaining space in the viewport.
      RenderSliverFooter({
        RenderBox child,
      }) : super(child: child);
    
      @override
      void performLayout() {
        final extent = constraints.remainingPaintExtent - math.min(constraints.overlap, 0.0);
        var childGrowthSize = .0; // added
        if (child != null) {
           // changed maxExtent from 'extent' to double.infinity
          child.layout(constraints.asBoxConstraints(minExtent: extent, maxExtent: double.infinity), parentUsesSize: true);
          childGrowthSize = constraints.axis == Axis.vertical ? child.size.height : child.size.width; // added
        }
        final paintedChildSize = calculatePaintOffset(constraints, from: 0.0, to: extent);
        assert(paintedChildSize.isFinite);
        assert(paintedChildSize >= 0.0);
        geometry = new SliverGeometry(
          // used to be this : scrollExtent: constraints.viewportMainAxisExtent,
          scrollExtent: math.max(extent, childGrowthSize),
          paintExtent: paintedChildSize,
          maxPaintExtent: paintedChildSize,
          hasVisualOverflow: extent > constraints.remainingPaintExtent || constraints.scrollOffset > 0.0,
        );
        if (child != null) {
          setChildParentData(child, constraints, geometry);
        }
      }
    }
    

    这里我改变了三件事

    • 不受约束的孩子 maxExtent 。因为如果没有更多可用的屏幕空间,那么页脚的高度将强制为0。
    • 已更改 SliverGeometry scrollExtent 从“全屏高度”到“实际可用大小”。所以实际上 填满 剩余的可见空间。不填充屏幕。
    • 将最小值添加到该值 滚动范围 ,等于实际页脚高度。因此,如果视口中没有剩余空间,只需添加子对象,而周围没有任何间距。

    我们现在可以在 CustomScrollView 像往常一样。

    最终结果:

    new CustomScrollView(
      slivers: <Widget>[
        new SliverFixedExtentList(
          itemExtent: 42.0,
          delegate: new SliverChildBuilderDelegate((context, index) {
            return new SizedBox.expand(
              child: new Card(),
            );
          }, childCount: 42),
        ),
        new SliverFooter(
          child: new Align(
            alignment: Alignment.bottomCenter,
            child: new Container(
              height: 42.0,
              color: Colors.red,
            ),
          ),
        ),
      ],
    ),
    
        2
  •  9
  •   evelyne24    7 年前

    这可以通过使用 SingleChildScrollView 如前所述,有特殊限制 here 。

    请看下面的示例:

    @override
    Widget build(BuildContext context) {
      return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return SingleChildScrollView(
            child: ConstrainedBox(
              constraints: constraints.copyWith(
                minHeight: constraints.maxHeight,
                maxHeight: double.infinity,
              ),
              child: IntrinsicHeight(
                child: Column(
                  children: <Widget>[
                    Container(height: 200, color: Colors.blue),
                    Container(height: 200, color: Colors.orange),
                    Container(height: 200, color: Colors.green),
                    Container(height: 50, color: Colors.pink),
                    Expanded(
                      child: Align(
                        alignment: Alignment.bottomCenter,
                        child: Container(
                          width: double.infinity,
                          color: Colors.red,
                          padding: EdgeInsets.all(12.0),
                          child: Text('FOOTER', textAlign: TextAlign.center,),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        }
      );
    }
    
    

    这将生成以下布局: enter image description here

    如果您将粉红色容器的高度更改为更大的值,例如500,您将看到页脚也会与整个列表一起滚动。

    感谢Simon Lightfoot为我指明了正确的方向!

        3
  •  1
  •   m4n0    5 年前

    使用生成器在ListView中添加页脚

    1. 只需在“itemCount”中添加+1,例如:“itemCount:list.length+1”
    2. 在返回customWidget时,检查条件“list.length==index”,只需返回一个SizedBox(高度:“required 页脚高度”)。

    代码:

    ListView.builder(
    itemCount: list.length+1,
    physics: BouncingScrollPhysics(),
    itemBuilder: (context, index) {
      return GestureDetector(
          onTap: () {
            if (list.length == index) {
              return;
            }
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => PlotDetail(list[index])));
          },
          child: list.length != index?PlotListItemWidget(list[index]):SizedBox(height: 100,));
       },
    );
    

    谢谢你!

        4
  •  0
  •   Randal Schwartz    8 年前

    类似于 https://flutter.io/web-analogs/#setting-absolute-position ?这似乎会导致小部件停留在屏幕上的固定位置。

        5
  •  0
  •   Happy Mishra    5 年前

    只有当列表最后一项顶部出现浮动操作按钮之类的问题时,前面的答案才有效。 但若我们想在屏幕底部显示页脚,我们可以简单地使用堆栈。