Created
April 3, 2019 13:15
-
-
Save timbergus/89758cd7ab79e19669b3080fce440e41 to your computer and use it in GitHub Desktop.
flutter_login_form
This file contains hidden or 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
| import 'package:flutter/material.dart'; | |
| class Login extends StatefulWidget { | |
| @override | |
| _LoginState createState() => _LoginState(); | |
| } | |
| class _LoginState extends State<Login> { | |
| String _email; | |
| String _password; | |
| final _loginFormKey = GlobalKey<FormState>(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Form( | |
| key: _loginFormKey, | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| TextFormField( | |
| validator: (val) { | |
| if (val.isEmpty) { | |
| return 'You must enter a valid email!'; | |
| } | |
| return null; | |
| }, | |
| onSaved: (val) => _email = val, | |
| ), | |
| // Password | |
| TextFormField( | |
| obscureText: true, | |
| validator: (val) { | |
| if (val.isEmpty) { | |
| return 'You must enter a valid password!'; | |
| } | |
| return null; | |
| }, | |
| onSaved: (val) => _email = val, | |
| ), | |
| // Login button. | |
| RaisedButton( | |
| child: Text('Login'), | |
| onPressed: () { | |
| if (_loginFormKey.currentState.validate()) { | |
| _loginFormKey.currentState.save(); | |
| print('Email: $_email, password: $_password'); | |
| } | |
| }, | |
| ), | |
| ], | |
| ), | |
| ), | |
| ), | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment