Last active
June 16, 2020 08:29
-
-
Save stefandevo/3e14e38c449da182d0b79e4551414b49 to your computer and use it in GitHub Desktop.
demo2.dart
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.off(HomePage()); | |
} else { | |
Get.off(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
Only difference with first demo is
This would force to remove all underlying pages and make that the main page. So Splash -> SignupPage (should remove Splash) -> HomePage (should remove SignupPage).
But this fails: