【发布时间】:2020-11-17 22:46:05
【问题描述】:
我正在尝试创建一个一次只显示一个 textFormField 的表单。我创建了一个Form 和一个key: 和一个Container() 作为孩子。 Container() 调用了一个方法,该方法基于一个计数器返回一个 TextFormField 小部件,该计数器在我每次单击下一步按钮时都会递增。除了我在字段中输入的文本保留在每个字段更改之间之外,一切正常。这是我的相关代码:
...
Form(
key: _formkey,
child: Column(
children: <Widget>[
SizedBox(
height: _height * .10,
),
if (pageCount > 0 && pageCount <= 2)
_createField(pageCount),
RaisedButton(onPressed: ()=> _next()),
...
void _next() {
if (pageCount < 10) {
setState(() {
pageCount++;
});
}
}
...
Widget _createField(int count) {
switch (count) {
case 1:
return TextFormField(
textAlign: TextAlign.center,
decoration: textInputDecoration.copyWith(
hintText: 'name',
hintStyle: TextStyle(
color: Colors.grey[500],
fontStyle: FontStyle.italic,
),
),
validator: (val) => val.isEmpty ? 'enter your display name' : null,
onChanged: (val) {
setState(() => displayName = val);
},
);
break;
case 2:
return TextFormField(
textAlign: TextAlign.center,
decoration: textInputDecoration.copyWith(
hintText: 'email',
hintStyle: TextStyle(
color: Colors.grey[500],
fontStyle: FontStyle.italic,
),
),
validator: (val) => val.isEmpty ? 'enter an email' : null,
onChanged: (val) {
setState(() => email = val);
},
);
break;
...
default:
return null;
}
}
提示文本和验证有效,唯一的问题是我输入的任何内容都会留在下一个字段中。我尝试使用TextEditingController 清除该字段,但也许我没有正确使用它,因为它要么不起作用,要么有时我收到一个错误,即控制器被调用为空。我不确定我会把它放在哪里。我的最终目标是在填写表格时保留屏幕空间。我愿意接受有关其他方式来实现此目的的建议。
【问题讨论】:
标签: flutter