Jonny Borges
Committed by GitHub

Merge pull request #2898 from jonataslaw/indexed_route

add IndexedRouteBuild to make nested routes easier
... ... @@ -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',
),
],
),
);
}),
);
},
),
),
],
);
}
}
... ...
... ... @@ -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;
}
}
... ...
... ... @@ -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
... ...
... ... @@ -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);
}
}
... ...