Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When keyboard is shown, everything is pushed up and I get a error

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

like image 879
Neigaard Avatar asked Aug 22 '18 19:08

Neigaard


People also ask

How do you prevent the keyboard pushes a widget up on Flutter?

You can solve this problem by setting resizeToAvoidBottomInset: false in Scaffold() .

How do you fix a keyboard overflow Flutter?

The solution to resolve this overflow error is to make your entire widget or in our case the Column scrollable. We can do that by wrapping our Column inside a SingleChildScrollView. Also, wrap the SingleChildScrollView with Center so that the entire UI is centered.


2 Answers

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

like image 82
Dinesh Balasubramanian Avatar answered Oct 27 '22 00:10

Dinesh Balasubramanian


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,
like image 44
Lucas Avatar answered Oct 27 '22 00:10

Lucas