Created
June 16, 2020 08:24
-
-
Save stefandevo/65f818d9cf748d4b9eb7aed1d439fb0c to your computer and use it in GitHub Desktop.
Get Package - Login Flow
This file contains 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'; | |
import 'package:get/get.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
void main() { | |
WidgetsFlutterBinding.ensureInitialized(); | |
runApp(DemoApp()); | |
} | |
class DemoApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return GetMaterialApp( | |
title: 'Demo', | |
initialBinding: InitialBinding(), | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: Splash(), | |
); | |
} | |
} | |
class InitialBinding extends Bindings { | |
@override | |
void dependencies() async { | |
final prefs = await SharedPreferences.getInstance(); | |
Get.put<SharedPreferences>(prefs); | |
Get.put<ProfileController>(ProfileController()); | |
} | |
} | |
class Splash extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold(); | |
} | |
} | |
class ProfileController extends RxController { | |
static const profileKey = 'profileId'; | |
var isLoggedIn = false; | |
var user = ''; | |
static ProfileController get to => Get.find(); | |
@override | |
void onInit() { | |
final prefs = Get.find<SharedPreferences>(); | |
user = prefs.getString(profileKey); | |
isLoggedIn = (user != null); | |
} | |
@override | |
void onReady() { | |
checkLoggedInStatus(); | |
super.onReady(); | |
} | |
void checkLoggedInStatus() { | |
if (isLoggedIn) { | |
Get.to(HomePage()); | |
} else { | |
Get.to(SignupPage(), binding: SignupBinding()); | |
} | |
} | |
void signup() { | |
final prefs = Get.find<SharedPreferences>(); | |
user = 'John Doe'; | |
prefs.setString(profileKey, user); | |
isLoggedIn = true; | |
checkLoggedInStatus(); | |
} | |
void logout() { | |
final prefs = Get.find<SharedPreferences>(); | |
prefs.remove(profileKey); | |
isLoggedIn = false; | |
checkLoggedInStatus(); | |
} | |
} | |
class SignupBinding extends Bindings { | |
@override | |
void dependencies() { | |
Get.put<SignupController>(SignupController()); | |
} | |
} | |
class SignupController extends RxController { | |
void signup() { | |
final profileController = Get.find<ProfileController>(); | |
profileController.signup(); | |
} | |
} | |
class SignupPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final controller = Get.find<SignupController>(); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Signup'), | |
), | |
body: Center( | |
child: Column( | |
children: <Widget>[ | |
RaisedButton( | |
child: Text("Do Signup"), | |
onPressed: () => controller.signup(), | |
) | |
], | |
), | |
), | |
); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Home'), | |
), | |
body: Center( | |
child: Column( | |
children: <Widget>[ | |
Container( | |
child: Text('Welcome ' + ProfileController.to.user), | |
), | |
RaisedButton( | |
child: Text("Logout"), | |
onPressed: () => ProfileController.to.logout(), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment