flutter - TextFormField Validator 在 Flutter 中不起作用
问题描述
我创建了一个页面,他们可以在其中向用户发送验证码。
但是即使我创建了一个 if else 案例,验证器也不起作用。当我单击按钮(即使它是空的)时,它会转到另一个页面。我怎样才能解决这个问题?
这是我的完整代码;
我在最后评论之后将它添加到表单小部件中,但它没有用
return Scaffold(
body: SafeArea(
child: Form(
key: _formKey,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
),
FlatButton(
onPressed:(){
Navigator.pop(context);
},
child: Text(
allTranslations.text('login'),
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: HexColor('#4A8EB0'),
),
),
),
],
),
),
Container(
padding: EdgeInsets.only(bottom: 10),
width: double.infinity,
child: Text(
allTranslations.text('signUp'),
style: textTheme.headline5,
textAlign: TextAlign.center,
maxLines: 3,
),
),
Padding(
padding: EdgeInsets.all(15),
child: TextFormField(
controller: _controller,
keyboardType: TextInputType.text,
decoration: CommonInputStyle.textFieldStyle(
hintTextStr: 'E-mail',
labelTextStr: 'E-mail',
),
validator: (value) =>
value.isEmpty ? 'Password cannot be blank' : null,
),
),
Padding(
padding: EdgeInsets.all(16.0),
child: Container(
height: 50.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: HexColor('#4A8EB0'),
),
child: InkWell(
onTap: () async {
setState(() {
Navigator.pushNamed(context,AnotherPage.routeName);
});
},
child: Center(
child: Text(
'Send Code',
),
),
),
],
),
),
),
);
}
}
解决方案
正如 Limbou 提到的,您需要在用户点击按钮时进行验证。如果成功,它将返回 true。
onTap: () async {
if(_formKey.currentState.validate()) {
setState(() {
Navigator.pushNamed(context, AnotherPage.routeName);
});
}
}
推荐阅读
- javascript - HttpInterceptor catchError 没有捕获 - Angular 7
- c# - 将多个任务分配给多个线程
- java - 混淆使用带有用户输入的参数构造函数从类创建对象
- python - 如何在 pyspark 应用程序中维护临时字典?
- ios - 在使用捏合手势缩小文本字段后输入文本字段时出现奇怪的行为?
- python - LSI 主题连贯性的“好”值是多少?
- ios - 我在哪里可以获得 Xamarin.iOS 12.2.1.13 的详细信息或发行说明?
- mysql - 防止 INTO OUTFILE 命令的安全文件私有
- c++ - 升压精神的懒惰论点不接受 lambda
- c# - 如何在不刷新网页的情况下从下拉列表索引更改事件的数据库中检索数据