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,
),
)
],
)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With