草庐IT

Flutter 键盘完成按钮导致文本字段内容消失

coder 2023-07-21 原文

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

@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。 因此,要解决此问题,请将这些变量 nameTextFieldControlleremailTextFieldController 移到构建方法之外。

像这样:

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

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

关于Flutter 键盘完成按钮导致文本字段内容消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946081/

有关Flutter 键盘完成按钮导致文本字段内容消失的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  3. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  6. ruby - 在 Ruby 中用键盘诅咒数组浏览 - 2

    我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作

  7. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  8. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  9. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  10. ruby-on-rails - 在 Ruby on Rails 中发送响应之前如何等待多个异步操作完成? - 2

    在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.

随机推荐