Flutter底部导航栏,使用Getx进行绑定



我有问题,希望能帮助我。当我在底部导航栏上点击主页时,我如何调用页面主页的绑定

**底部导航栏控制器**

class BottomNavBarController extends GetxController{
List bodyPage=[
const Home(),
const Settings(),
const ProfileView()
];

导航栏

body:Obx(()=> navBarController.currentPage,
),
// bodyPage[navBarController.indexNavBar],
bottomNavigationBar:Obx(()=> NavigationBar(
currentIndex: navBarController.indexNavBar.value,
items: [
NavigationBarItem(
icon: const Icon(home_outline, ),

),
NavigationBarItem(
icon: const Icon(setting_outline),

),
NavigationBarItem(
icon: const Icon(menu_outline),

)])));

**绑定**

class HomeBinding implements Bindings{
@override
void dependencies() {
Get.put(HomeController());
}}

**获取页面**

getPages: [
GetPage(name: "/home", page:()=> const HomeView(), binding:HomeBinding()),
]

我使用底部导航和Getx。这样:

changeNavigationIndex(int index) {

myPresenter.setMainNavigationIndex(index);
switch (index) {
case 1:
setState(() {
mainNavigationTitle = txtTitle2;
});
break;
case 2:
setState(() {
mainNavigationTitle = txtTitle3;
});
break;
case 3:
setState(() {
mainNavigationTitle = txtTitle4;
});
break;
case 0:
default:
setState(() {
mainNavigationTitle = txtTitle1;
});
break;
}
}

buildBottomNavigationMenu(上下文,MyPresenter MyPresenter({

changeNavigationIndex(myPresenter.mainNavigationIndex);
return Obx(() =>
MediaQuery(
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
child: SizedBox(
child: BottomNavigationBar(
onTap: changeNavigationIndex,
type: BottomNavigationBarType.fixed,
currentIndex: salesPresenter.mainNavigationIndex,
selectedIconTheme: IconThemeData(
color: CustomColors.iconColor,
size: 30,
),
items: [
BottomNavigationBarItem(
icon: const Icon(Icons.icon1),
label: txt1,
backgroundColor: Theme.of(context).primaryColor,
),
BottomNavigationBarItem(
icon: const Icon(Icons.icon2),
label: txt2,
backgroundColor: Theme.of(context).primaryColor,
),
BottomNavigationBarItem(
icon: const Icon(Icons.icon3),
label: txt3,
backgroundColor: Theme.of(context).primaryColor,
),
BottomNavigationBarItem(
icon: const Icon(Icons.icon4),
label: txt4,
backgroundColor: Theme.of(context).primaryColor,
),
],
),
)
));

}

return
WillPopScope(
onWillPop: () async => false,
child: Scaffold(
body:
: GetX<MyPresenter>(
builder: (mp) => IndexedStack(
index: mp.mainNavigationIndex,
children: const[
Screen1(),
Screen2(),
Screen3(),
Screen4(),
],
)
),
bottomNavigationBar: buildBottomNavigationMenu(context, myPresenter),
drawer: const MainDrawerWidget(),
),
);

因此,在这个应用程序中,所有Getx绑定都在我在Main.dart.上调用的分离文件(MainBind.dart(中

但如果你想在你已经编程底部导航的同一个文件中调用绑定,你可以把绑定放在你的主类中,如下所示:

class _MainScreenState extends State<MainScreen> {
Get.lazyPut<MyPresenter>(() => MyPresenter(), fenix: true);
}

最新更新