【问题标题】:Flutter :- Keyboard causes layout to overflow on the bottom when on focusFlutter :- 键盘在焦点上导致布局在底部溢出
【发布时间】:2020-05-13 15:32:55
【问题描述】:

我有以下布局代码。

@override
Widget build(BuildContext context) {
return Material(
  child: Scaffold(

//        resizeToAvoidBottomPadding: false,
//        resizeToAvoidBottomInset: false,
    appBar: PreferredSize(
      preferredSize: Size.fromHeight(70),
      child: AppBar(
        centerTitle: true,
        title: AutoSizeText(
          meal['name'],
          minFontSize: 30,
          maxFontSize: 50,
        ),
        backgroundColor: Colors.black,
        elevation: 1,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () => null,
          ),
        ],
      ),
    ),
    body: Builder(
      builder: (context) {
        return Container(
          color: Colors.black,
          alignment: Alignment.center,
          child: FractionallySizedBox(
            widthFactor: 0.85,
            child: Container(
              child: Column(
                children: <Widget>[
                  Spacer(flex: 1,),
                  Container(
                    margin: EdgeInsets.only(bottom: 50),
                    child: Column(
                      children: <Widget>[
                        Container(
                          decoration: BottomWhiteDecoration(6.0),
                          padding: EdgeInsets.only(bottom: 8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                'Servings: 1',
                                style: TextStyle(color: Colors.white),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          child: Column(
                            children: <Widget>[
                              RowNutrimentalInfo('Calories', meal['calories'], showGram: false),
                              RowNutrimentalInfo('Protein', meal['macros']['proteins']),
                              RowNutrimentalInfo('Carbs', meal['macros']['carbs']),
                              RowNutrimentalInfo('Fat', meal['macros']['fats']),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                Spacer(flex: 2,),
                  Container(
                    child: Column(
                      children: <Widget>[
                        Form(
                          key: this._mealFormKey,
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                flex: 10,
                                child: TextFormField(...),
                              ),
                              Spacer(flex: 1,),
                              Expanded(
                                flex: 10,
                                child: TextFormField(...),
                              ),
                            ],
                          ),
                        ),
                        FractionallySizedBox(
                          widthFactor: .50,
                          child: OutlineButton(
                            borderSide: BorderSide(color: Colors.white),
                            color: Colors.black,
                            onPressed: _eatMeal,
                            child: Padding(
                              padding: EdgeInsets.all(20),
                              child: Text('Ok',
                                  style: TextStyle(color: Colors.white)),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Spacer(flex: 2,),
                ],
              ),
            ),
          ),
        );
      },
    ),
  ),
);
}

但是,当我开始在其中一个文本字段中输入时,键盘会导致布局在底部溢出,如下图所示。

以下是我遇到的错误以及它在手机上的显示方式。有什么想法可以解决这个问题吗?我见过类似的问题,设置 resizeToAvoidBottomPadding: false 等解决方案,对我不起作用,因为键盘仍然覆盖文本输入。

The following assertion was thrown during layout:
A RenderFlex overflowed by 45 pixels on the bottom.

The relevant error-causing widget was: 
Column file:///C:/Users/bolon/StudioProjects/macro_counter/lib/Meal.dart:92:26
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and 
black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the 
RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. 
If the content is legitimately bigger than the available space, consider clipping it with a ClipRect 
widget before putting it in the flex, or using a scrollable container rather than a Flex, like a 
ListView.

【问题讨论】:

  • 您是否尝试取消注释行 resizeToAvoidBottomPadding: false 看看会发生什么?
  • @MarcoFregoso 请检查以下解决方案,如有疑问请告诉我
  • @Alok 我之前尝试过,它仍然导致溢出。 Ravindra 的回答解决了我的问题!谢谢

标签: flutter dart flutter-layout


【解决方案1】:

问题是您使用的是扩展小部件,您看到扩展小部件本质上是灵活的,它们会根据可用空间消耗和缩小。如果你不想这样,你需要指定一个高度。

所以我使用MediaQueryfor它来根据设备分辨率设置widget,请查看下面的解决方案,我已经创建了你的代码演示。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlearningapp/colors.dart';

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomeScreen();
  }
}

class _HomeScreen extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Material(
        child: Scaffold(
      appBar: AppBar(
        title: Text("Demo Scroll"),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        color: Colors.black,
        child: SingleChildScrollView(

            child: Column(

              children: <Widget>[
                Container(
                  height: MediaQuery.of(context).size.height*0.6,
                  child: Column(
                    children: <Widget>[
                      CommonWidget("Calories", "150"),
                      CommonWidget("Protein", "9g"),
                      CommonWidget("Carbs", "15g"),
                      CommonWidget("Fat", "25g"),
                    ],
                  ),
                ),
                Container(
                  height: MediaQuery.of(context).size.height * 0.4,
                  child: Align(
                    alignment: Alignment.topCenter,
                    child: Column(
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: TextField(
                                style: new TextStyle(color: Colors.white),
                                decoration: InputDecoration(
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: BorderSide(color: Colors.white),
                                    ),
                                    focusedBorder: OutlineInputBorder(
                                      borderSide: BorderSide(color: Colors.white),
                                    ),
                                    labelText: "Serving"),
                              ),
                            ),
                            SizedBox(
                              width: 10.0,
                            ),
                            Expanded(
                              child: TextField(
                                style: new TextStyle(color: Colors.white),
                                decoration: InputDecoration(
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: BorderSide(color: Colors.white),
                                    ),
                                    focusedBorder: OutlineInputBorder(
                                      borderSide: BorderSide(color: Colors.white),
                                    ),
                                    labelText: "Germs"),
                              ),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 10.0,
                        ),
                        OutlineButton(
                          borderSide: BorderSide(color: Colors.white),
                          color: Colors.black,
                          onPressed: () {},
                          child: Padding(
                            padding: EdgeInsets.all(20),
                            child: Text('   Ok  ',
                                style: TextStyle(color: Colors.white)),
                          ),
                        )
                      ],
                    ),
                  ),
                )




              ],
            ),
          ),
        ),

    ));
  }

  Widget CommonWidget(var name, var value) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: Text(
                  name,
                  style: TextStyle(fontSize: 16.0, color: Colors.white),
                ),
              ),
              Expanded(
                child: Align(
                  alignment: Alignment.centerRight,
                  child: Text(value,
                      style: TextStyle(fontSize: 16.0, color: Colors.white)),
                ),
              )
            ],
          ),
          Divider(
            color: Colors.white,
            height: 20.0,
          )
        ],
      ),
    );
  }
}

输出将跟随示例

【讨论】:

  • 这就是解决我的问题的方法,谢谢!此外,我只为 Column 中的顶部 Container 设置了“MediaQuery.of...”,因为为底部 Container 添加 MediaQuery 会导致底部有额外的空白区域,从而使屏幕可滚动,再次感谢 :)
  • 很高兴帮助你@MarcoFregoso...保持愉快的编码..也请投票
猜你喜欢
  • 2021-04-07
  • 1970-01-01
  • 2023-03-25
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2020-03-12
  • 2019-05-27
相关资源
最近更新 更多