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
Schaban
2020-11-28 22:08:45 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f13266f629a0c07034ed22b22c17a1b4629d6084
f13266f6
1 parent
85945773
Docs
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
41 deletions
lib/get_navigation/src/routes/default_route.dart
lib/get_navigation/src/routes/get_route.dart
lib/get_navigation/src/routes/route_middleware.dart
test/navigation/middleware_test.dart
lib/get_navigation/src/routes/default_route.dart
View file @
f13266f
...
...
@@ -11,27 +11,27 @@ import 'default_transitions.dart';
import
'transitions_type.dart'
;
class
GetPageRoute
<
T
>
extends
PageRoute
<
T
>
{
GetPageRoute
({
RouteSettings
settings
,
this
.
transitionDuration
=
const
Duration
(
milliseconds:
300
),
this
.
opaque
=
true
,
this
.
parameter
,
this
.
curve
,
this
.
alignment
,
this
.
transition
,
this
.
popGesture
,
this
.
customTransition
,
this
.
barrierDismissible
=
false
,
this
.
barrierColor
,
this
.
binding
,
this
.
bindings
,
this
.
routeName
,
this
.
page
,
this
.
barrierLabel
,
this
.
maintainState
=
true
,
bool
fullscreenDialog
=
false
,
this
.
middlewares
})
:
assert
(
opaque
!=
null
),
GetPageRoute
(
{
RouteSettings
settings
,
this
.
transitionDuration
=
const
Duration
(
milliseconds:
300
),
this
.
opaque
=
true
,
this
.
parameter
,
this
.
curve
,
this
.
alignment
,
this
.
transition
,
this
.
popGesture
,
this
.
customTransition
,
this
.
barrierDismissible
=
false
,
this
.
barrierColor
,
this
.
binding
,
this
.
bindings
,
this
.
routeName
,
this
.
page
,
this
.
barrierLabel
,
this
.
maintainState
=
true
,
bool
fullscreenDialog
=
false
,
this
.
middlewares
})
:
assert
(
opaque
!=
null
),
assert
(
barrierDismissible
!=
null
),
assert
(
maintainState
!=
null
),
assert
(
fullscreenDialog
!=
null
),
...
...
@@ -65,7 +65,7 @@ class GetPageRoute<T> extends PageRoute<T> {
final
Curve
curve
;
final
Alignment
alignment
;
final
List
<
GetMiddleware
>
middlewares
;
@override
...
...
@@ -123,8 +123,8 @@ class GetPageRoute<T> extends PageRoute<T> {
binding
.
dependencies
();
}
}
final
pageToBuild
=
middlewareRunner
.
runOnPageBuildStart
(
page
);
final
pageToBuild
=
middlewareRunner
.
runOnPageBuildStart
(
page
);
return
middlewareRunner
.
runOnPageBuilt
(
pageToBuild
());
}
...
...
@@ -383,6 +383,8 @@ class GetPageRoute<T> extends PageRoute<T> {
WidgetsBinding
.
instance
.
addPostFrameCallback
((
_
)
=>
GetInstance
()
.
removeDependencyByRoute
(
"
${settings?.name ?? routeName}
"
));
}
final
middlewareRunner
=
MiddlewareRunner
(
middlewares
);
middlewareRunner
.
runOnPageDispose
();
super
.
dispose
();
}
}
...
...
lib/get_navigation/src/routes/get_route.dart
View file @
f13266f
...
...
@@ -48,4 +48,46 @@ class GetPage {
assert
(
name
!=
null
),
assert
(
maintainState
!=
null
),
assert
(
fullscreenDialog
!=
null
);
GetPage
copyWith
({
String
name
,
GetPageBuilder
page
,
bool
popGesture
,
Map
<
String
,
String
>
parameter
,
String
title
,
Transition
transition
,
Curve
curve
,
Alignment
alignment
,
bool
maintainState
,
bool
opaque
,
Bindings
binding
,
List
<
Bindings
>
bindings
,
CustomTransition
customTransition
,
Duration
transitionDuration
,
bool
fullscreenDialog
,
RouteSettings
settings
,
List
<
GetPage
>
children
,
List
<
GetMiddleware
>
middlewares
,
})
{
return
GetPage
(
name:
name
??
this
.
name
,
page:
page
??
this
.
page
,
popGesture:
popGesture
??
this
.
popGesture
,
parameter:
parameter
??
this
.
parameter
,
title:
title
??
this
.
title
,
transition:
transition
??
this
.
transition
,
curve:
curve
??
this
.
curve
,
alignment:
alignment
??
this
.
alignment
,
maintainState:
maintainState
??
this
.
maintainState
,
opaque:
opaque
??
this
.
opaque
,
binding:
binding
??
this
.
binding
,
bindings:
bindings
??
this
.
bindings
,
customTransition:
customTransition
??
this
.
customTransition
,
transitionDuration:
transitionDuration
??
this
.
transitionDuration
,
fullscreenDialog:
fullscreenDialog
??
this
.
fullscreenDialog
,
settings:
settings
??
this
.
settings
,
children:
children
??
this
.
children
,
middlewares:
middlewares
??
this
.
middlewares
,
);
}
}
...
...
lib/get_navigation/src/routes/route_middleware.dart
View file @
f13266f
...
...
@@ -18,17 +18,19 @@ abstract class _RouteMiddleware {
/// {@end-tool}
int
priority
;
/// This function will be the first thing to call when this Page is called
/// you can use it to redirect befor anything in this page happend.
/// This function will be called when the page of
/// the called route is being searched for.
/// It take RouteSettings as a result an redirect to the new settings or
/// give it null and there will be no redirecting.
/// {@tool snippet}
/// ```dart
/// GetPage redirect(
) {
/// GetPage redirect(
String route
) {
/// final authService = Get.find<AuthService>();
/// return authService.
isAuthed ? null : '/login'
;
/// return authService.
authed.value ? null : RouteSettings(name: '/login')
;
/// }
/// ```
/// {@end-tool}
RouteSettings
redirect
();
RouteSettings
redirect
(
String
route
);
/// This function will be called when this Page is called
/// you can use it to change something about the page or give it new page
...
...
@@ -36,8 +38,7 @@ abstract class _RouteMiddleware {
/// ```dart
/// GetPage onPageCalled(GetPage page) {
/// final authService = Get.find<AuthService>();
/// page.title = 'Wellcome ${authService.UserName}';
/// return page;
/// return page.copyWith(title: 'Wellcome ${authService.UserName}');
/// }
/// ```
/// {@end-tool}
...
...
@@ -45,13 +46,25 @@ abstract class _RouteMiddleware {
/// This function will be called right before the [Bindings] are initialize.
/// Here you can change [Bindings] for this page
/// {@tool snippet}
/// ```dart
/// List<Bindings> onBindingsStart(List<Bindings> bindings) {
/// final authService = Get.find<AuthService>();
/// if (authService.isAdmin) {
/// bindings.add(AdminBinding());
/// }
/// return bindings;
/// }
/// ```
/// {@end-tool}
List
<
Bindings
>
onBindingsStart
(
List
<
Bindings
>
bindings
);
/// This function will be called right after the [Bindings] are initialize.
/// Here you can change the Page to build
GetPageBuilder
onPageBuildStart
(
GetPageBuilder
page
);
// Get the built page
/// This function will be called right after the
/// GetPage.page function is called and will give you the result
/// of the function. and take the widget that will be showed.
Widget
onPageBuilt
(
Widget
page
);
void
onPageDispose
();
...
...
@@ -59,8 +72,8 @@ abstract class _RouteMiddleware {
/// The Page Middlewares.
/// The Functions will be called in this order
/// (( [redirect] -> [onBindingsStart] -> [onPageBuildStart] ->
/// [onPageBuilt] -> [onPageDispose] ))
/// (( [redirect] -> [onPageCalled] -> [onBindingsStart] ->
/// [onPageBuildStart] -> [onPageBuilt] -> [onPageDispose] ))
class
GetMiddleware
implements
_RouteMiddleware
{
@override
int
priority
=
0
;
...
...
@@ -68,7 +81,7 @@ class GetMiddleware implements _RouteMiddleware {
GetMiddleware
({
this
.
priority
});
@override
RouteSettings
redirect
()
=>
null
;
RouteSettings
redirect
(
String
route
)
=>
null
;
@override
GetPage
onPageCalled
(
GetPage
page
)
=>
page
;
...
...
@@ -106,10 +119,10 @@ class MiddlewareRunner {
return
page
;
}
RouteSettings
runRedirect
()
{
RouteSettings
runRedirect
(
String
route
)
{
RouteSettings
to
;
_getMiddlewares
().
forEach
((
element
)
{
to
=
element
.
redirect
();
to
=
element
.
redirect
(
route
);
});
if
(
to
!=
null
)
{
Get
.
log
(
'Redirect to
$to
'
);
...
...
@@ -208,7 +221,7 @@ class PageRedirect {
if
(
match
.
route
.
middlewares
==
null
||
match
.
route
.
middlewares
.
isEmpty
)
{
return
false
;
}
final
newSettings
=
runner
.
runRedirect
();
final
newSettings
=
runner
.
runRedirect
(
settings
.
name
);
if
(
newSettings
==
null
)
{
return
false
;
}
...
...
test/navigation/middleware_test.dart
View file @
f13266f
...
...
@@ -6,7 +6,7 @@ import 'get_main_test.dart';
class
RedirectMiddleware
extends
GetMiddleware
{
@override
RouteSettings
redirect
()
=>
RouteSettings
(
name:
'/second'
);
RouteSettings
redirect
(
String
route
)
=>
RouteSettings
(
name:
'/second'
);
}
main
()
{
...
...
@@ -33,4 +33,3 @@ main() {
expect
(
find
.
byType
(
SecondScreen
),
findsOneWidget
);
});
}
...
...
Please
register
or
login
to post a comment