打开键盘时Flutter中出现溢出错误 [英] Overflow Error in Flutter when keyboard open

查看:219
本文介绍了打开键盘时Flutter中出现溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个登录页面,它溢出,当我单击任何文本表单字段时,它会打开键盘,并通过类似这样的溢出警告来查看附件图像。
我也希望在按钮的右侧应该有一个凸起按钮图标。

I am designing a login page it overflowed when I click on any text form field it open keyboard and through overflow warning like this see attached image. I also want a Raised button icon should be on the right side of the button.

Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        body: Stack(
          fit: StackFit.expand,
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                  image: new DecorationImage(
                      image: new AssetImage('assets/login_page_bg_1.jpg'),
                      fit: BoxFit.cover,
                      colorFilter: new ColorFilter.mode(
                          Colors.black.withOpacity(0.55), BlendMode.dstATop))),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Container(
                    margin: new EdgeInsets.only(top: 42.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Image.asset('assets/logo.png',
                            width: 250.0, height: 200.21),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Container(
                    margin: new EdgeInsets.only(bottom: 40.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        //form filed goes here
                        Text('Login As User',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 35.0)),
                        TextFormField(
                            keyboardType: TextInputType.emailAddress,
                            decoration: InputDecoration(
                              hintText: 'you@example.com',
                              labelText: 'Email Address',
                            ),
                        new Container(
                          // width: MediaQuery.of(context).size.width,
                          child: RaisedButton.icon(
                            color: Color.fromARGB(251, 188, 74, 1),
                            label: Text('LOGIN'),
                            icon: Icon(Icons.send,
                                size: 10.0, color: Colors.black),
                            onPressed: () {
                              this.submit();
                            }, ),)],),),)],)],),),);

初始状态

错误/状态溢出

推荐答案

之所以发生这种情况,是因为当键盘出现在屏幕上时,绘制的画布的高度减小。一种解决方案是像这样将根容器包装在SingleChildScrollView中:

This is happening because when the keyboard comes on screen, the height of the canvas to draw decreases. One solution is to wrap your root container inside SingleChildScrollView like this :

Widget build(BuildContext context) {
return Scaffold(
      body: Stack(
        fit: StackFit.loose,
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                image: new DecorationImage(
                    image: new AssetImage('assets/login_page_bg_1.jpg'),
                    fit: BoxFit.cover,
                    colorFilter: new ColorFilter.mode(
                        Colors.black.withOpacity(0.55), BlendMode.dstATop)
                        )
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: 42,),
              Expanded(
                flex: 1,
                child: Center(
                  child:
                    Image.asset('assets/logo.png',
                        width: 250.0, height: 200.21),
                ),
              ),
              Expanded(
                flex: 2,
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                    //form filed goes here
                    Text('Login As User',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontWeight: FontWeight.bold, fontSize: 35.0)),
                TextFormField(
                  keyboardType: TextInputType.emailAddress,
                  decoration: InputDecoration(
                    hintText: 'you@example.com',
                    labelText: 'Email Address',
                  )
                ),
                  new Container(
                    // width: MediaQuery.of(context).size.width,
                    child: RaisedButton.icon(
                      color: Color.fromARGB(251, 188, 74, 1),
                      label: Text('LOGIN'),
                      icon: Icon(Icons.send,
                          size: 10.0, color: Colors.black),
                      onPressed: () {
                        //this.submit();
                      }, ),)],)),
              SizedBox(height: 40,)

            ],)],),);

当内容的高度超过视口的可用高度时,它将使屏幕可滚动

It will make your screen scrollable when the height of the content gets more than the available height of the viewport.

这篇关于打开键盘时Flutter中出现溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆