gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:47:11 26 4
gpt4 key购买 nike

我在一个容器中有两个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com