Flutter 颤振:-键盘在对焦时导致布局在底部溢出

Flutter 颤振:-键盘在对焦时导致布局在底部溢出,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我有以下布局代码 @override Widget build(BuildContext context) { return Material( child: Scaffold( // resizeToAvoidBottomPadding: false, // resizeToAvoidBottomInset: false, appBar: PreferredSize( preferredSize: Size.fromHeight(70),

我有以下布局代码

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

问题是,当您使用扩展的小部件时,您会发现扩展的小部件本质上是灵活的,它们将根据可用空间进行消耗和收缩。如果不希望,则需要指定高度

因此,我使用了
MediaQuery
来根据设备分辨率设置
小部件
,请检查下面的解决方案,我已经创建了您的代码演示

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,
          )
        ],
      ),
    );
  }
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”;
导入“package:flatterlearningapp/colors.dart”;
类主屏幕扩展StatefulWidget{
@凌驾
状态createState(){
//TODO:实现createState
返回主屏幕();
}
}
类_主屏幕扩展状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
退货(
孩子:脚手架(
appBar:appBar(
标题:文本(“演示卷轴”),
),
主体:容器(
高度:双无限,
宽度:double.infinity,
颜色:颜色,黑色,
子:SingleChildScrollView(
子:列(
儿童:[
容器(
高度:MediaQuery.of(上下文).size.height*0.6,
子:列(
儿童:[
CommonWidget(“卡路里”,“150”),
CommonWidget(“蛋白质”,“9g”),
CommonWidget(“碳水化合物”、“15g”),
CommonWidget(“Fat”、“25g”),
],
),
),
容器(
高度:MediaQuery.of(上下文).size.height*0.4,
子对象:对齐(
对齐:alignment.topCenter,
子:列(
儿童:[
划船(
儿童:[
扩大(
孩子:TextField(
样式:新文本样式(颜色:Colors.white),
装饰:输入装饰(
enabledBorder:OutlineInputBorder(
borderSide:borderSide(颜色:Colors.white),
),
聚焦顺序:大纲输入边框(
borderSide:borderSide(颜色:Colors.white),
),
labelText:“服务”),
),
),
大小盒子(
宽度:10.0,
),
扩大(
孩子:TextField(
样式:新文本样式(颜色:Colors.white),
装饰:输入装饰(
enabledBorder:OutlineInputBorder(
borderSide:borderSide(颜色:Colors.white),
),
聚焦顺序:大纲输入边框(
borderSide:borderSide(颜色:Colors.white),
),
标签文字:“细菌”),
),
),
],
),
大小盒子(
身高:10.0,
),
大纲按钮(
borderSide:borderSide(颜色:Colors.white),
颜色:颜色,黑色,
按下:(){},
孩子:填充(
填充:边缘设置。全部(20),
子项:文本('Ok',
样式:TextStyle(颜色:Colors.white)),
),
)
],
),
),
)
],
),
),
),
));
}
小部件CommonWidget(变量名称、变量值){
返回填充(
填充:所有边缘设置(15.0),
子:列(
儿童:[
划船(
儿童:[
扩大(
子:文本(
名称
样式:TextStyle(fontSize:16.0,颜色:Colors.white),
),
),
扩大(
子对象:对齐(
对齐:alignment.centerRight,
子项:文本(值,
样式:TextStyle(fontSize:16.0,颜色:Colors.white)),
),
)
],
),
分隔器(
颜色:颜色,白色,
身高:20.0,
)
],
),
);
}
}
和输出将在示例中显示


您是否尝试取消行的注释,重设为resizeToAvoidBottomPadding:false并查看发生了什么情况?@MarcoFregoso请检查以下解决方案,并在出现问题时通知我concern@Alok我以前试过,它仍然会导致溢出。拉文德拉的回答解决了我的问题!谢谢这就解决了我的问题,谢谢!此外,我只为列中的顶部容器设置了“MediaQuery.of…”,因为为底部容器添加MediaQuery会在底部产生额外的空白,使屏幕可以滚动,再次感谢:)很高兴帮助您@MarcoFregoso…保持愉快的编码…请也进行更新投票
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,
          )
        ],
      ),
    );
  }
}