0

我希望创建一个简单的应用程序,保持 bottomNavigationBar 不变,改变它的孩子。
我想 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 路线,但不能只使用它。想要在控制器中保留显示子项的逻辑。我应该怎么办?

4

1 回答 1

0

利用

Get.reset();
Get.toNamed('${Routes.ITEMS}', parameters: parameters);
于 2021-04-07T20:55:14.243 回答