Skip to content

Commit

Permalink
Merge pull request #2898 from jonataslaw/indexed_route
Browse files Browse the repository at this point in the history
add IndexedRouteBuild to make nested routes easier
  • Loading branch information
jonataslaw committed Aug 31, 2023
2 parents 5dfdf3c + 2556477 commit a8d9f7b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 61 deletions.
106 changes: 51 additions & 55 deletions example_nav2/lib/app/modules/home/views/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,61 @@ 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),
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:
}
return Column(
children: [
Container(
color: Colors.yellow,
width: double.infinity,
height: 25,
),
Expanded(
child: GetRouterOutlet.builder(
route: Routes.home,
builder: (context) {
return Scaffold(
body: GetRouterOutlet(
initialRoute: Routes.dashboard,
anchorRoute: Routes.home,
),
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(
icon: Icon(Icons.home),
label: 'Home',
),
// _Paths.HOME + Routes.PROFILE
BottomNavigationBarItem(
icon: Icon(Icons.account_box_rounded),
label: 'Profile',
),
// _Paths.HOME + _Paths.PRODUCTS
BottomNavigationBarItem(
icon: Icon(Icons.account_box_rounded),
label: 'Products',
),
],
);
}),
);
},
items: const [
// _Paths.HOME + [Empty]
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
// _Paths.HOME + Routes.PROFILE
BottomNavigationBarItem(
icon: Icon(Icons.account_box_rounded),
label: 'Profile',
),
// _Paths.HOME + _Paths.PRODUCTS
BottomNavigationBarItem(
icon: Icon(Icons.account_box_rounded),
label: 'Products',
),
],
),
);
},
),
],
);
}
}

6 changes: 2 additions & 4 deletions lib/get_navigation/src/routes/page_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/get_navigation/src/routes/route_middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 56 additions & 1 deletion lib/get_navigation/src/routes/router_outlet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand All @@ -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);
}
}

0 comments on commit a8d9f7b

Please sign in to comment.