【问题标题】:How to change the set value of a text field如何更改文本字段的设置值
【发布时间】:2021-07-23 07:13:12
【问题描述】:

所以我有 TextFormField,它有一个设定值。但是,当我尝试通过输入 Feild 来更改它的值时,它只是恢复为指定的值。这是我的代码:

TextFormField(
                onEditingComplete: () {

                  FocusScope.of(context).unfocus();
                  setState(() {
                    
                    // THIS IS WHERE I NEED TO CHANGE ITS VALUE

                  });

                },
                
                controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
                cursorHeight: 23.0,
                decoration: InputDecoration(
                  disabledBorder: UnderlineInputBorder(),
                  border: OutlineInputBorder(),
                  hintText: "Enter Text",
                  labelText: "Title",
                  labelStyle: TextStyle(
                    fontSize: 20.0
                  ),
                ),
              ),

我需要以某种方式将_titleController.text 设置为enter 或单击勾选按钮时字段中的值 - 但由于显而易见的原因,我不能只做_titleController.text = _titleController.text;。请帮忙。如果您需要更多详细信息,请发表评论。

【问题讨论】:

    标签: flutter dart flutter-textinputfield


    【解决方案1】:

    改成这样:

    @override
      void initState() {
        super.initState();
       _titleController.text = newTask.title;
      }
    .
    .
    .
    .
    .
    //And in your TextFormField
    controller: _titleController,
    

    【讨论】:

      【解决方案2】:

      您将 newTask.title 的值分配给 _titleController.text

      controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
      

      制作:

      controller: _titleController
      

      【讨论】:

        猜你喜欢
        • 2013-01-31
        • 2021-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-05-24
        • 2018-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多