Last active
September 24, 2021 17:19
-
-
Save manikantag/18a0cab5ea2423e321aae62e50a3e00d to your computer and use it in GitHub Desktop.
Revisions
-
manikantag renamed this gist
Sep 24, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
manikantag revised this gist
Sep 24, 2021 . 1 changed file with 222 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,222 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); runApp(GetMaterialApp.router( title: 'Test', initialBinding: BindingsBuilder.put(() => _AuthService(), permanent: true), getPages: [ GetPage( name: '/', page: () => const _LoginScreen(), binding: BindingsBuilder.put(() => _LoginController()), ), GetPage( name: '/teacher', page: () => const _TeacherHomeScreen(), binding: BindingsBuilder.put(() => _TeacherHomeController()), ), GetPage( name: '/student', page: () => const _StudentHomeScreen(), binding: BindingsBuilder.put(() => _StudentHomeController()), ), ], )); // Set overlay style status bar. It must run after MyApp(), because MaterialApp may override it. SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle( statusBarColor: Colors.transparent, systemNavigationBarColor: Colors.white); SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle); } // ----- Auth service ----- class _AuthService extends GetxController { // Rx<bool> isAuthenticated = false.obs; Rx<_AppUser> currentUser = Rx(_AppUser.dummy()); void doTeacherLogin() async { // isAuthenticated.value = true; currentUser = await Future.delayed( const Duration(milliseconds: 200), () => _Teacher.newTeacher('Teacher 1', 'Teacher 1 mobile').obs, ); /* currentUser.value = await Future.delayed( const Duration(milliseconds: 10), () => Teacher.newTeacher('Teacher 1', 'Teacher 1 mobile'), ); */ // ignore: avoid_print print(currentUser); Get.rootDelegate.offNamed('/teacher'); } void doStudentLogin() async { // isAuthenticated.value = true; currentUser = await Future.delayed( const Duration(milliseconds: 200), () => _Student.newStudent('Student 1', 'Student 1 mobile').obs, ); /* currentUser.value = await Future.delayed( const Duration(milliseconds: 10), () => _Student.newStudent('Student 1', 'Student 1 mobile'), ); */ // ignore: avoid_print print(currentUser); Get.rootDelegate.offNamed('/student'); } void doLogout() { // isAuthenticated.value = false; // currentUser(_AppUser.dummy()); currentUser = _AppUser.dummy().obs; // Get.reset(); // Get.reloadAll(force: true); // Get.reload<AuthService>(); // Get.deleteAll(force: true); Get.rootDelegate.offNamed('/'); } } // ----- Login controller & page ----- class _LoginController extends GetxController { final authService = Get.find<_AuthService>(); // bool isLoggedin() => authService.isAuthenticated.value; } class _LoginScreen extends GetView<_LoginController> { const _LoginScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // Obx(() => // controller.isLoggedin() ? const Text('Logged in') : const Text('Not logged in')), // Obx(() => Text('currentUser: ${controller.authService.currentUser.value}')), ElevatedButton( onPressed: controller.authService.doTeacherLogin, child: const Text('Teacher Login'), ), ElevatedButton( onPressed: controller.authService.doStudentLogin, child: const Text('Student Login'), ), /* ElevatedButton( onPressed: controller.authService.doLogout, child: const Text('Logout'), ), */ ], ), ), ); } } class _TeacherHomeController extends GetxController { final authService = Get.find<_AuthService>(); final Rx<_Teacher> currentUser = (Get.find<_AuthService>().currentUser.value as _Teacher).obs; } class _TeacherHomeScreen extends GetView<_TeacherHomeController> { const _TeacherHomeScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Welcom, Teacher'), ), body: Container( color: Colors.lightGreen.shade200, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Obx(() => Text('currentUser: ${controller.currentUser.value}')), ElevatedButton( onPressed: controller.authService.doLogout, child: const Text('Logout'), ), ], ), ), ), ); } } class _StudentHomeController extends GetxController { final authService = Get.find<_AuthService>(); final Rx<_Student> currentUser = (Get.find<_AuthService>().currentUser.value as _Student).obs; } class _StudentHomeScreen extends GetView<_StudentHomeController> { const _StudentHomeScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Welcom, Student'), ), body: Container( color: Colors.lightBlue.shade200, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Obx(() => Text('currentUser: ${controller.currentUser.value}')), ElevatedButton( onPressed: controller.authService.doLogout, child: const Text('Logout'), ), ], ), ), ), ); } } // ----- models ----- class _AppUser { final String uid; final String mobile; int count = 0; _AppUser({required this.uid, required this.mobile}); _AppUser.dummy() : this(uid: 'dummy', mobile: ''); @override String toString() { return {'mobile': mobile, 'uid': uid, 'counter': count}.toString(); } } class _Teacher extends _AppUser { final String? regId; _Teacher({required String uid, required String mobile, this.regId}) : super(uid: uid, mobile: mobile); _Teacher.newTeacher(String uid, String mobile) : this(mobile: mobile, uid: uid); } class _Student extends _AppUser { final String? marks; _Student({required String uid, required String mobile, this.marks}) : super(uid: uid, mobile: mobile); _Student.newStudent(String uid, String mobile) : this(uid: uid, mobile: mobile); } -
manikantag renamed this gist
Sep 16, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
manikantag revised this gist
Sep 16, 2021 . 1 changed file with 25 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -22,6 +22,7 @@ void main() async { )); } // ----- models ----- class AppUser { final String uid; final String mobile; @@ -39,23 +40,22 @@ class AppUser { } } class Teacher extends AppUser { final String? regId; Teacher({required String uid, required String mobile, this.regId}) : super(uid: uid, mobile: mobile); Teacher.newTeacher(String uid, String mobile) : this(mobile: mobile, uid: uid); } class Student extends AppUser { final String? marks; Student({required String uid, required String mobile, this.marks}) : super(uid: uid, mobile: mobile); Student.newStudent(String uid, String mobile) : this(uid: uid, mobile: mobile); } // ----- Auth service ----- @@ -66,35 +66,34 @@ class AuthService extends GetxController { /* @override void onInit() { currentUser = Student.newStudent('Student 1', 'Student 1 mobile').obs; // This is working super.onInit(); } */ void doStudentLogin() async { isAuthenticated.value = true; // currentUser = Student.newStudent('Student 1', 'Student 1 mobile').obs; // This is working currentUser = await Future.delayed( const Duration(milliseconds: 10), () => Student.newStudent('Student 1', 'Student 1 mobile').obs, // This is NOT working ); print(currentUser); } void doTeacherLogin() async { isAuthenticated.value = true; // currentUser = Teacher.newTeacher('Teacher 1', 'Teacher 1 mobile').obs; // This is working currentUser = await Future.delayed( const Duration(milliseconds: 10), () => Teacher.newTeacher('Teacher 1', 'Teacher 1 mobile').obs, // This is NOT working ); print(currentUser); } void doLogout() { isAuthenticated.value = false; authCounter = 0.obs; // currentUser = Teacher.newTeacher('Teacher 1', 'Teacher 1 mobile').obs; // This is working Get.rootDelegate.offNamed('/'); // Get.reset(); // Get.reloadAll(force: true); @@ -109,13 +108,13 @@ class LoginController extends GetxController { bool isLoggedin() => authService.isAuthenticated.value == true; void doTeacherLogin() { authService.doTeacherLogin(); Get.rootDelegate.offNamed('/home'); } void doStudentLogin() { authService.doStudentLogin(); Get.rootDelegate.offNamed('/home'); } } @@ -135,12 +134,12 @@ class LoginScreen extends GetView<LoginController> { children: [ Text('authCounter: ${controller.authService.authCounter.value}'), ElevatedButton( onPressed: controller.doTeacherLogin, child: const Text('Teacher Login'), ), ElevatedButton( onPressed: controller.doStudentLogin, child: const Text('Student Login'), ), ], ), @@ -188,7 +187,7 @@ class HomeScreen extends GetView<HomeController> { Text('Auth counter: ${controller.authCounter.value}'), Text('Home counter: ${controller.homeCounter.value}'), Text( 'Current User: ${controller.currentUser.value is Teacher ? 'Teacher' : 'Student'}'), Text('Current User: ${controller.currentUser.value}'), ElevatedButton( onPressed: controller.increment, -
manikantag created this gist
Sep 16, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,207 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); runApp(GetMaterialApp.router( title: 'Test', initialBinding: BindingsBuilder.put(() => AuthService(), permanent: true), getPages: [ GetPage( name: '/', page: () => const LoginScreen(), binding: BindingsBuilder.put(() => LoginController()), ), GetPage( name: '/home', page: () => const HomeScreen(), binding: BindingsBuilder.put(() => HomeController()), ), ], )); } class AppUser { final String uid; final String mobile; int count = 0; AppUser({required this.uid, required this.mobile}); AppUser.newUser(String uid, String mobile) : this(uid: uid, mobile: mobile); AppUser.dummyUser() : this(uid: '', mobile: ''); @override String toString() { return {'mobile': mobile, 'uid': uid, 'counter': count}.toString(); } } // ----- models ----- class Doctor extends AppUser { final String? regId; Doctor({required String uid, required String mobile, this.regId}) : super(uid: uid, mobile: mobile); Doctor.newDoctor(String uid, String mobile) : this(mobile: mobile, uid: uid); } class Patient extends AppUser { final String? bmi; Patient({required String uid, required String mobile, this.bmi}) : super(uid: uid, mobile: mobile); Patient.newPatient(String uid, String mobile) : this(uid: uid, mobile: mobile); } // ----- Auth service ----- class AuthService extends GetxController { Rx<bool> isAuthenticated = false.obs; Rx<int> authCounter = 0.obs; Rx<AppUser> currentUser = Rx(AppUser.dummyUser()); /* @override void onInit() { currentUser = Patient.newPatient('patient1', 'patientm1').toRx(); super.onInit(); } */ void doPatientLogin() async { isAuthenticated.value = true; // currentUser = Patient.newPatient('Patient_1', 'Patient_mobile_1').toRx(); currentUser = await Future.delayed( const Duration(milliseconds: 10), () => Patient.newPatient('Patient_1', 'Patient_mobile_1').obs, ); print(currentUser); } void doDoctorLogin() async { isAuthenticated.value = true; // currentUser = Doctor.newDoctor('Doc_1', 'Doc_mobile_1').toRx(); currentUser = await Future.delayed( const Duration(milliseconds: 10), () => Doctor.newDoctor('Doc_1', 'Doc_mobile_1').obs, ); print(currentUser); } void doLogout() { isAuthenticated.value = false; authCounter = 0.obs; // tempCounter = Patient.newPatient('p2', 'm2').toRx(); // currentUser = Doctor.newDoctor('doctor1', 'docm1').toRx(); Get.rootDelegate.offNamed('/'); // Get.reset(); // Get.reloadAll(force: true); // Get.reload<AuthService>(); // Get.deleteAll(force: true); } } // ----- Login controller & page ----- class LoginController extends GetxController { final authService = Get.find<AuthService>(); bool isLoggedin() => authService.isAuthenticated.value == true; void doDoctorLogin() { authService.doDoctorLogin(); Get.rootDelegate.offNamed('/home'); } void doPatientLogin() { authService.doPatientLogin(); Get.rootDelegate.offNamed('/home'); } } class LoginScreen extends GetView<LoginController> { const LoginScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Obx( () => controller.isLoggedin() ? const Text('Already logged in -> Logout (state not cleared)') : Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('authCounter: ${controller.authService.authCounter.value}'), ElevatedButton( onPressed: controller.doDoctorLogin, child: const Text('Doctor Login'), ), ElevatedButton( onPressed: controller.doPatientLogin, child: const Text('Patient Login'), ), ], ), ), ), ); } } // ----- Home controller & page ----- class HomeController extends GetxController { final AuthService _authService = Get.find<AuthService>(); final Rx<int> homeCounter = 0.obs; late final Rx<int> authCounter; late final Rx<AppUser> currentUser; @override void onInit() { authCounter = _authService.authCounter; currentUser = _authService.currentUser; super.onInit(); } void increment() { homeCounter.value++; authCounter.value++; currentUser.value.count++; } void logout() => _authService.doLogout(); } class HomeScreen extends GetView<HomeController> { const HomeScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( body: Obx( () => Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Auth counter: ${controller.authCounter.value}'), Text('Home counter: ${controller.homeCounter.value}'), Text( 'Current User: ${controller.currentUser.value is Doctor ? 'Doctor' : 'Patient'}'), Text('Current User: ${controller.currentUser.value}'), ElevatedButton( onPressed: controller.increment, child: const Text('Increment counters'), ), ElevatedButton( onPressed: controller.logout, child: const Text('Logout'), ), ], ), ), ), ); } }