Last active
June 19, 2019 12:04
-
-
Save bettdouglas/ece7d13d81a4276b48a72a0860cfec8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DynamicInput extends StatefulWidget { | |
final String label,hint; | |
///type denotes number or string or email | |
final DataType type; | |
final TextEditingController controller; | |
... | |
} | |
class _DynamicInputState extends State<DynamicInput>{ | |
... | |
Widget build(BuildContext context) { | |
return TextFormField( | |
controller: widget.controller, // | |
keyboardType: keyBoardType(widget.type), ///gets keyboardtype based on the DataTypes we defined | |
validator: (str)=>validator(str), //validator function that checks the type of data needed and validates it | |
decoration: InputDecoration( | |
hintText: widget.hint, //text input hint | |
labelText: widget.label, //text widget label | |
helperText: widget.hint, | |
), | |
); | |
} | |
///This checks the current text in textinput form and | |
///validates depending with the type of DataType we defined above | |
String validator(String s) { | |
var validator = Validator(s,widget.type).validator; | |
switch (widget.type) { | |
case DataType.EMAIL: | |
return validator.validateRuleFor('email').errorText; | |
break; | |
case DataType.INTEGER: | |
return validator.validateRuleFor('int').errorText; | |
break; | |
case DataType.TEXT: | |
return validator.validateRuleFor('text').errorText; | |
case DataType.PHONE: | |
return validator.validateRuleFor('phone').errorText; | |
case DataType.NAME: | |
return validator.validateRuleFor('name').errorText; | |
default: | |
return validator.validateRuleFor('text').errorText; | |
} | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment