I’m developing a Flutter application with a custom scaffold that may contains a list of items in the body or any other widget. I want to add a footer at the bottom that is initially off-screen, requiring the user to scroll down to see it, regardless of the amount of content above it.
The footer should not be visible when the user first navigates to the screen, even if the widgets do not fill the entire height of the screen. The footer should only come into view after the user scrolls down.
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewportConstraints.maxHeight,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
body,// Content provided from the app
footer,
],
),
),
);
Above is what I have tried so far. How can I modify my code so that the footer is always off-screen initially, and the user must scroll to see it, even when the body content is not tall enough to fill the screen?
2
Answers
You should wrap your child widget with a ConstrainedBox which takes as minHeight the one provided by the LayoutBuilder or your MediaQuery. Beware, that if that child widget is also scrollable you should provide the correct scroll controller:
You can use ListView and give the space like the answers before with LayoutBuilder, to create spacing just using SizedBox, and put you footer below the SizedBox.
Simple code to demonstrate:
This is the result:
Hopefully it can solve your problem, Thanks 😉