Hi,
I鈥檓 trying to get my head around doing this with Get:
https://bloclibrary.dev/#/flutterlogintutorial
Any examples or tips?
Hi, the bloc documentation is fantastic, and very complete.
However login is one of the simplest things to do with Get (in fact, everything is simple to do with Get).
I will try to write in a couple of minutes a login and home screen without using StatefulWidget, with a few lines of code, and a lot of organization.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(
GetMaterialApp(
initialBinding: InitialBinding(),
home: Splash(),
),
);
}
class Repository {
Future loginAuth() {
String user = "Pedro";
return Future.value(user);
}
}
class InitialBinding implements Bindings {
@override
void dependencies() {
Get.put(Repository());
Get.put(AuthController());
}
}
class AuthController extends RxController {
final isLogged = false.obs;
String user;
@override
void onInit() {
ever(isLogged, checkLogged);
}
@override
void onReady() {
checkLogged(null);
super.onReady();
}
void login() async {
user = await Get.find<Repository>().loginAuth();
if (user != null) {
isLogged.value = true;
} else {
Get.snackbar("Error", "Password incorrect");
}
}
void logout() async {
user = null;
isLogged.value = false;
}
void checkLogged(_) {
if (isLogged.value) {
Get.off(Home());
} else {
Get.off(Login());
}
}
}
class Splash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(body: Center(child: Text('Loading')));
}
}
class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(decoration: InputDecoration(filled: true)),
SizedBox(height: 10),
TextField(decoration: InputDecoration(filled: true)),
RaisedButton(
child: Text('Click to login'),
onPressed: () => Get.find<AuthController>().login()),
],
),
),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
AuthController c = Get.find();
return RaisedButton(
child: Text('Hi ${c.user}, click to logout'),
onPressed: () => c.logout());
}
}
I finished. If I put each class in a separate file, and divided the login module from the home module, from splashscreen we would have a well structured project here, much clearer and easier to understand, and with much more resource savings.
Lovely! I'm going to try this tonight. Thank you so much!
How would I do phone calls with Get and an API and..and..I'll get you to write my app :)
These guys could benefit from Get:
These guys could benefit from Get: https://pattle.dev/pattle/app/-/merge_requests/26
Surely they could benefit from Get using noContext.
Well, as the example was given, I am closing this.
@jonataslaw you could make an __examples__ category on readme and put those little things there. It would be very helpful
@jonataslaw you could make an examples category on readme and put those little things there. It would be very helpful
I'm working on it.
Examples, internationalization, storage, http communication and websockets, etc.
Internationalization seems easy, but storage I still don't know if I'm going to leave it up to people to add a "path", because including this in the package will increase the size.
I can put this in another package as well, and create a simple interface in the main package and use extensions of that interface for the micropackages that will be added in version 3.0 which apparently will come out tomorrow.
The name will also become GetX (due to SEO complaints and Google searches).
I need opinions about the main version, if it will have all the features, if I keep a "core" version with only the main one, or what I do with version 3.0
How can I keep user logged in this example? Like save the isLogged in the cache?
I'm doing this for AppAuth/OAuth2 by saving my refresh token and access token using https://pub.dev/packages/flutter_secure_storage
If the refresh token is there and hasn't expired, I'm loggedin.
Do I need a splash screen here?
Do I need a splash screen here?
Nope. Working. Now to expand to make login form, tokens and secure storage etc.
Thanks!
Do I need a splash screen here?
Nope. Working. Now to expand to make login form, tokens and secure storage etc.
Thanks!
Actually, I do need something as my home screen flashes up before routing to my login screen.
Most helpful comment
Hi, the bloc documentation is fantastic, and very complete.
However login is one of the simplest things to do with Get (in fact, everything is simple to do with Get).
I will try to write in a couple of minutes a login and home screen without using StatefulWidget, with a few lines of code, and a lot of organization.
I finished. If I put each class in a separate file, and divided the login module from the home module, from splashscreen we would have a well structured project here, much clearer and easier to understand, and with much more resource savings.