Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a SliverFillRemaining with a CustomScrollView and SliverList

Tags:

flutter

dart

I'm trying to create a scrolling list that can have a footer at the bottom of the scrolling list. If the list does not fill up all of the vertical screen space, the footer will need to shift down to the bottom of the page.

I tried implementing this with a SliverList and SliverFillRemaining in a CustomScrollView but the SliverFillRemaining shows some unexpected behavior I believe. It fills up more space than needed (see gif).

I used the following piece of code to create this list:

child: new CustomScrollView(
  slivers: <Widget>[
    new SliverList(
      delegate: new SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return new Card(
            child: new Container(
              padding: new EdgeInsets.all(20.0),
              child: new Center(child: new Text("Card $index")),
            ),
          );
        },
        childCount: 3,
      ),
    ),
    new SliverPadding(
      padding: new EdgeInsets.all(5.0),
    ),
    new SliverFillRemaining(
      child: new Container(
        color: Colors.red,
      ),
    )
  ],
)

Image showing what is wrong with the listview

like image 507
Bram Vanbilsen Avatar asked Apr 01 '18 21:04

Bram Vanbilsen


2 Answers

For anyone that is looking for an answer to this, I have a solution that has been working well whenever I needed something similar.

This is how I've managed it:

class ScrollOrFitBottom extends StatelessWidget {
  final Widget scrollableContent;
  final Widget bottomContent;

  ScrollOrFitBottom({this.scrollableContent, this.bottomContent});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: <Widget>[
        SliverFillRemaining(
          hasScrollBody: false,
          child: Column(
            children: <Widget>[
              Expanded(child: scrollableContent),
              bottomContent
            ],
          ),
        ),
      ],
    );
  }
}

As the name says, it will scroll if there is too much content, or have something pushed to the bottom otherwise.

I think it is simple and self-explanatory, but let me know if you have any questions

Example: https://codepen.io/lazarohcm/pen/xxdWJxb

like image 79
Lazaro Henrique Avatar answered Sep 17 '22 19:09

Lazaro Henrique


SliverFillRemaining will automatically size itself to fill the space between the bottom of the last list item and the bottom of the viewport. See the performLayout method of SliverFillRemaining for the code:

https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/rendering/sliver_fill.dart#L118

I don't think you can use it to achieve the effect you're going for, though you might be able to create a subclass that would work.

like image 25
RedBrogdon Avatar answered Sep 19 '22 19:09

RedBrogdon