Flutter - SingleChildScrollView placed at the top of the screen when keyboard pops up

2,443

I fixed it. When implementing my navigation, I unintentionally placed my Scaffold inside the body of another Scaffold, so my structure was like this:

Scaffold(
      appBar: AppBar(
        title: Text('My App'),
      ),
      body: ScaffoldShownInMyQuestion(),
    )

Returning just the SingleChildScrollView without the second Scaffold wrapping around it and using that as body fixed my problem.

Share:
2,443
Thomas
Author by

Thomas

Business Information Technology Student

Updated on December 07, 2022

Comments

  • Thomas
    Thomas over 1 year

    I want to make a simple screen that shows multiple TextFields in a Column, some content above and a button below. On my first attempt, without the SingleChildScrollView, I got an "renderflex overflowed" error which could be solved by placing the TextField within the SingeChildScrollView. Now I have the problem that the SingleChildScrollView is placed way too high on my screen when I select any TextField and the keyboard pops up. I want it to stay basically just where it is since there is enough (yellow) room for the keyboard below the SingleChildScrollView. This Gif shows how my app behaves.

    This Gif shows the behavior that I would expect. I tried applying code from the FlatApp-Flutter example to my app but was unable to find the part that actually fixes my issue. Does anyone know how I can control how much the keyboard pushes the SingleChildScrollView up or how I can fix the issue? Thanks a lot.

    Scaffold(
        backgroundColor: Colors.yellow,
        body: new SingleChildScrollView(
            controller: _scrollController,
            child: new Container(
              margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
              color: Colors.orange,
              child: new Column(children: <Widget>[
                new Container(
                  margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 20.0),
                  height: 200.0,
                  width: 200.0,
                  color: Colors.grey,
                  child: new Center(
                    child: Text("Show some stuff"),
                  ),
                ),
                new Container(
                  color: Colors.blue,
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      new TextField(
                        decoration: InputDecoration(labelText: "Label 1"),
                      ),
                      new TextField(
                        decoration: InputDecoration(labelText: "Label 2"),
                      ),
                    ],
                  ),
                ),
                new RaisedButton(
                  child: const Text('Start'),
                  color: Theme.of(context).accentColor,
                  textColor: Colors.white,
                  elevation: 4.0,
                  splashColor: Colors.blueGrey,
                  onPressed: () {
                    // Perform some action
                    //button1(context);
                  },
                ),
              ]),
            )));
    

    }