问题描述
但是即使我创建了if case,验证器也无法工作。 当我单击按钮(即使它是空的)时,它会转到另一页。 我该如何解决?
这是我的完整代码;
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),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);
});
}
}