显示键盘时,所有内容都被向上推,我收到错误消息

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

我有以下代码:



    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,
            )
          ],
        ));
      }
    }

看起来像这样:

但是当我单击 TextField 并且键盘打开时,我得到了这个:

我没想到我的所有布局都会向上移动整个键盘高度,我希望它只向上移动到足以让聚焦的 TextField 可见(而不是给出错误)。我该如何解决这个问题,为什么会这样?

谢谢
索伦

这是 Flutter 向我们展示在使用小键盘时用户的视线将隐藏多少像素内容的方式。尝试将 resizeToAvoidBottomPadding 设置为 false ... 来自文档:

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

 Scaffold(
    resizeToAvoidBottomPadding: false,

这将避免调整大小,因此您至少可以避免显示开发警告。但请记住用户不会看到某些内容,这也是开发者警告。

更新于 17/10/2019

resizeToAvoidBottomPadding 现已弃用。

使用 resizeToAvoidBottomInset 指定主体是否应在键盘出现时调整大小。

Scaffold(
    resizeToAvoidBottomInset: false,

这个问题有两个个解决方案。

  1. resizeToAvoidBottomPadding: false 添加到您的脚手架

    Scaffold(
     resizeToAvoidBottomPadding: false,
    body: ...)
    
  2. 将您的 Scaffold body 放入可滚动视图(如 SingleChildScrollView 或 ListView)

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

你可以找到相似的问题和答案

有关此错误的完整描述和两种可能的解决方案,请参见 https://medium.com/zipper-studios/the-keyboard-causes-the-bottom-overflowed-error-5da150a1c660 上的 Medium 文章。最后一个很适合我。

如果您只是使用 SingleChildScrollView 内容会失去垂直对齐。您还可以将 SingleChildScrollView 包装在中心小部件中。

child: Center( 
    child: SingleChildScrollView(
        child: Column(
            children: <Widget>...