Flutter - TextField 失去焦点时失去值(value)

标签 flutter textfield

我在一个容器中有两个 TextField。第一个定义为 TextInputType.text,第二个定义为 TextInputType.number。每当我改变焦点(First<>Second 或 Second<>First)时,失去焦点的 TextField 也会失去其值(value)。

奇怪的是,如果我将两个文本字段都定义为 TextInputType.text,如果我将其中一个设置为除 TextInputType.text 之外的任何内容,则两个文本字段都会丢失键入的内容重视焦点。

非常烦人。我不知道为什么会这样。

这是 Flutter 的错误还是我做错了什么?

这是小部件代码:

class LoginInput extends StatelessWidget {
  final String hintText;
  final IconData icon;
  final String iconTag;
  final TextEditingController controller;
  final TextInputType inputType;
  final bool obscureText;

  LoginInput(this.hintText, this.icon, this.iconTag, this.controller,
      this.inputType, this.obscureText);

  @override
  Widget build(BuildContext context) {
    return new Column(children: <Widget>[
      new Container(
          height: 56.0,
          child: new Material(
              borderRadius: BorderRadius.all(Radius.circular(5.0)),
              elevation: 0.0,
              color: Colors.white,
              child: new Container(
                  child: new ListTile(
                leading: new Hero(
                    tag: iconTag, child: new Icon(icon, color: Colors.black)),
                title: new TextField(

                  keyboardType: inputType,
                  obscureText: obscureText,
                  textInputAction: TextInputAction.send,
                  controller: controller,
                  decoration: new InputDecoration(
                    border: InputBorder.none,
                    hintText: hintText,
                  ),
                  style: new TextStyle(
                      color: Colors.black,
                      fontSize: 18.0,
                      fontFamily: 'Caecilia',
                      fontWeight: FontWeight.w500),
                ),
              )))),
      new Divider(height: 0.0, color: Theme.of(context).dividerColor),
    ]);
  }
}

这样称呼:

  final LoginInput nameField = new LoginInput("Full name", Icons.face, "leading_icon_name", new TextEditingController(), TextInputType.text,false);

  final LoginInput birthField = new LoginInput("Birth date", Icons.date_range, "leading_icon_birth", new TextEditingController(), TextInputType.number, false);

最佳答案

如果你移动你的

final TextEditingController controller;

类外,TextField的内容不会在每次TextField失去焦点时重置。

将此应用于您的代码:

final TextEditingController controller;

class LoginInput extends StatelessWidget {
  final String hintText;
  final IconData icon;
  final String iconTag;
  final TextInputType inputType;
  final bool obscureText;

  LoginInput(this.hintText, this.icon, this.iconTag, this.inputType, this.obscureText);

  @override
  Widget build(BuildContext context) {
    return new Column(children: <Widget>[
      new Container(
          height: 56.0,
          child: new Material(
              borderRadius: BorderRadius.all(Radius.circular(5.0)),
              elevation: 0.0,
              color: Colors.white,
              child: new Container(
                  child: new ListTile(
                leading: new Hero(
                    tag: iconTag, child: new Icon(icon, color: Colors.black)),
                title: new TextField(

                  keyboardType: inputType,
                  obscureText: obscureText,
                  textInputAction: TextInputAction.send,
                  controller: controller,
                  decoration: new InputDecoration(
                    border: InputBorder.none,
                    hintText: hintText,
                  ),
                  style: new TextStyle(
                      color: Colors.black,
                      fontSize: 18.0,
                      fontFamily: 'Caecilia',
                      fontWeight: FontWeight.w500),
                ),
              )))),
      new Divider(height: 0.0, color: Theme.of(context).dividerColor),
    ]);
  }
}

关于Flutter - TextField 失去焦点时失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51821861/

相关文章:

ios - textField 阻止键盘显示事件

input - Robot Framework 文本字段清除和输入

flutter - 在 Flutter 的第一个字符中限制 "0"

user-interface - 当屏幕在 flutter 中滚动时动画小部件位置(包括 GIF)

android - 在Dart中从云中解析嵌套的JSON但出现类型错误

flutter - 当我双击 flutter_console.bat 时,它在一秒钟内打开和关闭?

android - 包含 TextFieldValue(text ='' , selection=TextRange(0, 0), composition=null) 的 MutableState 无法使用当前的 SaveableStateRegistry 保存

java - 当用户在字段中输入时,将 $ 符号添加到文本字段中

dart - 'String' 不是 'int' flutter 的子类型

android - 如何在 flutter 中具有这样的双向夹具 View ?