Skip to content

Instantly share code, notes, and snippets.

@eduardoflorence
Last active April 10, 2024 10:40
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save eduardoflorence/e49780ab232fa8ad7767bbdbf8389f1e to your computer and use it in GitHub Desktop.
Save eduardoflorence/e49780ab232fa8ad7767bbdbf8389f1e 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,
)
],
),
),
),
);
}
}
@bubnenkoff
Copy link

Big thanks! I looked same example for a long time!

@bubnenkoff
Copy link

bubnenkoff commented Aug 26, 2021

@eduardoflorence, could you help me with follow code: https://stackoverflow.com/questions/68934499/cant-get-onchanged-properly-work-with-getx

I can't figure out how to work with onChange

@bouaziz-mohamed-amine
Copy link

thank you

@Hochul822
Copy link

Thank you!

@amotalles
Copy link

Thank you

@clust3rsekt0r
Copy link

clust3rsekt0r commented Apr 30, 2022

@eduardoflorence, Thanks for the example, I'm just learning, I know the problem below is due to different flutter version and the null safety feature, but I try everything and still controller.validator is not accepted.

validator: controller.validator,

it is given me this error

Error: The argument type 'String Function(String)' can't be assigned to the parameter type 'String? Function(String?)?' because 'String?' is nullable and 'String' isn't.

@ZheruiL
Copy link

ZheruiL commented May 15, 2022

@eduardoflorence, Thanks for the example, I'm just learning, I know the problem below is due to different flutter version and the null safety feature, but I try everything and still controller.validator is not accepted.

validator: controller.validator,

it is given me this error

Error: The argument type 'String Function(String)' can't be assigned to the parameter type 'String? Function(String?)?' because 'String?' is nullable and 'String' isn't.

@clust3rsekt0r
you can simply change the validator to =>

String? validator(String? value) {
  if (value == null || value.isEmpty) {
    return 'Please this field must be filled';
  }
  return null;
}

and it will work.

you need to check if the value is null before using value.isEmpty

@Aboubacry-Tall
Copy link

Big thanks

@bubnenkoff
Copy link

@zegraoui
Copy link

zegraoui commented Oct 9, 2022

big big big thanks...

@AhmadSadik1
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment