Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Last active June 10, 2020 08:42
Show Gist options
  • Save IhwanID/275dcbac208764d0a477c24f1ffb6390 to your computer and use it in GitHub Desktop.
Save IhwanID/275dcbac208764d0a477c24f1ffb6390 to your computer and use it in GitHub Desktop.
Login Register Page in Flutter with separation widget
import 'package:flutter/material.dart';
import 'package:fluttercallback/common/action_button.dart';
import 'package:fluttercallback/common/custom_text_field.dart';
import 'package:fluttercallback/common/switch_button.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) {
bool isLogin = currentState == FormType.Login;
return Scaffold(
appBar: AppBar(
title: Text(isLogin ? "Login" : "Register"),
),
body: Container(
margin: EdgeInsets.all(16),
child: Column(
children: <Widget>[
CustomTextField(
controller: usernameController,
labelText: 'Username',
),
!isLogin
? CustomTextField(
controller: emailController,
labelText: 'Email',
)
: Container(),
CustomTextField(
controller: passwordController,
labelText: 'Password',
isObscureText: true,
),
SizedBox(
height: 20,
),
ActionButton(
state: currentState,
onPressed: () {
if (isLogin) {
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}");
}
}),
SwitchButton(
state: currentState,
onPressed: () {
setState(() {
if (isLogin) {
currentState = FormType.Register;
} else {
currentState = FormType.Login;
}
});
},
)
],
),
),
);
}
}
import 'package:flutter/material.dart';
class CustomTextField extends StatelessWidget {
final TextEditingController controller;
final String labelText;
final bool isObscureText;
CustomTextField(
{this.controller, this.labelText, this.isObscureText = false});
@override
Widget build(BuildContext context) {
return TextField(
controller: controller,
decoration: InputDecoration(labelText: labelText),
obscureText: isObscureText,
);
}
}
import 'package:flutter/material.dart';
import 'package:fluttercallback/main.dart';
class ActionButton extends StatelessWidget {
final FormType state;
final VoidCallback onPressed;
ActionButton({this.state, this.onPressed});
@override
Widget build(BuildContext context) {
return MaterialButton(
textColor: Colors.white,
color: Colors.blueAccent,
child: Text(state == FormType.Login ? "Login" : "Register"),
onPressed: onPressed);
}
}
import 'package:flutter/material.dart';
import 'package:fluttercallback/main.dart';
class SwitchButton extends StatelessWidget {
final FormType state;
final VoidCallback onPressed;
SwitchButton({this.state, this.onPressed});
@override
Widget build(BuildContext context) {
return FlatButton(
child: Text(state == FormType.Register
? 'Have an account? Click here to login.'
: 'Don\'nt have an account? Create new Accoun'),
onPressed: onPressed,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment