import 'package:flutter/material.dart'; class CredentialsForm extends StatefulWidget { CredentialsForm({Key key}) : super(key: key); @override _CredentialsState createState() => _CredentialsState(); } class _CredentialsState extends State { String username; String password; bool _hidden = true; final _credFormKey = GlobalKey(); InputDecoration _textFieldDecoration(String label, IconData icon) { return InputDecoration( labelText: label, icon: Icon(icon), hintText: 'Please, insert your zucchetti account $label', ); } String validateUsername(String username) { if (username.isEmpty) { return 'Username must not be empty'; } return null; } String validatePassword(String password) { if (password.isEmpty) { return 'Password must not be empty'; } return null; } @override Widget build(BuildContext context) { return Form( key: _credFormKey, child: Column(children: [ TextFormField( autofocus: true, autocorrect: false, decoration: this._textFieldDecoration("username", Icons.alternate_email), validator: this.validateUsername), TextFormField( autocorrect: false, decoration: this._textFieldDecoration("password", Icons.lock), validator: this.validatePassword, obscureText: this._hidden), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ RaisedButton( onPressed: () { if (_credFormKey.currentState.validate()) { Scaffold.of(context) .showSnackBar(SnackBar(content: Text('Processing Data'))); } }, child: Text('Validate'), ), Row(children: [ Text('Reveal password'), Checkbox( value: !this._hidden, onChanged: (bool value) { setState(() { _hidden = !value; }); return; }) ]) ]), ])); } }