Showing
3 changed files
with
139 additions
and
22 deletions
| 1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; | 
| 2 | import 'package:get/get.dart'; | 2 | import 'package:get/get.dart'; | 
| 3 | - | ||
| 4 | -import 'app/routes/app_pages.dart'; | ||
| 5 | -import 'services/auth_service.dart'; | 3 | +import 'dart:math'; | 
| 6 | 4 | ||
| 7 | void main() { | 5 | void main() { | 
| 8 | - runApp( | ||
| 9 | - GetMaterialApp.router( | ||
| 10 | - title: "Application", | ||
| 11 | - initialBinding: BindingsBuilder( | ||
| 12 | - () { | ||
| 13 | - Get.put(AuthService()); | ||
| 14 | - }, | 6 | + runApp(GetMaterialApp( | 
| 7 | + initialRoute: '/splash', | ||
| 8 | + getPages: [ | ||
| 9 | + GetPage( | ||
| 10 | + name: '/splash', | ||
| 11 | + page: () => SplashPage(), | ||
| 12 | + binding: SplashBinding(), | ||
| 13 | + ), | ||
| 14 | + GetPage( | ||
| 15 | + name: '/login', | ||
| 16 | + page: () => LoginPage(), | ||
| 17 | + binding: LoginBinding(), | ||
| 18 | + ), | ||
| 19 | + ], | ||
| 20 | + )); | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +class SplashPage extends GetView<SplashController> { | ||
| 24 | + @override | ||
| 25 | + Widget build(BuildContext context) { | ||
| 26 | + return Scaffold( | ||
| 27 | + appBar: AppBar(title: Text('Splash')), | ||
| 28 | + body: Center( | ||
| 29 | + child: RaisedButton( | ||
| 30 | + onPressed: () => Get.offNamed('/login'), | ||
| 31 | + child: Obx(() => Text( | ||
| 32 | + 'Login ${controller.service.title} >>> ${controller.service.counter}')), | ||
| 33 | + ), | ||
| 34 | + ), | ||
| 35 | + floatingActionButton: FloatingActionButton( | ||
| 36 | + onPressed: controller.service.increment, | ||
| 37 | + child: Icon(Icons.add), | ||
| 15 | ), | 38 | ), | 
| 16 | - getPages: AppPages.routes, | ||
| 17 | - // routeInformationParser: GetInformationParser( | ||
| 18 | - // // initialRoute: Routes.HOME, | ||
| 19 | - // ), | ||
| 20 | - // routerDelegate: GetDelegate( | ||
| 21 | - // backButtonPopMode: PopMode.History, | ||
| 22 | - // preventDuplicateHandlingMode: | ||
| 23 | - // PreventDuplicateHandlingMode.ReorderRoutes, | ||
| 24 | - // ), | ||
| 25 | - ), | ||
| 26 | - ); | 39 | + ); | 
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +class SplashBinding extends Bindings { | ||
| 44 | + @override | ||
| 45 | + void dependencies() { | ||
| 46 | + Get.lazyPut(() => ServiceController()); // or lazyPut | ||
| 47 | + Get.lazyPut(() => SplashController(service: Get.find())); | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +class SplashController extends GetxController { | ||
| 52 | + // final service = Get.find<ServiceController>(); | ||
| 53 | + final ServiceController service; | ||
| 54 | + SplashController({ | ||
| 55 | + required this.service, | ||
| 56 | + }); | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +class LoginBinding extends Bindings { | ||
| 60 | + @override | ||
| 61 | + void dependencies() { | ||
| 62 | + Get.lazyPut(() => ServiceController()); // or lazyPut | ||
| 63 | + Get.lazyPut(() => LoginController(service: Get.find())); | ||
| 64 | + } | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +class LoginController extends GetxController { | ||
| 68 | + // final service = Get.find<ServiceController>(); | ||
| 69 | + final ServiceController service; | ||
| 70 | + LoginController({ | ||
| 71 | + required this.service, | ||
| 72 | + }); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +class LoginPage extends GetView<LoginController> { | ||
| 76 | + @override | ||
| 77 | + Widget build(BuildContext context) { | ||
| 78 | + return Scaffold( | ||
| 79 | + appBar: AppBar(title: Text('Login')), | ||
| 80 | + body: Center( | ||
| 81 | + child: Obx(() => Text( | ||
| 82 | + 'Login ${controller.service.title} >>> ${controller.service.counter}')), | ||
| 83 | + ), | ||
| 84 | + floatingActionButton: FloatingActionButton( | ||
| 85 | + onPressed: controller.service.increment, | ||
| 86 | + child: Icon(Icons.add), | ||
| 87 | + ), | ||
| 88 | + ); | ||
| 89 | + } | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +class ServiceController extends GetxController { | ||
| 93 | + final title = Random().nextInt(99999).toString(); | ||
| 94 | + final counter = 0.obs; | ||
| 95 | + | ||
| 96 | + increment() => counter.value++; | ||
| 97 | + | ||
| 98 | + @override | ||
| 99 | + void onInit() { | ||
| 100 | + print('onInit $counter'); | ||
| 101 | + super.onInit(); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @override | ||
| 105 | + void onClose() { | ||
| 106 | + print('onClose $counter'); | ||
| 107 | + super.onClose(); | ||
| 108 | + } | ||
| 27 | } | 109 | } | 
| @@ -214,11 +214,37 @@ class GetInstance { | @@ -214,11 +214,37 @@ class GetInstance { | ||
| 214 | 214 | ||
| 215 | for (final element in keysToRemove) { | 215 | for (final element in keysToRemove) { | 
| 216 | delete(key: element); | 216 | delete(key: element); | 
| 217 | + _routesKey.remove(element); | ||
| 218 | + } | ||
| 219 | + | ||
| 220 | + keysToRemove.clear(); | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + void reloadDependencyByRoute(String routeName) { | ||
| 224 | + final keysToRemove = <String>[]; | ||
| 225 | + _routesKey.forEach((key, value) { | ||
| 226 | + if (value == routeName) { | ||
| 227 | + keysToRemove.add(key); | ||
| 228 | + } | ||
| 229 | + }); | ||
| 230 | + | ||
| 231 | + /// Removes [Get.create()] instances registered in [routeName]. | ||
| 232 | + if (_routesByCreate.containsKey(routeName)) { | ||
| 233 | + for (final onClose in _routesByCreate[routeName]!) { | ||
| 234 | + // assure the [DisposableInterface] instance holding a reference | ||
| 235 | + // to [onClose()] wasn't disposed. | ||
| 236 | + onClose(); | ||
| 237 | + } | ||
| 238 | + _routesByCreate[routeName]!.clear(); | ||
| 239 | + _routesByCreate.remove(routeName); | ||
| 217 | } | 240 | } | 
| 218 | 241 | ||
| 219 | for (final element in keysToRemove) { | 242 | for (final element in keysToRemove) { | 
| 220 | - _routesKey.remove(element); | 243 | + print('reload $element'); | 
| 244 | + reload(key: element); | ||
| 245 | + //_routesKey.remove(element); | ||
| 221 | } | 246 | } | 
| 247 | + | ||
| 222 | keysToRemove.clear(); | 248 | keysToRemove.clear(); | 
| 223 | } | 249 | } | 
| 224 | 250 | 
| 1 | import 'package:flutter/widgets.dart'; | 1 | import 'package:flutter/widgets.dart'; | 
| 2 | +import 'package:get/instance_manager.dart'; | ||
| 2 | import '../../../../get_core/get_core.dart'; | 3 | import '../../../../get_core/get_core.dart'; | 
| 3 | import '../../../get_navigation.dart'; | 4 | import '../../../get_navigation.dart'; | 
| 4 | import '../../dialog/dialog_route.dart'; | 5 | import '../../dialog/dialog_route.dart'; | 
| @@ -193,6 +194,10 @@ class GetObserver extends NavigatorObserver { | @@ -193,6 +194,10 @@ class GetObserver extends NavigatorObserver { | ||
| 193 | currentRoute.isBottomSheet ? false : value.isBottomSheet; | 194 | currentRoute.isBottomSheet ? false : value.isBottomSheet; | 
| 194 | value.isDialog = currentRoute.isDialog ? false : value.isDialog; | 195 | value.isDialog = currentRoute.isDialog ? false : value.isDialog; | 
| 195 | }); | 196 | }); | 
| 197 | + if (oldRoute is GetPageRoute) { | ||
| 198 | + print(oldRoute.reference); | ||
| 199 | + GetInstance().reloadDependencyByRoute(oldRoute.reference); | ||
| 200 | + } | ||
| 196 | 201 | ||
| 197 | routing?.call(_routeSend); | 202 | routing?.call(_routeSend); | 
| 198 | } | 203 | } | 
| @@ -216,6 +221,10 @@ class GetObserver extends NavigatorObserver { | @@ -216,6 +221,10 @@ class GetObserver extends NavigatorObserver { | ||
| 216 | value.isDialog = currentRoute.isDialog ? false : value.isDialog; | 221 | value.isDialog = currentRoute.isDialog ? false : value.isDialog; | 
| 217 | }); | 222 | }); | 
| 218 | 223 | ||
| 224 | + if (route is GetPageRoute) { | ||
| 225 | + print(route.reference); | ||
| 226 | + GetInstance().reloadDependencyByRoute(route.reference); | ||
| 227 | + } | ||
| 219 | routing?.call(_routeSend); | 228 | routing?.call(_routeSend); | 
| 220 | } | 229 | } | 
| 221 | } | 230 | } | 
- 
Please register or login to post a comment