Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

dart - Is there any Validator in TextFormField in Flutter?

class EmailITextField extends StatelessWidget {
  final String hintText;
  final IconData icon;
  final ValueChanged<String> onChanged;
  final TextEditingController controller;
  final Validator validator; //here
  const EmailITextField({
    Key key,
    this.hintText,
    this.icon = Icons.person,
    this.onChanged,
    this.validator,
    this.controller
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFieldContainer(
      child: TextFormField(
        validator: validator,
        controller: controller,
        onChanged: onChanged,
        cursorColor: kPrimaryightColor,
        decoration: InputDecoration(
          icon: Icon(
            icon,
            color: kPrimaryColor,
          ),
          labelText: hintText,
          labelStyle: TextStyle(color: kPrimaryColor) ,
          hintText: "[email protected]",
          border: InputBorder.none,
        ),
      ),
    );
  }
}

There is no Validator thing, i want to add Validator attribute on this reusable widget, but i dont know how to add the Validator attribute. any other name?

question from:https://stackoverflow.com/questions/65642431/is-there-any-validator-in-textformfield-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
EmailTextField(
  validator: (String value) {
              if (value.trim().isEmpty){ 
                return 'Email cannot be empty.';
              }
              return null;
  },
)

Like this, you can create more if statements to validate the inputted value you can also reference a method here like,

EmailTextField(validator: _validate);



String _validate() {
  if (value.trim().isEmpty){ 
  return 'Email cannot be empty.';
  }
  return null;

}

Forgive me if there is any syntax error


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...