【问题标题】:TextFormField enters value backwards [Flutter]TextFormField 向后输入值 [Flutter]
【发布时间】:2019-11-28 12:12:07
【问题描述】:

如下面的 GIF 所示,我使用的 TextFormField 正在向后输入值。我也将TextDirection 属性设置为ltr,但它没有改变任何东西。其他TextFormFields 似乎没有这个问题。输入的文本正在使用Navigator.pop 发送回另一个屏幕,并以相同的反向发送。

Weird TextFormField

导致问题的 textFormField 代码:

TextFormField(
// validator: (String value) {
// return value.isEmpty ? "task must have a name" : null;
// },
    textDirection: TextDirection.ltr,
    maxLength: 100,
    controller: newcontroller, // Just an ordinary TextController
    onChanged: (value) {
        setState(() {
            newcontroller.text = value;
        });
    },
    decoration: InputDecoration(
        errorText: _validate  // Just a boolean value set to false by default
                   ? 'Value Can\'t Be Empty' : null,
        labelText: "name of task"
    ),
    style: TextStyle(height: 1.2, fontSize: 20, color: Colors.black87)
)

【问题讨论】:

    标签: flutter textfield direction


    【解决方案1】:

    当调用onChanged 时,您不必在newcontroller.text 中设置文本。 在您的TextFormField 中输入的文本默认分配给newcontroller

    您收到此错误是因为对于这段代码,

    所以,请尝试删除以下代码

     setState(() {
                newcontroller.text = value;
            });
    

    【讨论】:

      【解决方案2】:

      你可以在Navigator.pop(context,whtever you want to pass)中发送你想要的任何东西

        TextFormField(
      //                        validator: (String value) {
      //                          return value.isEmpty ? "task must have a name" : null;
      //                        },
                      textAlign: TextAlign.end,
                      maxLength: 100,
                      controller: newcontroller, // Just an ordinary TextController
                      onChanged: (value) {
                        print(value);
                      },
      
                      decoration: InputDecoration(
                          errorText:
                              _validate // Just a boolean value set to false by default
                                  ? 'Value Can\'t Be Empty'
                                  : null,
                          labelText: "name of task"),
                      style:
                          TextStyle(height: 1.2, fontSize: 20, color: Colors.black87),
                    ),
                    MaterialButton(
                      onPressed: () {
                        Navigator.pop(context, newcontroller.text);
                      },
                      child: Text("GO BACK!"),
                    ), 
      

      【讨论】:

        【解决方案3】:

        onChanged 替换为 onEditingComplete

        onEditingComplete: (value) {
                    newController.text = value;
                    FocusScope.of(context).unfocus(); //use this to dismiss the screen keyboard
                }
        

        【讨论】:

          【解决方案4】:

          只需删除 onChanged 函数。没必要。

          【讨论】:

            猜你喜欢
            • 2021-02-03
            • 2021-12-23
            • 1970-01-01
            • 2022-01-22
            • 2023-01-28
            • 1970-01-01
            • 2021-10-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多