Created
June 10, 2020 02:55
-
-
Save IhwanID/91154a114645cff58e50fa1e5314ae18 to your computer and use it in GitHub Desktop.
Simple Login Register Page in Flutter
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'; | |
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