0

I have a code like this:

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);
}

Coming to the question, when I press the button, I want it to switch from one of the pages in the menu to another. But it doesn't switch. In Controller the value of PageIndex changes but the page does not.

Menu codes:

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);
              });
            },
          ),
        ),
      ),

To change the page, I wrote the following code:

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

Menu and Body codes in one file.

The value in PaginationController changes but the page does not. Why could it be? How can I solve the problem? Thanks in advance for your help.

Emir Bolat
  • 899
  • 3
  • 14
  • 37

1 Answers1

0

try to look at this it might help you the one i answered back

Flutter: How to control a PageView by GetxController?

Arbiter Chil
  • 1,061
  • 1
  • 6
  • 9