Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give onPress function for BottomNavigationBar menu in flutter?

Tags:

flutter

dart

i have a bottomnavigation bar in flutter ,what i want is on click of the individual widgets of the bottombar it should navigate me to the next page

    Widget _bottemTab() {
    return new BottomNavigationBar(


    currentIndex: _currentIndex,
    onTap:(newIndex) => setState((){_currentIndex = newIndex;}),
    type: BottomNavigationBarType.fixed,
    items: [
      new BottomNavigationBarItem(
          icon: Image.asset(
            "assets/home.png",
            width: 24.0,
            height: 24.0,
          ),
          title: Text(
            'Home',
          ),
      ),
      new BottomNavigationBarItem(
          icon: Image.asset(
            "assets/shopping-bagg.png",
            width: 24.0,
            height: 24.0,
          ),
          title: Text(
            'Mall',
          )),
      new BottomNavigationBarItem(
          icon: Image.asset(
            "assets/qr-code.png",
            width: 24.0,
            height: 24.0,
          ),
          title: Text(
            'Scan',
          )),
      new BottomNavigationBarItem(

          icon: Image.asset(
            "assets/bank.png",
            width: 24.0,
            height: 24.0,
          ),
          title: Text(
            'Bank',
          )),



      new BottomNavigationBarItem(
          icon: Image.asset(
            "assets/delivery.png",
            width: 24.0,
            height: 24.0,
          ),
          title: Text(
            'Inbox',
          )),


    ]);
      }

i want is on click of the individual widgets of the bottombar it should navigate me to the next page which i have created separately for each menu items of bottom navigation view..any help is appreciated

like image 352
akshay dhone Avatar asked Aug 14 '19 13:08

akshay dhone


People also ask

How to set bottomnavigationbar in flutter?

In Flutter application, we usually set the bottom navigation bar in conjunction with the scaffold widget. Scaffold widget provides a Scaffold.bottomNavigationBar argument to set the bottom navigation bar. It is to note that only adding BottomNavigationBar will not display the navigation items.

How to create a navigation bar in flutter material app?

Let’s get started. Once you get your basic flutter app up and running, your material app assigns the HomePage () class to the home attribute. Now create a stateful widget and name it as HomePage. Return a Scaffold from the HomePage class. Now this Scaffold is the main element that contains our bottom navigation bar.

What is the bottom navigation bar?

A material widget that’s displayed at the bottom of an app for selecting among a small number of views, typically… What is not working? According to point 2, the Bottom Navigation Bar should remain unchanged but it does not.

What is commonbottomnavigationbar widget?

A material widget that’s displayed at the bottom of an app for selecting among a small number of views, typically… What is not working? According to point 2, the Bottom Navigation Bar should remain unchanged but it does not. We also need to update the MainScreen to use the CommonBottomNavigationBar Widget-


2 Answers

In your build() method, you can add this logic.

@override
Widget build(BuildContext context) {
  Widget widget = Container(); // default
  switch (_currentIndex) {
    case 0:
      widget = FirstPage();
      break;

    case 1:
      widget = SecondPage();
      break;

    case 2:
      widget = ThirdPage();
      break;
  }

  return Scaffold(
    body: widget,
    // ...
  );
}

Update:

I'm not sure how you did that, here you can see a small example of doing it right way.

enter image description here

int _index = 0;

@override
Widget build(BuildContext context) {
  Widget child = Container();

  switch(_index) {
    case 0:
      child = FlutterLogo(colors: Colors.orange);
      break;

    case 1:
      child = FlutterLogo();
      break;
  }

  return Scaffold(
    appBar: AppBar(),
    bottomNavigationBar: _bottomTab(),
    body: SizedBox.expand(child: child),
  );
}

Widget _bottomTab() {
  return BottomNavigationBar(
    currentIndex: _index,
    onTap: (int index) => setState(() => _index = index),
    backgroundColor: Colors.deepPurple,
    items: [
      BottomNavigationBarItem(icon: Icon(Icons.looks_one), title: Text("1")),
      BottomNavigationBarItem(icon: Icon(Icons.looks_two), title: Text("2")),
    ],
  );
}
like image 115
CopsOnRoad Avatar answered Nov 15 '22 06:11

CopsOnRoad


Hope this helps.

int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const _pages = <Widget>[
  Home(),//this is a stateful widget on a separate file 
  Business(),//this is a stateful widget on a separate file 
  School(),//this is a stateful widget on a separate file 

];

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

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('BottomNavigationBar Sample'),
    ),
    body: Center(
      child: _pages.elementAt(_selectedIndex),
    ),
    bottomNavigationBar: BottomNavigationBar(
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text('Home'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.business),
          title: Text('Business'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.school),
          title: Text('School'),
        ),
      ],
      currentIndex: _selectedIndex,
      selectedItemColor: Colors.amber[800],
      onTap: _onItemTapped,
    ),
  );
}
like image 34
Ali Bacelonia Avatar answered Nov 15 '22 07:11

Ali Bacelonia