如何解决GetX Get.ToNamed 在 child
我希望创建一个简单的应用程序来保持底部导航栏不变,改变它的孩子。
我认为 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 路由,但不能只使用它。 想要在控制器上保留 show child 的逻辑。 我该怎么办?
解决方法
使用
Get.reset();
Get.toNamed('${Routes.ITEMS}',parameters: parameters);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。