irongirl
irongirl

Reputation: 935

Navigate using bottom app bar tabs - flutter

I am using bottom app bar for bottomnavigation in flutter. when tapped on one of the tab in the bottom app bar, i would still want the bottom app bar and app bar to remain as it is in it's fixed position but only the body content changes based on what is tapped.

I have tried to use push() method but it gives me a new page instead with a back button.

Navigation_tabs.dart:

import 'package:flutter/material.dart';

class NavigationTabs extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: () {},
        ),
        appBar: AppBar(
          title: Text('Dashboard'),
        ),
        bottomNavigationBar: BottomAppBar(
          shape: CircularNotchedRectangle(),
          notchMargin: 4.0,
          child: new Row(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.home,
                  color: Colors.cyan[700],
                ),
                onPressed: () {},
              ),
              new Container(
                  padding: EdgeInsets.only(left: 20),
                  child: IconButton(
                    icon: Icon(
                      Icons.list,
                      color: Colors.cyan[700],
                    ),
                    onPressed: () => Navigator.pushNamed(context, '/login'),
                  )),
              new Container(
                  padding: EdgeInsets.only(left: 120),
                  child: IconButton(
                    icon: Icon(
                      Icons.explore,
                      color: Colors.cyan[700],
                    ),
                    onPressed: () {},
                  )),
              new Container(
                  height: 22.0,
                  child: new RawMaterialButton(
                    onPressed: () {},
                    child: new Icon(
                      Icons.person,
                      color: Colors.white,
                      size: 20.0,
                    ),
                    shape: new CircleBorder(),
                    elevation: 1.0,
                    fillColor: Colors.cyan[700],
                  ))
            ],
          ),
        ));
  }
}

I want to be able to only make the page content change without a back button instead of going to a completely new page when one of the tabs is pressed.

Upvotes: 3

Views: 5393

Answers (1)

Sean Ervinson
Sean Ervinson

Reputation: 123

You can use a BottomNavigationBarItem instead of creating buttons and use the ontap of the bottomNavigationBar.

class _MyHomePageState extends State<MyHomePage> {
  int _index = 0;
  final List<Widget> _children = [
    Center(child: Text("First Page"),),
    Center(child: Text("Second Page"),),
    Center(child: Text("Third Page"),)
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Bottom Navigation"),
      ),
      body: Center(
        child: (_children[_index ]),
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped,
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.looks_one),
            title: Text('First'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.looks_two),
            title: Text('Second'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.looks_3),
            title: Text('Third'),
          )
        ],
      ),
    );
  }

  void onTabTapped(int index) {
   setState(() {
     _index = index;
   });
 }
}

Sample

For more detailed explanation:

Flutter documentation

Upvotes: 1

Related Questions