【发布时间】:2020-09-17 21:45:12
【问题描述】:
我创建了一个页面,他们可以在其中向用户发送验证码。
但即使我创建了一个 if else 案例,验证器也不起作用。 当我单击按钮(即使它是空的)时,它会转到另一个页面。 我该如何解决这个问题?
这是我的完整代码;
我在最后一个 cmets 之后在表单小部件中添加了它,但它没有工作
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',
),
),
),
],
),
),
),
);
}
}
【问题讨论】:
-
TextFormField 必须是 Form 小部件的子级,然后在 Form 小部件上您可以调用方法
validate,但您仍然必须手动执行此操作。 -
即使我附上了表单小部件,它仍然是一样的:/
-
您是否手动拨打
validate?请分享代码 -
我尝试打印但没有得到任何结果
-
请使用您当前的代码库编辑您的帖子。我猜你没有正确调用 validate 方法。
标签: flutter text textformfield