Jonny Borges

fix controller remain when navigate to same route

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 -import 'dart:math';  
4 3
5 -void main() {  
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),  
38 - ),  
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 -} 4 +import 'app/routes/app_pages.dart';
  5 +import 'services/auth_service.dart';
58 6
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), 7 +void main() {
  8 + runApp(
  9 + GetMaterialApp.router(
  10 + title: "Application",
  11 + initialBinding: BindingsBuilder(
  12 + () {
  13 + Get.put(AuthService());
  14 + },
87 ), 15 ),
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 - } 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 + );
109 } 27 }