Skip to content

Instantly share code, notes, and snippets.

@timbergus
Created April 3, 2019 13:15
Show Gist options
  • Select an option

  • Save timbergus/89758cd7ab79e19669b3080fce440e41 to your computer and use it in GitHub Desktop.

Select an option

Save timbergus/89758cd7ab79e19669b3080fce440e41 to your computer and use it in GitHub Desktop.
flutter_login_form
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>[
// Email
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