颤振键盘完成按钮导致文本字段内容消失 [英] Flutter keyboard done button causes textfield content to vanish

查看:90
本文介绍了颤振键盘完成按钮导致文本字段内容消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格中有2个文本字段.当我在第二个文本字段中单击键盘上的完成按钮时,键盘将隐藏并且两个文本字段都为空.当我手动关闭键盘时,同样的情况也会丢失,文本字段的内容也会丢失.似乎每次发生时屏幕都会刷新.为什么会这样?

I have 2 textfields in a form. When i click on done button on keyboard in the second text field , keyboard hides and both the text fields get empty. The same happes when i closes the keyboard manually, then also the content of the textfields get lost. It looks like the screen gets refresh evertime this happens. why is it so?

@override
  Widget build(BuildContext context) {
    TextEditingController nameTextFieldController = TextEditingController();
    TextEditingController emailTextFieldController = TextEditingController();

    FocusNode emailFocusNode = new FocusNode();

    // TODO: implement build
    return WillPopScope(
        onWillPop: () {
          moveToLastScreen();
        },
        child: Scaffold(
          appBar: AppBar(
            title: Text("Signup"),
            leading: IconButton(
                icon: Icon(Icons.arrow_back),
                onPressed: () {
                  moveToLastScreen();
                }),
          ),
          body: Form(
              key: _formKey,
              child: Container(
                child: ListView(
                  padding: EdgeInsets.only(top: 20, left: 35, right: 35),
                  children: <Widget>[
                    Center(
                        child: GestureDetector(
                            onTap: () {
                              imageSelectorGalary();
                            },
                            child: new Container(
                                width: 100.0,
                                height: 100.0,
                                child: _image == null
                                    ? new Image.asset(
                                        'images/profilepic.png',
                                        fit: BoxFit.fitWidth,
                                      )
                                    : new CircleAvatar(
                                        backgroundImage: new FileImage(_image),
                                        radius: 200.0,
                                      )))),
                    Text(
                      "NAME",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 5, bottom: 10),
                      child: TextFormField(
                          inputFormatters:[
                            LengthLimitingTextInputFormatter(10),
                          ],
                          onFieldSubmitted: (String  value){
                            FocusScope.of(context).requestFocus(emailFocusNode);
                          },
                          keyboardType: TextInputType.text,
                          controller: nameTextFieldController,
                          validator: (String value) {
                            if (value.isEmpty) {
                              return 'Please enter the name';
                            }
                          },
                          decoration: InputDecoration(
                              border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(5.0)),
                              contentPadding: EdgeInsets.only(
                                  top: 15, bottom: 15, left: 20, right: 20))),
                    ),
                    Text(
                      "EMAIL ID",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 5, bottom: 10),
                      child: TextFormField(
                        controller: emailTextFieldController,
                        focusNode: emailFocusNode ,
                        onFieldSubmitted: (String  value){
                          FocusScope.of(context).requestFocus(new FocusNode());
                        },
                        validator: (String value){
                          if(value.isEmpty){
                            return "Please enter your email-id";
                          }
                        },
                        decoration: InputDecoration(
                            contentPadding: EdgeInsets.only(
                                top: 15, bottom: 15, left: 20, right: 20),
                            border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(5.0))),
                      ),
                    ),
                    Text(
                      "CITY AND AREA",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    GestureDetector(
                        onTap: () {
                          CityAreasDialog cityAreasDialog =
                              new CityAreasDialog();
                          cityAreasDialog.information(context);
                        },
                        child: Container(
                          margin: EdgeInsets.only(top: 5, bottom: 10),
                          padding: EdgeInsets.only(left: 20, right: 20),
                          decoration: new BoxDecoration(
                              border: new Border.all(color: CustomColors.grey),
                              borderRadius: BorderRadius.all(Radius.circular(
                                      5.0) //                 <--- border radius here
                                  )),
                          child: Container(
                              padding: EdgeInsets.only(top: 10, bottom: 10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Text("Select Your City"),
                                      Text(
                                        "and area",
                                        style: TextStyle(
                                            color: Colors.grey, fontSize: 10),
                                      )
                                    ],
                                  ),
                                  Icon(
                                    Icons.arrow_drop_down,
                                    color: Colors.black,
                                  )
                                ],
                              )),
                        )),
                    Container(
                        margin: EdgeInsets.only(top: 20, left: 10, right: 10),
                        child: RaisedButton(
                          onPressed: () {
                            debugPrint("Next Click");
                            onSignup();
                          },
                          child: new Text(
                            "NEXT",
                          ),
                        ))
                  ],
                ),
              )),
        ));
  }

推荐答案

此问题是因为每次重新构建窗口小部件时,您都在创建新的 TextEditingController .因此,要解决此问题,请将这些变量 nameTextFieldController emailTextFieldController 移至构建方法之外.

This issue is because you are creating new TextEditingController every time your widget is rebuilt. So to fix this issue, move these variables nameTextFieldController ,emailTextFieldController outside your build method.

赞:

  TextEditingController nameTextFieldController = TextEditingController();
   TextEditingController emailTextFieldController = TextEditingController();

@override
  Widget build(BuildContext context) {
    ...

这篇关于颤振键盘完成按钮导致文本字段内容消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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