gpt4 book ai didi

flutter - 在同一Wrap子项下为不同的textformfields指定不同的字段输入

转载 作者:行者123 更新时间:2023-12-03 04:36:39 26 4
gpt4 key购买 nike

我遵循了Flutter文档here上的有关如何一次验证多个TextFormField的知识。但是在此示例中,所有文本格式字段都是使用相同的输入字段(即名称)创建的。我希望可以将不同的字段用于不同的输入,例如名称,密码,电子邮件等。有人可以帮助实现上述内容吗?

   class _MyStatefulWidgetState extends State<MyStatefulWidget> {
Widget build(BuildContext context) {
return Material(
child: Center(
child: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
// Pressing enter on the field will now move to the next field.
LogicalKeySet(LogicalKeyboardKey.enter): NextFocusIntent(),
},
child: FocusTraversalGroup(
child: Form(
autovalidate: true,
onChanged: () {
Form.of(primaryFocus.context).save();
},
child: Wrap(
children: List<Widget>.generate(5, (int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ConstrainedBox(
constraints: BoxConstraints.tight(Size(200, 50)),
child: TextFormField(
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name *',
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
validator: (String value) {
return value.contains('@') ? 'Do not use the @ char.' : null;
},
),
),
);
}),
),
),
),
),
),
);
}
}

最佳答案

创建一个以hintTextlabelText等作为字段的类,列出此类的实例,并将其提供给TextFormField:

class _MyStatefulWidgetState extends State<MyStatefulWidget> {

final List<HintAndLabel> list = const <HintAndLabel>[HintAndLabel(labelText:'Name',hintText:"What do people call you?"),
HintAndLabel(labelText:'label',hintText:"hint"),
HintAndLabel(labelText:'label',hintText:"hint"),
HintAndLabel(labelText:'label',hintText:"hint"),
HintAndLabel(labelText:'label',hintText:"hint")];
@override
Widget build(BuildContext context) {
return Material(
child: Center(

child: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
// Pressing enter on the field will now move to the next field.
LogicalKeySet(LogicalKeyboardKey.enter): NextFocusIntent(),
},

child: FocusTraversalGroup(
child: Form(
autovalidate: true,
onChanged: () {
Form.of(primaryFocus.context).save();
},
child: Wrap(
children: List<Widget>.generate(5, (int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ConstrainedBox(
constraints: BoxConstraints.tight(Size(200, 50)),
child: TextFormField(
decoration: InputDecoration(
icon: Icon(Icons.person),
hintText: list[index].hintText,
labelText: list[index].labelText,
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
validator: (String value) {
return value.contains('@') ? 'Do not use the @ char.' : null;
},
),
),
);
}),
),
),
),
),
),
);
}
}


class HintAndLabel
{
final String hintText;
final String labelText;
const HintAndLabel({this.hintText,this.labelText});

}


关于flutter - 在同一Wrap子项下为不同的textformfields指定不同的字段输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63986419/

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