6

I have the following code:



    class _MyHomePageState extends State {
      @override
      Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return new Scaffold(
            body: new Column(
          children: [
            new Container(
              color: JBTheme.colorGreenBrand,
              height: 130.0,
              alignment: Alignment.bottomLeft,
              child: new Center(
                child: new Align(
                  alignment: Alignment.bottomCenter,
                  child: new Container(
                    color: JBTheme.colorOrange,
                    constraints: new BoxConstraints(maxWidth: 270.0),
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        new Column(
                          mainAxisSize: MainAxisSize.min,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            new Image.asset("flags/flag_dk.png"),
                            new Container(
                              margin: const EdgeInsets.only(top: 4.0, bottom: 4.0),
                              child: new Text("Danmark"),
                            ),
                            new Text("DKK")
                          ],
                        ),
                        new Expanded(child: new Image.asset("images/arrow.png")),
                        new Column(
                          mainAxisSize: MainAxisSize.min,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            new Image.asset("flags/flag_us.png"),
                            new Container(
                              margin: const EdgeInsets.only(top: 4.0, bottom: 4.0),
                              child: new Text("USA"),
                            ),
                            new Text("USD")
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            new Expanded(
                child: new Container(
              color: JBTheme.colorGreyMedium,
              child: new Column(
                children: [
                  new Expanded(child: new Container()),
                  new Padding(
                      padding: const EdgeInsets.only(bottom: 8.0),
                    child: new Card(
                      elevation: -8.0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(15.0),
                      ),
                      child: new Container(
                          width: 200.0,
                          height: 30.0,
                          color: JBTheme.colorWhite,
                          child: new Stack(
                            children: [
                              new TextField(
                                  textAlign: TextAlign.center
                              )
                            ],
                          )
                      ),
                    )
                  )
                ],
              ),
            )),
            new Container(
              height: 260.0,
              color: JBTheme.colorGreenMint,
            )
          ],
        ));
      }
    }

And it looks like this: no keyboard

But when I click the TextField and the keyboard opens I get this: enter image description here

I did not expect all of my layout to be moved up by the full keyboard height, I would like it to only move up enough so that the focused TextField is visible (and not to give a error). How can I fix this, and why is it happening?

Thank you
Søren

Neigaard
  • 3,726
  • 9
  • 49
  • 85

4 Answers4

22

There are two solutions to this problem.

  1. Add resizeToAvoidBottomPadding: false to your Scaffold

    Scaffold(
     resizeToAvoidBottomPadding: false,
    body: ...)
    
  2. Put your Scaffold body inside a scrollableView (like SingleChildScrollView or ListView)

    new Scaffold(
        body: SingleChildScrollView(child: //your existing body
    ...)
    

You can find similar problem and answer here

Dinesh Balasubramanian
  • 20,532
  • 7
  • 64
  • 57
11

This is Flutters way of showing us how many pixels of content will be hidden from users eyesight when using the keypad. Try setting resizeToAvoidBottomPadding to false... From docs:

Whether the body (and other floating widgets) should size themselves to avoid the window's bottom padding.

 Scaffold(
    resizeToAvoidBottomPadding: false,

This will avoid the resizing so you will at least avoid the dev warning being shown. But remember user will not see some content, and also this is a developer warning.

Update on 17/10/2019

resizeToAvoidBottomPadding is now Deprecated.

Use resizeToAvoidBottomInset to specify if the body should resize when the keyboard appears.

Scaffold(
    resizeToAvoidBottomInset: false,
MSARKrish
  • 3,355
  • 4
  • 30
  • 43
Lucas
  • 9,871
  • 5
  • 42
  • 52
1

If you just use SingleChildScrollView content looses vertical alignment. You can also wrap the SingleChildScrollView in a Center Widget.

child: Center( 
    child: SingleChildScrollView(
        child: Column(
            children: <Widget>...
ronbla
  • 11
  • 1
0

A full description of this error and two possible solutions can be found on Medium article available on https://medium.com/zipper-studios/the-keyboard-causes-the-bottom-overflowed-error-5da150a1c660. The last one works great for me.

Loredana
  • 9
  • 1
  • 5
    Please not just post a link as an answer. Once the link might be broken, someone looking for the same answer would not get any help. Please update your answer to cover the key features of the solution and provide the link only as reference. – Erbsenkoenig Aug 01 '19 at 11:58
  • I would have upvoted this answer, if at least a bit of code snippet was added here! – Randika Vishman Sep 02 '19 at 05:01