Fighter,Textfield,如何先显示数字并能更改PUT文本 [英] Flutter, TextField, how to show up number firstly and able to change put text too
本文介绍了Fighter,Textfield,如何先显示数字并能更改PUT文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何先显示数字又能更改PUT文本?
目前我所做的是
keyboardType: TextInputType.number
这使第一步工作正常。
但是,它不能改回字母,只有数字是必填项。
推荐答案
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
focusNode: widget._childnod,
onChanged: (s) {
if (s.length == 1) {
setState(() {
FocusScope.of(context).unfocus();
widget._inputType = TextInputType.text;
});
Future.delayed(const Duration(milliseconds: 1), () {
FocusScope.of(context).requestFocus(widget._childnod);
});
}
},
keyboardType: widget._inputType,
),
)
这里我们添加了未来。延迟是因为我们需要一个很小的时间间隔来重新打开键盘,否则它将无法工作。这里我们不使用任何按钮,而是使用延迟
Future.delayed(const Duration(milliseconds: 1), () {
FocusScope.of(context).requestFocus(widget._childnod);
});
五月是这样的:
void main() => runApp(
MaterialApp(home: Scaffold(appBar: AppBar(), body: Changekeyboard())));
class Changekeyboard extends StatefulWidget {
Changekeyboard({Key? key}) : super(key: key);
TextInputType _inputType = TextInputType.number;
FocusNode? _childnod = FocusNode();
@override
_ChangekeyboardState createState() => _ChangekeyboardState();
}
class _ChangekeyboardState extends State<Changekeyboard> {
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
FlatButton(
color: Colors.blue,
onPressed: () {
FocusScope.of(context).unfocus();
setState(() {
if (widget._inputType == TextInputType.number)
widget._inputType = TextInputType.text;
else
widget._inputType = TextInputType.number;
});
},
child: Text(
"Change type",
style: TextStyle(color: Colors.white),
)),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
focusNode: widget._childnod,
onChanged: (s) {
if (s.length == 1) {
setState(() {
FocusScope.of(context).unfocus();
widget._inputType = TextInputType.text;
});
Future.delayed(const Duration(milliseconds: 1), () {
FocusScope.of(context).requestFocus(widget._childnod);
});
}
},
keyboardType: widget._inputType,
),
),
],
),
);
}
}
这篇关于Fighter,Textfield,如何先显示数字并能更改PUT文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文