Home > OS >  How to make SingleChildScrollView fill full screen height with blank space where required
How to make SingleChildScrollView fill full screen height with blank space where required

Time:07-23

In my flutter app I need a layout where I'm able to scroll if all the widgets are too long for the screen,so I added a SingleChildScrollView. But, if the widgets are smaller and leave a lot of space, I want the last row to be pinned to the bottom of the screen with blank space between the last two items. So I added a Spacer to help with that.

However that causes an error, because the SingleChildScrollView doesn't like the Spacer. I've tried everything I know, but I can't find a layout that satisfies both conditions without an error. Can someone suggest a solution please?

Code below - you may have to alter the size (or number) of the containers to demonstrate the issue on your device.

class _TestMain extends State<TestMain> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  color: Colors.blue,
                  height: 100,
                ),
                Container(
                  color: Colors.yellow,
                  height: 100,
                ),
                Container(
                  color: Colors.green,
                  height: 100,
                ),
                Container(
                  color: Colors.pink,
                  height: 100,
                ),
                // comment out these four containers to demonstrate issue
                Container(
                  color: Colors.blue,
                  height: 100,
                ),
                Container(
                  color: Colors.yellow,
                  height: 100,
                ),
                Container(
                  color: Colors.green,
                  height: 100,
                ),
                Container(
                  color: Colors.pink,
                  height: 100,
                ),
                // SingleChildScrollView won't allow the Spacer
                //const Spacer(),
                Container(
                  color: Colors.blue,
                  height: 100,
                  child: Text("I'm always fixed to the bottom of the screen!"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

The error if I add the Spacer is:

======== Exception caught by rendering library =====================================================
The following assertion was thrown during performLayout():
RenderFlex children have non-zero flex but incoming height constraints are unbounded.

When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction.
These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.

Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.

CodePudding user response:

You can make use of CustomScrollView and SliverFillRemaining slightly differently to avoid letting the screen scroll when it's partially filled. This way we can make use of Spacer and Expanded widgets too!

Scaffold(
      body: SafeArea(
        child: CustomScrollView(
          slivers: [
            SliverFillRemaining(
              hasScrollBody: false,
              child: Column(
                children: [

                  Text("I'm at top part"),
                  Text("I'm at top part"),
                  Text("I'm at top part"),
                  const Spacer(),
                  Text("I'm at bottom"),
                ],
              ),
            )
          ],
        ),
      ),
    )

CodePudding user response:

You can use bottomNavigationBar from Scaffold.

return Scaffold(
  bottomNavigationBar: Container(
    color: Colors.blue,
    height: 100,
    child: Text("I'm always fixed to the bottom of the screen!"),
  ),
  body: SafeArea(...

And You can use CustomScrollView which is better than SingleChildScrollView; and use SliverFillRemaining will do the trick with Align widget.

class TestMain extends StatefulWidget {
  TestMain({Key? key}) : super(key: key);

  @override
  State<TestMain> createState() => _TestMain();
}

class _TestMain extends State<TestMain> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Container(
        color: Colors.blue,
        height: 100,
        child: Text("I'm always fixed to the bottom of the screen!"),
      ),
      body: SafeArea(
        child: CustomScrollView(
          slivers: [
            /// your top level items
            // SliverList(delegate: SliverChildBuilderDelegate((context, index) => ,)),

            SliverList(
                // better use `SliverChildBuilderDelegate`
                delegate: SliverChildListDelegate([
              // you can put regular container here
              Container(
                color: Colors.amber,
                height: 100,
              ),
            ])),
            SliverFillRemaining(
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  color: Colors.blue,
                  height: 100,
                  child: Text("I'm always fixed to the bottom of the screen!"),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
  • Related