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
2023-08-31 20:40:01 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
2556477619bbf37d9c4534ab64c3effd05017b05
25564776
1 parent
7bd4623b
add IndexedRouteBuild to make nested routes easier
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
38 deletions
example_nav2/lib/app/modules/home/views/home_view.dart
lib/get_navigation/src/routes/page_settings.dart
lib/get_navigation/src/routes/route_middleware.dart
lib/get_navigation/src/routes/router_outlet.dart
example_nav2/lib/app/modules/home/views/home_view.dart
View file @
2556477
...
...
@@ -6,45 +6,36 @@ import '../controllers/home_controller.dart';
class
HomeView
extends
GetView
<
HomeController
>
{
const
HomeView
({
Key
?
key
})
:
super
(
key:
key
);
@override
Widget
build
(
BuildContext
context
)
{
return
GetRouterOutlet
.
builder
(
routerDelegate:
Get
.
nestedKey
(
Routes
.
home
),
return
Column
(
children:
[
Container
(
color:
Colors
.
yellow
,
width:
double
.
infinity
,
height:
25
,
),
Expanded
(
child:
GetRouterOutlet
.
builder
(
route:
Routes
.
home
,
builder:
(
context
)
{
final
delegate
=
context
.
navigation
;
//This router outlet handles the appbar and the bottom navigation bar
final
currentLocation
=
context
.
location
;
var
currentIndex
=
0
;
if
(
currentLocation
.
startsWith
(
Routes
.
products
)
==
true
)
{
currentIndex
=
2
;
}
if
(
currentLocation
.
startsWith
(
Routes
.
profile
)
==
true
)
{
currentIndex
=
1
;
}
return
Scaffold
(
body:
GetRouterOutlet
(
initialRoute:
Routes
.
dashboard
,
anchorRoute:
Routes
.
home
,
//delegate: Get.nestedKey(Routes.HOME),
// key: Get.nestedKey(Routes.HOME),
),
bottomNavigationBar:
BottomNavigationBar
(
currentIndex:
currentIndex
,
onTap:
(
value
)
{
switch
(
value
)
{
case
0
:
delegate
.
toNamed
(
Routes
.
home
);
break
;
case
1
:
delegate
.
toNamed
(
Routes
.
profile
);
break
;
case
2
:
delegate
.
toNamed
(
Routes
.
products
);
break
;
default
:
}
},
bottomNavigationBar:
IndexedRouteBuilder
(
routes:
const
[
Routes
.
dashboard
,
Routes
.
profile
,
Routes
.
products
],
builder:
(
context
,
routes
,
index
)
{
final
delegate
=
context
.
delegate
;
return
BottomNavigationBar
(
currentIndex:
index
,
onTap:
(
value
)
=>
delegate
.
toNamed
(
routes
[
value
]),
items:
const
[
// _Paths.HOME + [Empty]
BottomNavigationBarItem
(
...
...
@@ -62,9 +53,14 @@ class HomeView extends GetView<HomeController> {
label:
'Products'
,
),
],
),
);
}),
);
},
),
),
],
);
}
}
...
...
lib/get_navigation/src/routes/page_settings.dart
View file @
2556477
...
...
@@ -43,11 +43,9 @@ extension PageArgExt on BuildContext {
return
parser
?.
restoreRouteInformation
(
config
)?.
location
??
'/'
;
}
RouterDelegate
get
delegate
{
return
router
.
routerDelegate
;
}
GetDelegate
get
navigation
{
GetDelegate
get
delegate
{
return
router
.
routerDelegate
as
GetDelegate
;
}
}
...
...
lib/get_navigation/src/routes/route_middleware.dart
View file @
2556477
...
...
@@ -234,7 +234,7 @@ class PageRedirect {
if
(
settings
==
null
&&
route
!=
null
)
{
settings
=
route
;
}
final
match
=
context
.
navigation
.
matchRoute
(
settings
!.
name
!);
final
match
=
context
.
delegate
.
matchRoute
(
settings
!.
name
!);
Get
.
parameters
=
match
.
parameters
;
// No Match found
...
...
lib/get_navigation/src/routes/router_outlet.dart
View file @
2556477
...
...
@@ -148,10 +148,14 @@ class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> {
required
Widget
Function
(
BuildContext
context
,
)
builder
,
String
?
route
,
GetDelegate
?
routerDelegate
,
})
:
super
.
builder
(
builder:
builder
,
delegate:
routerDelegate
,
delegate:
routerDelegate
??
(
route
!=
null
?
Get
.
nestedKey
(
route
)
:
Get
.
rootController
.
rootDelegate
),
);
}
...
...
@@ -166,3 +170,54 @@ extension PagesListExt on List<GetPage> {
return
pickAtRoute
(
route
).
skip
(
1
);
}
}
class
GetRouterOutletInherited
extends
InheritedWidget
{
final
String
anchorRoute
;
const
GetRouterOutletInherited
({
super
.
key
,
required
this
.
anchorRoute
,
required
Widget
child
,
})
:
super
(
child:
child
);
static
GetRouterOutletInherited
?
of
(
BuildContext
context
)
{
return
context
.
dependOnInheritedWidgetOfExactType
<
GetRouterOutletInherited
>();
}
@override
bool
updateShouldNotify
(
covariant
InheritedWidget
oldWidget
)
{
return
true
;
}
}
typedef
NavigatorItemBuilderBuilder
=
Widget
Function
(
BuildContext
context
,
List
<
String
>
routes
,
int
index
);
class
IndexedRouteBuilder
<
T
>
extends
StatelessWidget
{
const
IndexedRouteBuilder
({
Key
?
key
,
required
this
.
builder
,
required
this
.
routes
,
})
:
super
(
key:
key
);
final
List
<
String
>
routes
;
final
NavigatorItemBuilderBuilder
builder
;
// Method to get the current index based on the route
int
_getCurrentIndex
(
String
currentLocation
)
{
for
(
int
i
=
0
;
i
<
routes
.
length
;
i
++)
{
if
(
currentLocation
.
startsWith
(
routes
[
i
]))
{
return
i
;
}
}
return
0
;
// default index
}
@override
Widget
build
(
BuildContext
context
)
{
final
location
=
context
.
location
;
final
index
=
_getCurrentIndex
(
location
);
return
builder
(
context
,
routes
,
index
);
}
}
...
...
Please
register
or
login
to post a comment