Home > Software design >  BottomNavigationBar with Navigator?
BottomNavigationBar with Navigator?

Time:12-21

I have a problem with this code, is not working why? please.

My code :

Container(
  color: Colors.white,
  child: BottomNavigationBar(
    onTap: (_index) {
      StepState.disabled.index;
      switch (_index) {
        case 0:
          onTap:
          () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => Home()),
            );
          };
          break;

        case 1:
          onTap:
          () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => NosVoyages()),
            );
          };
          break;
      }
      ;
    },
    currentIndex: _index,
    items: [
      BottomNavigationBarItem(
          label: '',
          icon: Padding(
            padding: const EdgeInsets.only(top: 10.0),
            child: Icon(Icons.home),
          )),
      BottomNavigationBarItem(
          label: '',
          icon: Padding(
            padding: const EdgeInsets.only(top: 10.0),
            child: Icon(Icons.list_alt),
          )),
    ],
    type: BottomNavigationBarType.fixed,
    backgroundColor: Colors.white,
    unselectedItemColor: Colors.black,
  ),
);

The home page is loading fine but as soon as I click any button, it change nothing . Probably there is an issue with MaterialApp or Scaffold but I am not able to fix it yet. Can anyone tell me what's the problem and how to fix it?

CodePudding user response:

try this variant initialized _index

int _index=0;
 @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            top: false,
            child: Container(
              color: Colors.white,
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: BottomNavigationBar(
                  onTap: (_index) {
                    StepState.disabled.index;
                    switch (_index) {
                      case 0:
                        {
                          Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) => Home()),
                          );
                          setState(() {
                            _index = 1;
                          });
                        }
                        ;
                        break;

                      case 1:
                        {
                          setState(() {
                            _index = 0;
                          });
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => NosVoyages()),
                          );
                        }
                        ;
                        break;
                    }
                  },
                  currentIndex: _index,
                  items: [
                    BottomNavigationBarItem(
                        label: '',
                        icon: Padding(
                          padding: const EdgeInsets.only(top: 10.0),
                          child: Icon(Icons.home),
                        )),
                    BottomNavigationBarItem(
                        label: '',
                        icon: Padding(
                          padding: const EdgeInsets.only(top: 10.0),
                          child: Icon(Icons.list_alt),
                        )),
                  ],
                  type: BottomNavigationBarType.fixed,
                  backgroundColor: Colors.white,
                  unselectedItemColor: Colors.black,
                ),
              ),
            )));
  }
  • Related