更改表单中 TextFormField 的键盘类型
Change the keyboard type for a TextFormField in a Form
我有一个接受字母和数字的文本表单,键盘会动态变化。它工作正常,但是当我删除代码时,数字键盘仍然存在 这是示例
final _controller = TextEditingController();
var keyboardType = TextInputType.text;
FocusNode _focusNode = FocusNode();
TextFormField(
style: TextStyle(
color: Colors.black,
fontSize: 50,
fontWeight: FontWeight.w700),
decoration:
InputDecoration(labelText: "code", hintText: 'ABC1234'),
controller: _controller,
focusNode: _focusNode,
textCapitalization: TextCapitalization.characters,
keyboardType: keyboardType,
onChanged: (value) {
if (value.length == 3) {
FocusScope.of(context).unfocus();
setState(() {
keyboardType = TextInputType.number;
});
Future.delayed(Duration(milliseconds: 150)).then((value) {
FocusScope.of(context).requestFocus();
});
} else {
setState(() {
keyboardType = TextInputType.text;
});
}
}),
那是因为它已经开放了。您将必须执行与将其显示为数字键盘相同的步骤
即
else {
FocusScope.of(context).unfocus();
setState(() {
keyboardType = TextInputType.text;
});
Future.delayed(Duration(milliseconds: 150)).then((value) {
FocusScope.of(context).requestFocus();
});
另外,这种处理条件的方式在很多情况下会导致键盘的关闭和重新打开,您应该按如下方式进行
if (value.length == 3 && keyboardType == TextInputType.text) {
//....
} else if (value.length < 3 && keyboardType != TextInput.text) {
我有一个接受字母和数字的文本表单,键盘会动态变化。它工作正常,但是当我删除代码时,数字键盘仍然存在 这是示例
final _controller = TextEditingController();
var keyboardType = TextInputType.text;
FocusNode _focusNode = FocusNode();
TextFormField(
style: TextStyle(
color: Colors.black,
fontSize: 50,
fontWeight: FontWeight.w700),
decoration:
InputDecoration(labelText: "code", hintText: 'ABC1234'),
controller: _controller,
focusNode: _focusNode,
textCapitalization: TextCapitalization.characters,
keyboardType: keyboardType,
onChanged: (value) {
if (value.length == 3) {
FocusScope.of(context).unfocus();
setState(() {
keyboardType = TextInputType.number;
});
Future.delayed(Duration(milliseconds: 150)).then((value) {
FocusScope.of(context).requestFocus();
});
} else {
setState(() {
keyboardType = TextInputType.text;
});
}
}),
那是因为它已经开放了。您将必须执行与将其显示为数字键盘相同的步骤
即
else {
FocusScope.of(context).unfocus();
setState(() {
keyboardType = TextInputType.text;
});
Future.delayed(Duration(milliseconds: 150)).then((value) {
FocusScope.of(context).requestFocus();
});
另外,这种处理条件的方式在很多情况下会导致键盘的关闭和重新打开,您应该按如下方式进行
if (value.length == 3 && keyboardType == TextInputType.text) {
//....
} else if (value.length < 3 && keyboardType != TextInput.text) {