Home > Back-end >  Hide and replace BottomNavigationBar once pressed
Hide and replace BottomNavigationBar once pressed

Time:02-15

I have a BottomNavigationBar which if the user pressed on an icon, then it'll show the CarouselSlider and hide the BottomNavigationBar

This is my code, but I'm not sure if I'm getting the right index of the pressed icon and if this is the right way to execute it since I get this error

The argument type 'void Function()' can't be assigned to the parameter type 'void Function(int)?'

int _selectedIndex = 0;

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

    bool carosuel_vis = false;
    bool bottom_vis = true;

bottomNavigationBar: Stack(children: [
        Visibility(
          visible: bottom_vis,
          child: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: 'Carosuel',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.business),
                label: 'Business',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.school),
                label: 'School',
              ),
            ],
            onTap : () {
              _onItemTapped, 
                  if(_selectedIndex == 0) {carosuel_vis = !carosuel_vis}; // a lot of errors in here
            }
          ),
        ),
        BottomAppBar(
          color: Colors.white,
          child: Visibility(
              visible: carosuel_vis,
              child: CarouselSlider.builder(
                itemCount: imageList.length,
                options: CarouselOptions(
                  autoPlay: true,
                  aspectRatio: 2.0,
                  enlargeCenterPage: true,
                ),
                itemBuilder: (context, index, realIdx) {
                  return Container(
                      child: Center(
                    child: GestureDetector(
                        onTap: () {
                          ff(imageList[index], widget.image.path).then((value) {
                            setState(() {
                              finalsd = value; //update the image
                            });
                          });
                        },
                        child: Image.network(imageList[index],
                            fit: BoxFit.cover, width: 1000)),
                  ));
                },
              )),
        ),
      ])

CodePudding user response:

try this

onTap: _onItemTapped,


void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      if(_selectedIndex == 0) {carosuel_vis = !carosuel_vis};
    });
  }

CodePudding user response:

BottomNavigationBar's onTap comes with index.

 onTap : (index) {
    _onItemTapped(index);
       if(_selectedIndex == 0) {carosuel_vis = !carosuel_vis}; 
    }
  • Related