0

I have BottomNavigationBar in flutter & called separate widgets for each tab. For example I am having button into that widget which redirects to new screen, I want that BottomNavigationBar should be persisted in new screen as well. I have checked couple of articles but unable to understand. Below is code for BottomTabNavigationBar

import 'package:flutter/material.dart';
import 'package:timeout_dubai/fragments/HomeFragment.dart';
import 'package:timeout_dubai/fragments/MtTimeFragment.dart';
import 'package:timeout_dubai/fragments/MyLocFragment.dart';
import 'package:timeout_dubai/fragments/ThingsLoveFragment.dart';

class LoginScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeState();
  }
}

class _HomeState extends State<LoginScreen> {
  int _currentIndex = 0;

  GlobalKey globalKey = new GlobalKey(debugLabel: 'btm_app_bar');

  final List<Widget> _children = [
      new HomeFragment(),
      new ThingsLoveFragment(),
      new MyLocFragment(),
      new MyTimeFragment()

  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTabTapped,
        key: globalKey,
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home,),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.favorite),
            title: Text('Things I Love'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.location_on),
            title: Text('My Locations'),
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text('My Time Out')
          ),

        ],
      ),
    );
  }

  void onTabTapped(int index) {
    print("Index $index");
    setState(() {
      _currentIndex = index;
    });

  }
}
sagarchavan
  • 187
  • 3
  • 14
  • As you know, bottomNavigation is also a widget. You can send that widget to the new state and can place it inside. Also, in a different dart file, you can create a custom statelesswidget that handles your bottom navigation thing. After all, you can import that class in each state manually adding path of your custom statelesswidget inside the current file. Just tried to give you some different views. Hope it helps. – Zahid Tekbaş Mar 31 '20 at 08:18
  • You meant to say I have to create a Separate widget for BottomNavBar and have to use it importing everywhere in the screens, Right? – sagarchavan Mar 31 '20 at 12:28

0 Answers0