Search code examples
androidiosflutterdartflutter-navigation

Navigation & Routing Flutter


I just started flutter and trying to build a bottom navigation bar that navigates between 4 pages:

'.../pages/home.dart';
'.../pages/history.dart';
'.../pages/search.dart';
'.../pages/bookmarks.dart';

The home page should be always on display as the main screen when starting the app. (obviously !!)

I build the navigation bar following some documentations. The navigation bar seems to work with no trouble but the problem is

I have no idea of where and how to implement the rest of the navigation and tab switching logic

this is my main_screen.dart

class _MainScreenState extends State<MainScreen> {
  PageController _pageController;
  int _page = 0;
  @override 
  Widget build(BuildContext context) {
    return Scaffold( 
      body: PageView( 
        physics: NeverScrollableScrollPhysics(),
        controller: _pageController,
        onPageChanged: onPageChanged,
        children: List.generate(4, (index) => Home()),
      ),
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
          canvasColor: Theme.of(context).primaryColor,
          primaryColor: Theme.of(context).accentColor,
          textTheme: Theme.of(context).textTheme.copyWith(caption: TextStyle(color: Colors.grey[400]),),
        ),
        child: BottomNavigationBar( 
          type: BottomNavigationBarType.fixed,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(
                Feather.getIconData("home"),
              ),
              title: Container(height: 0.0),
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Feather.getIconData("file"),
              ),
              title: Container(height: 0.0),
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Feather.getIconData("search"),
              ),
              title: Container(height: 0.0),
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Feather.getIconData("bookmark"),
              ),
              title: Container(height: 0.0),
            ),
          ],
          onTap: navigationTapped,
          currentIndex: _page,
        ),
      ),
    );
  }

  void navigationTapped(int page){
    _pageController.jumpToPage(page);
  }
  @override 
  void initState(){
    super.initState();
    _pageController = PageController(initialPage: 0);
  }
  @override 
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }
  void onPageChanged(int page){
    setState(() {
      this._page = page;
    });
  }
}

Solution

  • So in your PageView you list children These are the pages that should correspond to your tabs.

    At the moment you have your Home widget listed 4 times, which will obviously not show any difference when you click the tabs.

    if you replace make your children like this it should work fine

    children: [Home(), History(), Search(), Bookmarks()]