【问题标题】:When keyboard is shown, everything is pushed up and I get a error显示键盘时,所有内容都被向上推,出现错误
【发布时间】:2019-01-29 03:38:23
【问题描述】:

我有以下代码:



    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 flutter-layout


    【解决方案1】:

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

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

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

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

    您可以找到类似问题并回答here

    【讨论】:

      【解决方案2】:

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

      主体(和其他浮动小部件)是否应自行调整大小 以避免窗口的底部填充。

       Scaffold(
          resizeToAvoidBottomPadding: false,
      

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

      2019 年 10 月 17 日更新

      resizeToAvoidBottomPadding 现已弃用。

      使用 resizeToAvoidBottomInset 指定在键盘出现时是否应调整正文的大小。

      Scaffold(
          resizeToAvoidBottomInset: false,
      

      【讨论】:

        【解决方案3】:

        如果您只使用 SingleChildScrollView 内容会失去垂直对齐。您还可以将 SingleChildScrollView 包装在 Center Widget 中。

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

        【讨论】:

          【解决方案4】:

          可以在 https://medium.com/zipper-studios/the-keyboard-causes-the-bottom-overflowed-error-5da150a1c660 上的 Medium 文章中找到有关此错误的完整描述和两种可能的解决方案。最后一个对我很有用。

          【讨论】:

          • 请不要只发布链接作为答案。一旦链接可能被破坏,寻找相同答案的人将得不到任何帮助。请更新您的答案以涵盖解决方案的主要功能,并提供链接仅供参考。
          • 如果这里至少添加了一点代码 sn-p,我会赞成这个答案!
          猜你喜欢
          • 2020-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-07
          • 2019-08-14
          • 1970-01-01
          相关资源
          最近更新 更多