user9378857
user9378857

Reputation: 61

GetX Get.toNamed() is ignoring onGenerateRoute on Navigator

I have a problem, I'm using GetX in my Flutter Application. I need to use nested navigation cause I'm using BottomNavigationBar. First I have a Navigator Widget, the problem is when the Get.toNamed method is called and the onGenerateRoute is not executing.

This is my code

class NavigatorPage extends GetWidget<NavigatorController> {
  NavigatorPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> pages = [
      Navigator(
          key: Get.nestedKey(2), // create a key by index
          onGenerateRoute: (settings) {
            if (settings.name == VozComercialRoutes.home) {
              return GetPageRoute(
                  page: () => HomePage(), binding: HomePageBinding());
            } else if (settings.name == VozComercialRoutes.listaRegistros) {
              return GetPageRoute(
                  page: () => ListaRegistrosPage(),
                  binding: ListaRegistrosBinding());
            }
          }),
      Navigator(
          key: Get.nestedKey(2), // create a key by index
          onGenerateRoute: (settings) {
            if (settings.name == VozComercialRoutes.home) {
              return GetPageRoute(
                  page: () => HomePage(), binding: HomePageBinding());
            } else if (settings.name == VozComercialRoutes.listaRegistros) {
              return GetPageRoute(
                  page: () => ListaRegistrosPage(),
                  binding: ListaRegistrosBinding());
            }
          }),
    ];
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: CustomAppBar(),
      body: PageView(
        onPageChanged: controller.changePage,
        controller: controller.pageController,
        children: pages,
      ),
      bottomNavigationBar: Obx(() => BottomNavigationBar(
              onTap: controller.changePage,
              currentIndex: controller.tabIndex.value,
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'home',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.download),
                  label: 'download',
                ),
              ])),
    );
  }
}

The navigate code is

Get.toNamed(VozComercialRoutes.registro, arguments: registro);

I have tried with the following code and it works, but I want to use GetX

Navigator.pushNamed(VozComercialRoutes.registro)

Thanks

Upvotes: 4

Views: 4305

Answers (1)

Michael Soliman
Michael Soliman

Reputation: 2788

try

Get.toNamed(
    VozComercialRoutes.registro,
    arguments: registro,
    // the same id as the navigation key
    id: "2"  
);

Upvotes: 2

Related Questions