Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Last active January 4, 2021 18:38
Show Gist options
  • Save prafullakumar/26e24aa39ce670769d0e68a0870f14d3 to your computer and use it in GitHub Desktop.
Save prafullakumar/26e24aa39ce670769d0e68a0870f14d3 to your computer and use it in GitHub Desktop.
Flutter: Demo login page
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: TextFieldFocusExample(),
);
}
}
class TextFieldFocusExample extends StatefulWidget {
@override
State createState() => TextFieldFocusExampleState();
}
class TextFieldFocusExampleState extends State<TextFieldFocusExample> {
TextEditingController _emailController,_pwController;
FocusNode _emailFocus,_pwFocus;
// Initially password is obscure
bool _obscureText = true;
String _password;
String _email;
bool isValidEmail() {
if ((_email == null) || (_email.length == 0)) {
return true;
}
return RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(_email);
}
bool isValidPassword() {
if ((_password == null) || (_password.length == 0)) {
return true;
}
return (_password?.length > 6);
}
void _toggle() {
setState(() {
_obscureText = !_obscureText;
});
}
void _validate() {
setState(() {
_email = _emailController.text;
_password = _pwController.text;
});
}
void performLogin() {
//login here
}
@override
void initState() {
_emailController = TextEditingController();
_pwController = TextEditingController();
_emailFocus = FocusNode();
_pwFocus = FocusNode();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
children: <Widget>[
const SizedBox(height: 80.0),
Center(
child: Text('Login',style: TextStyle(
fontSize: 32.0
),),
),
const SizedBox(height: 80.0),
Padding(
padding: const EdgeInsets.all(16.0),
child: Material(
borderRadius: BorderRadius.circular(10.0),
child: TextField(
focusNode: _emailFocus,
controller: _emailController,
obscureText: false,
keyboardType: TextInputType.emailAddress, //show email keyboard
textInputAction: TextInputAction.next,
onSubmitted: (input){
_emailFocus.unfocus();
_email = input;
FocusScope.of(context).requestFocus(_pwFocus);
},
onTap: _validate,
decoration: InputDecoration(
labelText: "Email",
hintText: 'Enter your password',
errorText: isValidEmail() ? null : "Invalid Email Address",
),
),
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Material(
borderRadius: BorderRadius.circular(10.0),
child: TextField(
focusNode: _pwFocus,
controller: _pwController,
obscureText: _obscureText,
textInputAction: TextInputAction.done,
onSubmitted: (input){
_pwFocus.unfocus();
_password = input;
performLogin();
},
onTap: _validate,
decoration: InputDecoration(
labelText: "Password",
hintText: 'Enter your password',
errorText: isValidPassword() ? null : "Password too short.",
suffixIcon: IconButton(
icon: Icon(
_obscureText ? Icons.visibility : Icons.visibility_off,
color: Theme.of(context).primaryColorDark,
),
onPressed: _toggle,
),
),
),
),
),
const SizedBox(height: 40.0),
ButtonBar(
children: <Widget>[
RaisedButton(onPressed: (){
performLogin();
},child: Text('Login'),color: Colors.white)
],
)
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment