如何对文本字段进行多重验证

问题描述 投票:0回答:1

我有textFeilds我放置了空的字段验证,但是我不知道如何放置另一个验证(多重验证),例如“电子邮件无效”或我想要的任何内容。这只是重要的代码,所有代码都是冗长的]

 final TextEditingController _emailControl = new TextEditingController();
   bool _validateEmail = false;




child: TextField(
                style: TextStyle(
                  fontSize: 15.0,
                  color: Colors.black,
                ),
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(10.0),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0),
                    borderSide: BorderSide(color: Colors.white,),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.white,),
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  hintText: "Email",
                  prefixIcon: Icon(
                    Icons.mail_outline,
                    color: Colors.black,
                  ),
                  hintStyle: TextStyle(
                    fontSize: 15.0,
                    color: Colors.black,
                  ),

                labelText: 'Email',
                errorText: _validateEmail ? 'Email Can\'t Be Empty' : null,
                ),
                maxLines: 1,
                controller: _emailControl,
              ),

下面按下我的注册按钮

 onPressed: () {
                      setState(() {

                        _emailControl.text.isEmpty ? _validateEmail = true : _validateEmail = false;


                  if ( _validateEmail == false ){
                        _isLoading = true;
                      khan();

                        }
                      });
validation flutter dart uitextfield
1个回答
0
投票

使用TextFormField。

TextFormField(
validator(val):{
if(val.isEmpty){
return "This field cannot be empty",

}
else if(next conditon){
return ....
}
}
                style: TextStyle(
                  fontSize: 15.0,
                  color: Colors.black,
                ),
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(10.0),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0),
                    borderSide: BorderSide(color: Colors.white,),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.white,),
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  hintText: "Email",
                  prefixIcon: Icon(
                    Icons.mail_outline,
                    color: Colors.black,
                  ),
                  hintStyle: TextStyle(
                    fontSize: 15.0,
                    color: Colors.black,
                  ),

                labelText: 'Email',
                errorText: _validateEmail ? 'Email Can\'t Be Empty' : null,
                ),
                maxLines: 1,
                controller: _emailControl,
              ),

最后使用表格来验证文本字段。

© www.soinside.com 2019 - 2024. All rights reserved.