BottomNavigationBar with Flutter Getx



我有一个问题与Getx与底部导航栏。在控制器中,它更新值但只有当我保存时,buttonnavigationbar当前索引设置为那个值才更新。我得到这个错误:被小部件库捕获的异常= = =错误使用ParentDataWidget.

class BottomNavigation extends StatelessWidget {
BottomNavigation({
Key? key,
}) : super(key: key);
final AppController controller = Get.put(AppController());
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: controller.tabIndex.value,
onTap: (index) {
controller.changeTabIndex(index);
print(controller.tabIndex.value);
},
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
],
);
}
}
class _MyAppState extends State<MyApp> {
// Inintialize app router
@override
void initState() {
super.initState();
}
final AppController appController = Get.put(AppController());
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Politechnica',
theme: UniversityAppTheme.light(),
home: Obx(
() => IndexedStack(
index: appController.tabIndex.value,
children: Screens.pages,
),
),
);
}
}
class AppController extends GetxController {
final tabIndex = 0.obs;
changeTabIndex(int tab) {
tabIndex.value = tab;
}
}

你需要移动状态逻辑到你的AppController和使用父无状态视图,可以是一个Getview注入你的控制器和使用GetBuilder而不是Obx

最新更新