Skip to content

Instantly share code, notes, and snippets.

@bettdouglas
Last active June 19, 2019 12:04
Show Gist options
  • Save bettdouglas/ece7d13d81a4276b48a72a0860cfec8b to your computer and use it in GitHub Desktop.
Save bettdouglas/ece7d13d81a4276b48a72a0860cfec8b to your computer and use it in GitHub Desktop.
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