我正在创建“创建帐户”页面。它包含以下文本字段“名字,姓氏,电子邮件,密码”。如何创建下面的验证条件?
1.在空字段中显示文本“此字段是必填字段”
1.在电子邮件字段“输入有效的电子邮件”和“此电子邮件已被占用”中
验证码:
Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 35),
child: Container(
width: 310,
child: TextFormField(
controller: _firstnameController,
decoration: InputDecoration(
label: RichText(
text: TextSpan(
text: 'First Name',
style: GoogleFonts.poppins(
color: Color(0xffc1c1c1),
fontSize: 17,
letterSpacing: 0.4),
children: const [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontSize: 21,
)),
]),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Container(
width: 310,
child: TextFormField(
controller: _lastnameController,
decoration: InputDecoration(
label: RichText(
text: TextSpan(
text: 'Last Name',
style: GoogleFonts.poppins(
color: Color(0xffc1c1c1),
fontSize: 17,
letterSpacing: 0.4),
children: const [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontSize: 21,
)),
]),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Container(
width: 310,
child: TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
label: RichText(
text: TextSpan(
text: 'Enter Your Email',
style: GoogleFonts.poppins(
color: Color(0xffc1c1c1),
fontSize: 17,
letterSpacing: 0.4),
children: const [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontSize: 21,
)),
]),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Container(
width: 310,
child: TextFormField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
label: RichText(
text: TextSpan(
text: 'Enter Your Password',
style: GoogleFonts.poppins(
color: Color(0xffc1c1c1),
fontSize: 17,
letterSpacing: 0.4),
children: const [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontSize: 21,
)),
]),
),
suffixIcon: Icon(CupertinoIcons.eye_slash_fill, size: 25),
),
),
),
),
1条答案
按热度按时间cngwdvgl1#
若要向文本字段添加验证条件,可以使用TextFormField的validator属性。
验证
对于Eg(完整源代码):