大家好。我对flutter textformfield有问题。当我传递到下一个textformfield时,它不会使上一个失去焦点。当点击下一个字段时也会发生。我在真实手机(G3和Mi 9t)上试用了我的应用,并且两者都发生了。我在网上找不到解决方案。我在下面添加图片。图像显示问题更加清晰。感谢您的帮助。
表格代码:
Form(
key: _formKey,
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
textInputAction: TextInputAction.next,
focusNode: _emailFocus,
onFieldSubmitted: (term) {
_emailFocus.unfocus();
_fieldFocusChange(
context, _emailFocus, _passwordFocus);
},
validator: (value) =>
value.isEmpty ? 'Enter an email' : null,
onChanged: (value) {
setState(() => email = value);
},
),
SizedBox(
height: 20.0,
),
TextFormField(
decoration: InputDecoration(labelText: 'Password'),
textInputAction: TextInputAction.done,
validator: (value) => value.length < 6
? 'Enter an password 6+ chars long'
: null,
obscureText: true,
focusNode: _passwordFocus,
onFieldSubmitted: (term) async {
_passwordFocus.unfocus();
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await _authService
.registration(email, password);
if (result == null) {
setState(() {
error = "please supply a valid email";
loading = false;
});
}
}
},
onChanged: (value) {
setState(() => password = value);
},
),
SizedBox(
height: 20.0,
),
RaisedButton(
color: Theme.of(context).primaryColor,
child: Text('Register'),
onPressed: () async {
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await _authService
.registration(email, password);
if (result == null) {
setState(() {
error = "please supply a valid email";
loading = false;
});
}
}
},
),
SizedBox(height: 12.0),
Text(error,
style:
TextStyle(color: Colors.red, fontSize: 14.0)),
],
)
),
答案 0 :(得分:0)
您要重点放在下一个字段上,而不是将焦点放在当前字段上。在您提交的文件中,您需要调用requestFocus()
或nextFocus()
(documentation)将焦点移至以下字段,在这种情况下,请按照我在下面的操作输入密码字段:
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
textInputAction: TextInputAction.next,
focusNode: _emailFocus,
onFieldSubmitted: (term) {
_passwordFocus.requestFocus();
_fieldFocusChange(
context, _emailFocus, _passwordFocus);
},
validator: (value) =>
value.isEmpty ? 'Enter an email' : null,
onChanged: (value) {
setState(() => email = value);
},
),
我还建议您将Button上的代码移至某个方法,以便您可以在Button上重新使用该代码,然后在Password字段中提交:
void submitForm(){
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await _authService
.registration(email, password);
if (result == null) {
setState(() {
error = "please supply a valid email";
loading = false;
});
}
}
}