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-27 17:16:09 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
2e66ddede35b0bf11f5e619a524175c93b57ea83
2e66dded
1 parent
07722a06
WIP
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
121 additions
and
108 deletions
lib/get_navigation/src/root/get_cupertino_app.dart
lib/get_navigation/src/root/get_material_app.dart
lib/get_navigation/src/routes/observers/route_observer.dart
lib/get_navigation/src/routes/route_middleware.dart
test/navigation/middleware_test.dart
lib/get_navigation/src/root/get_cupertino_app.dart
View file @
2e66dde
...
...
@@ -188,55 +188,7 @@ class GetCupertinoApp extends StatelessWidget {
super
(
key:
key
);
Route
<
dynamic
>
generator
(
RouteSettings
settings
)
{
var
match
=
Get
.
routeTree
.
matchRoute
(
settings
.
name
);
var
runner
=
MiddlewareRunner
(
match
.
route
.
middlewares
);
var
redirect
=
runner
.
runRedirect
();
while
(!
redirect
.
isNullOrBlank
)
{
match
=
Get
.
routeTree
.
matchRoute
(
redirect
);
runner
=
MiddlewareRunner
(
match
.
route
.
middlewares
);
redirect
=
runner
.
runRedirect
();
}
Get
.
parameters
=
match
?.
parameters
;
if
(
match
?.
route
==
null
)
{
return
GetPageRoute
(
page:
unknownRoute
.
page
,
parameter:
unknownRoute
.
parameter
,
settings:
RouteSettings
(
name:
settings
.
name
,
arguments:
settings
.
arguments
),
curve:
unknownRoute
.
curve
,
opaque:
unknownRoute
.
opaque
,
customTransition:
unknownRoute
.
customTransition
,
binding:
unknownRoute
.
binding
,
bindings:
unknownRoute
.
bindings
,
transitionDuration:
(
unknownRoute
.
transitionDuration
??
Get
.
defaultTransitionDuration
),
transition:
unknownRoute
.
transition
,
popGesture:
unknownRoute
.
popGesture
,
fullscreenDialog:
unknownRoute
.
fullscreenDialog
,
middlewares:
unknownRoute
.
middlewares
,
);
}
match
.
route
=
runner
.
runOnPageCalled
(
match
.
route
);
return
GetPageRoute
(
page:
match
.
route
.
page
,
parameter:
match
.
route
.
parameter
,
settings:
RouteSettings
(
name:
settings
.
name
,
arguments:
settings
.
arguments
),
curve:
match
.
route
.
curve
,
opaque:
match
.
route
.
opaque
,
customTransition:
match
.
route
.
customTransition
,
binding:
match
.
route
.
binding
,
bindings:
match
.
route
.
bindings
,
transitionDuration:
(
match
.
route
.
transitionDuration
??
Get
.
defaultTransitionDuration
),
transition:
match
.
route
.
transition
,
popGesture:
match
.
route
.
popGesture
,
fullscreenDialog:
match
.
route
.
fullscreenDialog
,
middlewares:
match
.
route
.
middlewares
,
);
return
PageRedirect
(
settings
,
unknownRoute
).
page
();
}
List
<
Route
<
dynamic
>>
initialRoutesGenerate
(
String
name
)
{
...
...
lib/get_navigation/src/root/get_material_app.dart
View file @
2e66dde
...
...
@@ -199,53 +199,7 @@ class GetMaterialApp extends StatelessWidget {
super
(
key:
key
);
Route
<
dynamic
>
generator
(
RouteSettings
settings
)
{
var
match
=
Get
.
routeTree
.
matchRoute
(
settings
.
name
);
final
redirect
=
MiddlewareRunner
(
match
.
route
.
middlewares
).
runRedirect
();
if
(!
redirect
.
isNullOrBlank
)
{
match
=
Get
.
routeTree
.
matchRoute
(
redirect
);
}
Get
.
parameters
=
match
?.
parameters
;
if
(
match
?.
route
==
null
)
{
return
GetPageRoute
(
page:
unknownRoute
.
page
,
parameter:
unknownRoute
.
parameter
,
settings:
RouteSettings
(
name:
settings
.
name
,
arguments:
settings
.
arguments
),
curve:
unknownRoute
.
curve
,
opaque:
unknownRoute
.
opaque
,
customTransition:
unknownRoute
.
customTransition
,
binding:
unknownRoute
.
binding
,
bindings:
unknownRoute
.
bindings
,
transitionDuration:
(
unknownRoute
.
transitionDuration
??
Get
.
defaultTransitionDuration
),
transition:
unknownRoute
.
transition
,
popGesture:
unknownRoute
.
popGesture
,
fullscreenDialog:
unknownRoute
.
fullscreenDialog
,
middlewares:
unknownRoute
.
middlewares
,
);
}
return
GetPageRoute
(
page:
match
.
route
.
page
,
parameter:
match
.
route
.
parameter
,
settings:
RouteSettings
(
name:
settings
.
name
,
arguments:
settings
.
arguments
),
curve:
match
.
route
.
curve
,
opaque:
match
.
route
.
opaque
,
customTransition:
match
.
route
.
customTransition
,
binding:
match
.
route
.
binding
,
bindings:
match
.
route
.
bindings
,
transitionDuration:
(
match
.
route
.
transitionDuration
??
Get
.
defaultTransitionDuration
),
transition:
match
.
route
.
transition
,
popGesture:
match
.
route
.
popGesture
,
fullscreenDialog:
match
.
route
.
fullscreenDialog
,
middlewares:
match
.
route
.
middlewares
);
return
PageRedirect
(
settings
,
unknownRoute
).
page
();
}
List
<
Route
<
dynamic
>>
initialRoutesGenerate
(
String
name
)
{
...
...
lib/get_navigation/src/routes/observers/route_observer.dart
View file @
2e66dde
...
...
@@ -115,7 +115,8 @@ class GetObserver extends NavigatorObserver {
value
.
removed
=
''
;
value
.
previous
=
_extractRouteName
(
previousRoute
)
??
''
;
value
.
isSnackbar
=
newRoute
.
isSnackbar
?
true
:
value
.
isSnackbar
??
false
;
value
.
isBottomSheet
=
newRoute
.
isBottomSheet
?
true
:
value
.
isBottomSheet
??
false
;
value
.
isBottomSheet
=
newRoute
.
isBottomSheet
?
true
:
value
.
isBottomSheet
??
false
;
value
.
isDialog
=
newRoute
.
isDialog
?
true
:
value
.
isDialog
??
false
;
});
...
...
@@ -154,7 +155,8 @@ class GetObserver extends NavigatorObserver {
value
.
removed
=
''
;
value
.
previous
=
newRoute
.
name
??
''
;
value
.
isSnackbar
=
currentRoute
.
isSnackbar
?
false
:
value
.
isSnackbar
;
value
.
isBottomSheet
=
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isBottomSheet
=
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isDialog
=
currentRoute
.
isDialog
?
false
:
value
.
isDialog
;
});
...
...
@@ -184,7 +186,8 @@ class GetObserver extends NavigatorObserver {
value
.
removed
=
''
;
value
.
previous
=
'
$oldName
'
;
value
.
isSnackbar
=
currentRoute
.
isSnackbar
?
false
:
value
.
isSnackbar
;
value
.
isBottomSheet
=
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isBottomSheet
=
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isDialog
=
currentRoute
.
isDialog
?
false
:
value
.
isDialog
;
});
...
...
@@ -198,14 +201,15 @@ class GetObserver extends NavigatorObserver {
final
currentRoute
=
_RouteData
.
ofRoute
(
route
);
Get
.
log
(
"REMOVING ROUTE
$routeName
"
);
_routeSend
?.
update
((
value
)
{
value
.
route
=
previousRoute
;
value
.
isBack
=
false
;
value
.
removed
=
routeName
??
''
;
value
.
previous
=
routeName
??
''
;
value
.
isSnackbar
=
currentRoute
.
isSnackbar
?
false
:
value
.
isSnackbar
;
value
.
isBottomSheet
=
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isBottomSheet
=
currentRoute
.
isBottomSheet
?
false
:
value
.
isBottomSheet
;
value
.
isDialog
=
currentRoute
.
isDialog
?
false
:
value
.
isDialog
;
});
...
...
lib/get_navigation/src/routes/route_middleware.dart
View file @
2e66dde
...
...
@@ -28,7 +28,7 @@ abstract class _RouteMiddleware {
/// }
/// ```
/// {@end-tool}
String
redirect
();
RouteSettings
redirect
();
/// 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
...
...
@@ -68,7 +68,7 @@ class GetMiddleware implements _RouteMiddleware {
GetMiddleware
({
this
.
priority
});
@override
String
redirect
()
=>
''
;
RouteSettings
redirect
()
=>
null
;
@override
GetPage
onPageCalled
(
GetPage
page
)
=>
page
;
...
...
@@ -106,12 +106,12 @@ class MiddlewareRunner {
return
page
;
}
String
runRedirect
()
{
var
to
=
''
;
RouteSettings
runRedirect
()
{
RouteSettings
to
;
_getMiddlewares
().
forEach
((
element
)
{
to
=
element
.
redirect
();
});
if
(
!
to
.
isNullOrBlank
)
{
if
(
to
!=
null
)
{
Get
.
log
(
'Redirect to
$to
'
);
}
return
to
;
...
...
@@ -141,3 +141,74 @@ class MiddlewareRunner {
void
runOnPageDispose
()
=>
_getMiddlewares
().
forEach
((
element
)
=>
element
.
onPageDispose
());
}
class
PageRedirect
{
GetPage
route
;
GetPage
unknownRoute
;
RouteSettings
settings
;
bool
isUnknown
;
PageRedirect
(
this
.
settings
,
this
.
unknownRoute
,
{
this
.
isUnknown
=
false
,
this
.
route
});
// redirect all pages that needes redirecting
GetPageRoute
page
()
{
while
(
needRecheck
())
{}
return
isUnknown
?
GetPageRoute
(
page:
unknownRoute
.
page
,
parameter:
unknownRoute
.
parameter
,
settings:
RouteSettings
(
name:
settings
.
name
,
arguments:
settings
.
arguments
),
curve:
unknownRoute
.
curve
,
opaque:
unknownRoute
.
opaque
,
customTransition:
unknownRoute
.
customTransition
,
binding:
unknownRoute
.
binding
,
bindings:
unknownRoute
.
bindings
,
transitionDuration:
(
unknownRoute
.
transitionDuration
??
Get
.
defaultTransitionDuration
),
transition:
unknownRoute
.
transition
,
popGesture:
unknownRoute
.
popGesture
,
fullscreenDialog:
unknownRoute
.
fullscreenDialog
,
middlewares:
unknownRoute
.
middlewares
,
)
:
GetPageRoute
(
page:
route
.
page
,
parameter:
route
.
parameter
,
settings:
RouteSettings
(
name:
settings
.
name
,
arguments:
settings
.
arguments
),
curve:
route
.
curve
,
opaque:
route
.
opaque
,
customTransition:
route
.
customTransition
,
binding:
route
.
binding
,
bindings:
route
.
bindings
,
transitionDuration:
(
route
.
transitionDuration
??
Get
.
defaultTransitionDuration
),
transition:
route
.
transition
,
popGesture:
route
.
popGesture
,
fullscreenDialog:
route
.
fullscreenDialog
,
middlewares:
route
.
middlewares
);
}
/// check if redirect is needed
bool
needRecheck
()
{
final
match
=
Get
.
routeTree
.
matchRoute
(
settings
.
name
);
Get
.
parameters
=
match
?.
parameters
;
// No Match found
if
(
match
?.
route
==
null
)
{
isUnknown
=
true
;
return
false
;
}
final
runner
=
MiddlewareRunner
(
match
.
route
.
middlewares
);
route
=
runner
.
runOnPageCalled
(
match
.
route
);
// No middlewares found return match.
if
(
match
.
route
.
middlewares
==
null
||
match
.
route
.
middlewares
.
isEmpty
)
{
return
false
;
}
return
runner
.
runRedirect
()
!=
null
;
}
}
...
...
test/navigation/middleware_test.dart
View file @
2e66dde
import
'package:flutter/cupertino.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:get/get.dart'
;
main
(
)
{
import
'get_main_test.dart'
;
class
RedirectMiddleware
extends
GetMiddleware
{
@override
RouteSettings
redirect
()
=>
RouteSettings
(
name:
'/second'
);
}
main
()
{
testWidgets
(
"Middleware redirect smoke test"
,
(
tester
)
async
{
await
tester
.
pumpWidget
(
GetMaterialApp
(
initialRoute:
'/'
,
getPages:
[
GetPage
(
name:
'/'
,
page:
()
=>
Container
()),
GetPage
(
name:
'/first'
,
page:
()
=>
FirstScreen
(),
middlewares:
[
RedirectMiddleware
()]),
GetPage
(
name:
'/second'
,
page:
()
=>
SecondScreen
()),
GetPage
(
name:
'/third'
,
page:
()
=>
ThirdScreen
()),
],
),
);
Get
.
toNamed
(
'/first'
);
await
tester
.
pumpAndSettle
();
print
(
Get
.
routing
.
current
);
expect
(
find
.
byType
(
SecondScreen
),
findsOneWidget
);
});
}
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment