我有一个TextFormField,应该接收数字。输入第一个数字时,应跳到第二个TextFormField,然后跳到第三个TextFormField。每个TextFormField都有FocusNode属性,我只是不知道如何使用它。我有这个
TextFormField( //First Field
autofocus: true,
focusNode: FocusNode(),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0)
),
),
style: TextStyle(
color: Colors.orange,
fontSize: 15.0,
),
keyboardType:
TextInputType.number,
maxLength: 1,
),
// second Field
TextFormField(),
//third Field
答案 0 :(得分:1)
我相信这或多或少是您想要实现的目标:
void main() {
runApp(MaterialApp(home: PassCodeExample()));
}
class PassCodeExample extends StatelessWidget {
FocusNode f1 = FocusNode();
FocusNode f2 = FocusNode();
FocusNode f3 = FocusNode();
FocusNode f4 = FocusNode();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
child: TextField(
focusNode: f1,
keyboardType: TextInputType.number,
onChanged: (String newVal) {
if (newVal.length == 1) {
f1.unfocus();
FocusScope.of(context).requestFocus(f2);
}
},
decoration: InputDecoration(border: OutlineInputBorder()),
),
),
Flexible(
child: TextField(
focusNode: f2,
keyboardType: TextInputType.number,
onChanged: (String newVal) {
if (newVal.length == 1) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
}
},
decoration: InputDecoration(border: OutlineInputBorder()),
),
),
Flexible(
child: TextField(
focusNode: f3,
keyboardType: TextInputType.number,
onChanged: (String newVal) {
if (newVal.length == 1) {
f3.unfocus();
FocusScope.of(context).requestFocus(f4);
}
},
decoration: InputDecoration(border: OutlineInputBorder()),
),
),
Flexible(
child: TextField(
focusNode: f4,
keyboardType: TextInputType.number,
decoration: InputDecoration(border: OutlineInputBorder()),
),
),
]),
),
),
);
}
}
您可以通过使用onSubmitted
甚至向TextEditingController
提供唯一的TextField
来实现相同目的
答案 1 :(得分:0)
大多数情况下,如果将Textfield / TextFormField放置为Form的子级,则问题将得到解决。