Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Created June 10, 2020 02:55
Show Gist options
  • Save IhwanID/91154a114645cff58e50fa1e5314ae18 to your computer and use it in GitHub Desktop.
Save IhwanID/91154a114645cff58e50fa1e5314ae18 to your computer and use it in GitHub Desktop.
Simple Login Register Page in Flutter
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: LoginRegisterPage(),
);
}
}
enum FormType { Login, Register }
class LoginRegisterPage extends StatefulWidget {
@override
_LoginRegisterPageState createState() => _LoginRegisterPageState();
}
class _LoginRegisterPageState extends State<LoginRegisterPage> {
FormType currentState = FormType.Login;
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController usernameController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(currentState == FormType.Login ? "Login" : "Register"),
),
body: Container(
margin: EdgeInsets.all(16),
child: Column(
children: <Widget>[
TextField(
controller: usernameController,
decoration: InputDecoration(labelText: 'Username'),
),
currentState == FormType.Register
? TextField(
controller: emailController,
decoration: InputDecoration(labelText: 'Email'),
)
: Container(),
TextField(
controller: passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
),
SizedBox(
height: 20,
),
MaterialButton(
textColor: Colors.white,
color: Colors.blueAccent,
child:
Text(currentState == FormType.Login ? "Login" : "Register"),
onPressed: () {
if (currentState == FormType.Login) {
print(
"you are login with id ${usernameController.text} & password ${passwordController.text}");
} else {
print(
"you are register with id ${usernameController.text} , email ${emailController.text} & password ${passwordController.text}");
}
}),
FlatButton(
child: Text(currentState == FormType.Register
? 'Have an account? Click here to login.'
: 'Don\'nt have an account? Create new Accoun'),
onPressed: () {
setState(() {
if (currentState == FormType.Login) {
currentState = FormType.Register;
} else {
currentState = FormType.Login;
}
});
},
)
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment