GetX Get.ToNamed 在子进程中不起作用

问题描述 投票:0回答:2

我希望创建一个简单的应用程序,保持底部导航栏不变,改变它的子项。
我认为 GetX 是实现该目标的最佳且最简单的方法,但是,我面临着一个意想不到的问题。

这是代码片段:

class Home extends StatelessWidget {
  final NavController navController = Get.put(NavController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        //child: navController.bodyContent[navController.selectedIndex],
        child: Obx(
          // ignore: missing_return
          () {
            String _test =
                navController.bodyContent[navController.selectedIndex];
            print(_test);

            return Profile(); //well, it's hardcoded but it works. 
            //I can realise my logic to change screens right here...
            //... but I'm not sure it's good enough
            
            //And I can see recived from controller text but cannot use it as desighned.            
            //return Text(_test); 


            //return Get.toNamed(_test); //that's what i supposed to realize but faced an error:
            //The return type 'Future<dynamic>' isn't a 'Widget', as required by the closure's context.
          },
          //() => Text(navController.bodyContent[navController.selectedIndex])),
          //    navController.bodyContent[navController.selectedIndex]),
          //child: Showcase(),)
        ),
      ),
      bottomNavigationBar: Obx(() => BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            items: [], //here i specify my BottomNavigationBarItems
            currentIndex: navController.selectedIndex,
            onTap: (index) => navController.selectedIndex = index,
          )),
    );
  }
}

控制器:

class NavController extends GetxController {
  final _selectedIndex = 0.obs;
  final List<String> bodyContent = [
    '/showcase',
    '/liked',
    '/publish',
    '/messages',
    '/profile',
  ].obs;

  get selectedIndex => this._selectedIndex.value;
  set selectedIndex(index) => this._selectedIndex.value = index;
}

嗯,它部分有效。 我可以接收 bodyContent 路由,但不能只使用它。 想要在控制器中保留显示子项的逻辑。 我该怎么办?

flutter navigation flutter-getx
2个回答
1
投票

使用

Get.reset();
Get.toNamed('${Routes.ITEMS}', parameters: parameters);

0
投票

也许您需要的是 GetX 的嵌套导航,对吗?您是否尝试过 Get.nestedKey() 与 flutter 的默认导航器结合使用,您可以使用 Get.toNamed('url', id:'nested-key-id') 在 child 内开始导航

© www.soinside.com 2019 - 2024. All rights reserved.