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

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

问题描述

我正在设计一个登录页面,当我点击任何文本表单字段时,它会溢出,它会打开 keyboard 并通过像这样的溢出警告,请参见附图.我还希望按钮的右侧应该有一个凸起的按钮图标.

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: SingleChildScrollView(
          child: 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天全站免登陆