Skip to content

Instantly share code, notes, and snippets.

@ChangJoo-Park
Last active January 22, 2020 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChangJoo-Park/8d7dd37aaab21bf827787fc88890aefa to your computer and use it in GitHub Desktop.
Save ChangJoo-Park/8d7dd37aaab21bf827787fc88890aefa to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Login Form Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
FocusNode _emailFieldFocusNode = FocusNode();
FocusNode _passwordFieldFocusNode = FocusNode();
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String _email;
String _password;
bool _tryLogin = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 300,
child: _buildLoginForm(),
),
],
),
),
);
}
Form _buildLoginForm() {
return Form(
key: _formKey,
autovalidate: true,
child: Column(
children: <Widget>[
_buildEmailFormField(),
_buildPasswordFormField(),
_tryLogin
? Container(
padding: EdgeInsets.symmetric(vertical: 6.0),
child: CircularProgressIndicator())
: Container(
child: RaisedButton(
child: Text('Login', style: TextStyle(color: Colors.white)),
onPressed: _login,
color: Theme.of(context).primaryColor,
),
),
],
),
);
}
_buildEmailFormField() {
return TextFormField(
focusNode: _emailFieldFocusNode,
textInputAction: TextInputAction.next,
decoration: InputDecoration(hintText: 'Email'),
validator: (value) {
if (value.isEmpty) {
return 'Please insert email';
} else
return null;
},
onFieldSubmitted: (String value) {
_emailFieldFocusNode.unfocus();
FocusScope.of(context).requestFocus(_passwordFieldFocusNode);
},
onSaved: (String value) {
setState(() {
this._email = value;
});
},
);
}
_buildPasswordFormField() {
return TextFormField(
focusNode: _passwordFieldFocusNode,
textInputAction: TextInputAction.done,
obscureText: true,
decoration: InputDecoration(hintText: 'Password'),
validator: (value) {
if (value.isEmpty) {
return 'Please insert password';
} else
return null;
},
onFieldSubmitted: (String value) {
_passwordFieldFocusNode.unfocus();
},
onSaved: (String value) {
setState(() {
this._password = value;
});
},
);
}
_login() async {
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
setState(() {
this._tryLogin = true;
});
await _requestLogin(
email: this._email,
password: this._password,
);
setState(() {
this._tryLogin = false;
});
}
Future<bool> _requestLogin({String email, String password}) {
return Future.delayed(Duration(seconds: 2), () {
return true;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment