Skip to content

Instantly share code, notes, and snippets.

@quangpd
Forked from eduardoflorence/main.dart
Created November 12, 2021 16:41
Show Gist options
  • Save quangpd/af80e6d4c83278f226d11d205b28caf4 to your computer and use it in GitHub Desktop.
Save quangpd/af80e6d4c83278f226d11d205b28caf4 to your computer and use it in GitHub Desktop.
Getx - Sample Form
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(GetMaterialApp(
initialRoute: '/login',
getPages: [
GetPage(
name: '/login',
page: () => LoginPage(),
binding: LoginBinding(),
),
],
));
}
class LoginBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut(() => LoginController());
}
}
class LoginController extends GetxController {
final loginFormKey = GlobalKey<FormState>();
final emailController = TextEditingController();
final passwordController = TextEditingController();
@override
void onInit() {
// Simulating obtaining the user name from some local storage
emailController.text = 'foo@foo.com';
super.onInit();
}
@override
void onClose() {
emailController.dispose();
passwordController.dispose();
super.onClose();
}
String validator(String value) {
if (value.isEmpty) {
return 'Please this field must be filled';
}
return null;
}
void login() {
if (loginFormKey.currentState.validate()) {
checkUser(emailController.text, passwordController.text).then((auth) {
if (auth) {
Get.snackbar('Login', 'Login successfully');
} else {
Get.snackbar('Login', 'Invalid email or password');
}
passwordController.clear();
});
}
}
// Api Simulation
Future<bool> checkUser(String user, String password) {
if (user == 'foo@foo.com' && password == '123') {
return Future.value(true);
}
return Future.value(false);
}
}
class LoginPage extends GetView<LoginController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('LOGIN')),
body: Container(
padding: const EdgeInsets.all(16.0),
child: Form(
key: controller.loginFormKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: controller.emailController,
decoration: const InputDecoration(labelText: 'E-mail'),
validator: controller.validator,
),
TextFormField(
controller: controller.passwordController,
decoration: const InputDecoration(labelText: 'Password'),
validator: controller.validator,
obscureText: true,
),
RaisedButton(
child: Text('Login'),
onPressed: controller.login,
)
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment