Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
fluttertpc_get
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Jonny Borges
2021-07-20 06:00:00 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
008cb103c57486674440ff77cdd63df4a82d9663
008cb103
1 parent
4e296f74
fix controller remain when navigate to same route
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
139 additions
and
22 deletions
example_nav2/lib/main.dart
lib/get_instance/src/get_instance.dart
lib/get_navigation/src/routes/observers/route_observer.dart
example_nav2/lib/main.dart
View file @
008cb10
import
'package:flutter/material.dart'
;
import
'package:get/get.dart'
;
import
'app/routes/app_pages.dart'
;
import
'services/auth_service.dart'
;
import
'dart:math'
;
void
main
(
)
{
runApp
(
GetMaterialApp
.
router
(
title:
"Application"
,
initialBinding:
BindingsBuilder
(
()
{
Get
.
put
(
AuthService
());
},
runApp
(
GetMaterialApp
(
initialRoute:
'/splash'
,
getPages:
[
GetPage
(
name:
'/splash'
,
page:
()
=>
SplashPage
(),
binding:
SplashBinding
(),
),
GetPage
(
name:
'/login'
,
page:
()
=>
LoginPage
(),
binding:
LoginBinding
(),
),
],
));
}
class
SplashPage
extends
GetView
<
SplashController
>
{
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
appBar:
AppBar
(
title:
Text
(
'Splash'
)),
body:
Center
(
child:
RaisedButton
(
onPressed:
()
=>
Get
.
offNamed
(
'/login'
),
child:
Obx
(()
=>
Text
(
'Login
${controller.service.title}
>>>
${controller.service.counter}
'
)),
),
),
floatingActionButton:
FloatingActionButton
(
onPressed:
controller
.
service
.
increment
,
child:
Icon
(
Icons
.
add
),
),
getPages:
AppPages
.
routes
,
// routeInformationParser: GetInformationParser(
// // initialRoute: Routes.HOME,
// ),
// routerDelegate: GetDelegate(
// backButtonPopMode: PopMode.History,
// preventDuplicateHandlingMode:
// PreventDuplicateHandlingMode.ReorderRoutes,
// ),
),
);
);
}
}
class
SplashBinding
extends
Bindings
{
@override
void
dependencies
()
{
Get
.
lazyPut
(()
=>
ServiceController
());
// or lazyPut
Get
.
lazyPut
(()
=>
SplashController
(
service:
Get
.
find
()));
}
}
class
SplashController
extends
GetxController
{
// final service = Get.find<ServiceController>();
final
ServiceController
service
;
SplashController
({
required
this
.
service
,
});
}
class
LoginBinding
extends
Bindings
{
@override
void
dependencies
()
{
Get
.
lazyPut
(()
=>
ServiceController
());
// or lazyPut
Get
.
lazyPut
(()
=>
LoginController
(
service:
Get
.
find
()));
}
}
class
LoginController
extends
GetxController
{
// final service = Get.find<ServiceController>();
final
ServiceController
service
;
LoginController
({
required
this
.
service
,
});
}
class
LoginPage
extends
GetView
<
LoginController
>
{
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
appBar:
AppBar
(
title:
Text
(
'Login'
)),
body:
Center
(
child:
Obx
(()
=>
Text
(
'Login
${controller.service.title}
>>>
${controller.service.counter}
'
)),
),
floatingActionButton:
FloatingActionButton
(
onPressed:
controller
.
service
.
increment
,
child:
Icon
(
Icons
.
add
),
),
);
}
}
class
ServiceController
extends
GetxController
{
final
title
=
Random
().
nextInt
(
99999
).
toString
();
final
counter
=
0
.
obs
;
increment
()
=>
counter
.
value
++;
@override
void
onInit
()
{
print
(
'onInit
$counter
'
);
super
.
onInit
();
}
@override
void
onClose
()
{
print
(
'onClose
$counter
'
);
super
.
onClose
();
}
}
...
...
lib/get_instance/src/get_instance.dart
View file @
008cb10
...
...
@@ -214,11 +214,37 @@ class GetInstance {
for
(
final
element
in
keysToRemove
)
{
delete
(
key:
element
);
_routesKey
.
remove
(
element
);
}
keysToRemove
.
clear
();
}
void
reloadDependencyByRoute
(
String
routeName
)
{
final
keysToRemove
=
<
String
>[];
_routesKey
.
forEach
((
key
,
value
)
{
if
(
value
==
routeName
)
{
keysToRemove
.
add
(
key
);
}
});
/// Removes [Get.create()] instances registered in [routeName].
if
(
_routesByCreate
.
containsKey
(
routeName
))
{
for
(
final
onClose
in
_routesByCreate
[
routeName
]!)
{
// assure the [DisposableInterface] instance holding a reference
// to [onClose()] wasn't disposed.
onClose
();
}
_routesByCreate
[
routeName
]!.
clear
();
_routesByCreate
.
remove
(
routeName
);
}
for
(
final
element
in
keysToRemove
)
{
_routesKey
.
remove
(
element
);
print
(
'reload
$element
'
);
reload
(
key:
element
);
//_routesKey.remove(element);
}
keysToRemove
.
clear
();
}
...
...
lib/get_navigation/src/routes/observers/route_observer.dart
View file @
008cb10
import
'package:flutter/widgets.dart'
;
import
'package:get/instance_manager.dart'
;
import
'../../../../get_core/get_core.dart'
;
import
'../../../get_navigation.dart'
;
import
'../../dialog/dialog_route.dart'
;
...
...
@@ -193,6 +194,10 @@ class GetObserver extends NavigatorObserver {
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isDialog
=
currentRoute
.
isDialog
?
false
:
value
.
isDialog
;
});
if
(
oldRoute
is
GetPageRoute
)
{
print
(
oldRoute
.
reference
);
GetInstance
().
reloadDependencyByRoute
(
oldRoute
.
reference
);
}
routing
?.
call
(
_routeSend
);
}
...
...
@@ -216,6 +221,10 @@ class GetObserver extends NavigatorObserver {
value
.
isDialog
=
currentRoute
.
isDialog
?
false
:
value
.
isDialog
;
});
if
(
route
is
GetPageRoute
)
{
print
(
route
.
reference
);
GetInstance
().
reloadDependencyByRoute
(
route
.
reference
);
}
routing
?.
call
(
_routeSend
);
}
}
...
...
Please
register
or
login
to post a comment