Flutter-GetX的页面替换系统不工作



我有一个这样的代码:

main.dart:

body: GetX<PaginationController>(
init: PaginationController(),
initState: (_) {},
builder: (controller) {
if (controller.PageIndex.value == 0) {
return Pages[0];
} else if (controller.PageIndex.value == 1) {
return Pages[1];
} else if (controller.PageIndex.value == 2) {
return Pages[2];
}
},
),

PaginationController:

class PaginationController extends GetxController {
RxInt PageIndex = RxInt(0);
}

说到这个问题,当我按下按钮时,我希望它从菜单中的一个页面切换到另一个页面。但它不会改变。在控制器中,PageIndex的值会更改,但页面不会更改。

菜单代码:

PaginationController paginationController = PaginationController();
int PageIndex = paginationController.PageIndex.value;
//...
bottomNavigationBar: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30),
topLeft: Radius.circular(30),
),
boxShadow: [
BoxShadow(color: Colors.black38, spreadRadius: 0, blurRadius: 10),
],
),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),  
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Color.fromARGB(255, 53, 53, 57),
selectedFontSize: 16,
unselectedFontSize: 13,
selectedItemColor: Color(0xFFFBBF24),
selectedLabelStyle: TextStyle(color: Color(0xFFFBBF24), fontFamily: "Inter Bold"),
unselectedIconTheme: IconThemeData(
color: Color.fromARGB(255, 192, 192, 192),
),
unselectedLabelStyle: TextStyle(fontFamily: "Inter Regular"),
unselectedItemColor: Color.fromARGB(255, 192, 192, 192),
currentIndex: PageIndex,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
label: "Ana Sayfa",
icon: Icon(Icons.home)
),
BottomNavigationBarItem(
label: "Sepet",
icon: Icon(Icons.shopping_basket_outlined)
),
BottomNavigationBarItem(
label: "Cüzdan",
icon: Icon(Icons.account_balance_wallet_rounded)
),
],
onTap: (int index) {
setState(() {
PageIndex = index;
print(PageIndex);
print(index);
});
},
),
),
),

为了更改页面,我写了以下代码:

onPressed: () {
setState(() {
PageIndex = 2;
});
},

菜单和车身代码在一个文件中。

PaginationController中的值会更改,但页面不会更改。为什么会这样?我该如何解决这个问题?提前感谢您的帮助。

试着看看这个,它可能会帮助你我回复的那个

Flutter:如何通过GetxController控制PageView?

最新更新