【问题标题】:GetX Get.ToNamed doesn't work inside childGetX Get.ToNamed 在孩子内部不起作用
【发布时间】:2021-05-12 03:43:54
【问题描述】:

我希望创建一个简单的应用程序,让 bottomNavigationBar 保持不变,将其更改为子项。
我认为 GetX 是实现它的最佳和最简单的方法,但是,我遇到了一个意想不到的问题。

这里是sn-p的代码:

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


    【解决方案1】:

    使用

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

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2021-11-01
      • 2018-06-14
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多