Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...