Skip to content

Instantly share code, notes, and snippets.

@DevKhalyd
Created August 27, 2020 22:15
Show Gist options
  • Save DevKhalyd/ebdeb2e067da6efc3f1b3553907a08a9 to your computer and use it in GitHub Desktop.
Save DevKhalyd/ebdeb2e067da6efc3f1b3553907a08a9 to your computer and use it in GitHub Desktop.
Form Example Flutter
//Use a globalKey
final _formKey = GlobalKey<FormState>();
//Use that key inside a Form Widget
return Form(
key: _formKey,
autovalidate: autovaldiate,
child: Column(
children: [
Space(0.05),
SizedBox(
height: getScreenHeight(context) * 0.25,
width: getScreenWidth(context) * 0.5,
child: svgIcon),
Text(
'Bienvenido',
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: getWidgetPixels(context) * 0.065),
),
Space(0.01),
Text('Inicia sesión o crea una cuenta'),
Space(0.05),
TextFormFieldCustom(
labelText: 'E-mail',
//Use validator to make validations
validator: (value) {
if (!isValidateEmail(value)) {
return 'E-mail incorrecto';
}
return null;
},
//onSaved when you need save the valu validated
onSaved: (saved) {
email = saved;
},
isUnderLine: false,
keyboardType: TextInputType.emailAddress,
prefixIcon: Icon(Icons.email),
),
TextFormFieldCustom(
labelText: 'Contraseña',
validator: (value) {
if (!isValidatePassword(value)) {
return 'Contraseña incorrecta';
}
return null;
},
onSaved: (saved) {
password = saved;
},
isUnderLine: false,
keyboardType: TextInputType.visiblePassword,
obscureText: isNotVisiblePassword,
suffixIcon: isNotVisiblePassword
? IconButton(
icon: Icon(Icons.visibility_off),
onPressed: onVisibilityPassword)
: IconButton(
icon: Icon(Icons.visibility),
onPressed: onVisibilityPassword),
prefixIcon: Icon(Icons.lock_outline),
),
Padding(
padding:
EdgeInsets.only(right: getWidgetPixels(context)) * 0.05,
child: Align(
alignment: Alignment.centerRight,
child: Text('¿Olvidaste la contraseña?')),
),
Space(0.05),
ButtonCustom(
'Iniciar sesión',
isRaised: true,
colorBtn: primaryColor,
paddingV: getWidgetPixels(context) * 0.04,
paddingH: getWidgetPixels(context) * 0.1,
onPressed: () {
//Use this line to validate if the form is everything ok
if (!_formKey.currentState.validate()) {
setState(() {
//autovalidate means add a listener for each form field
//Put false to defalut nice UX
autovaldiate = true;
});
return;
}
//Save the values and make something with that data
_formKey.currentState.save();
print(email);
print(password);
},
),
Space(0.05),
RichTextCustomNormal(
'¿No tienes una cuenta? ',
'Crea una',
reversed: true,
onPressed: () => nextPage(context, SignUpScreen()),
),
],
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment