2

I have 4 pages in bottom navigation bar. I am facing a problem related to route that i want to route to a specific page in bottom navigation bar from another page? e.g I am going to login page and post successful login I want to route user to accountdetails(AccountSetting) but if I am selecting account setting then in that page bottom navigation bar is not showing and if I am routing to bottom nav bar here I am unable to select index of my choice. How can I select index of my choice because I don't want to make new page having bottom nav bar?

class BottomNavBar extends StatefulWidget {
  static String routeName = "/bottombar";

  @override
  _BottomNavBarState createState() => _BottomNavBarState();
}

class _BottomNavBarState extends State<BottomNavBar> {
  CartCountController cartCountController = CartCountController();
  DoublePressBackBotton doublePressBackBotton = DoublePressBackBotton();

  int _selectedIndex = 0;
  @override
  void initState() {
    Future.delayed(Duration(milliseconds: 1000)).then((value) {
      cartCountController.cartItemCount();
    });
    super.initState();
  }

  @override
  void dispose() {
    Future.delayed(Duration(milliseconds: 1000)).then((value) {
      cartCountController.cartItemCount();
    });
    super.dispose();
  }

  List _widgetOptions = [
    HomeScreen(),
    CategoryList(),
    InviteFriend(),
    AccountSetting(),
  ];

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

  @override
  Widget build(BuildContext context) {
    return
       
 
      // drawer: SideDrawer(),
      body: WillPopScope(
        child: _widgetOptions.elementAt(_selectedIndex),
        onWillPop: () {
          return doublePressBackBotton.doubleBack();
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        // backgroundColor: Color(0xff202020),
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            backgroundColor: kPrimaryColor,
            icon: Icon(
              Icons.home_outlined,
              color: Colors.white,
            ),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            backgroundColor: kPrimaryColor,
            icon: Icon(
              Icons.category,
              color: Colors.white,
            ),
            label: 'Category',
          ),
          BottomNavigationBarItem(
            backgroundColor: kPrimaryColor,
            icon: ImageIcon(
              AssetImage(AppImages.phonecall),
              color: Colors.white,
            ),
            label: 'Invite',
          ),
          BottomNavigationBarItem(
            backgroundColor: kPrimaryColor,
            icon: Icon(
              Icons.person_outlined,
              color: Colors.white,
            ),
            label: 'Account',
          ),
        ],
        // fixedColor: Colors.black,
        currentIndex: _selectedIndex,
        // selectedItemColor: Color(0xffF9C000),
        onTap: _onItemTapped,
        showUnselectedLabels: true,
        selectedItemColor: kPrimaryLightColor,
        // unselectedItemColor: Color(0xff737373),

        unselectedLabelStyle: GoogleFonts.lato(color: Colors.white),
        selectedLabelStyle: GoogleFonts.lato(color: Colors.white),
      ),
    );

    // );
  }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • in your init state you can control int _selectedIndex = 0; (example: if you want your user to go to the 2nd page set state _selectedIndex= 2) – Tasnuva Tavasum oshin Jan 02 '22 at 04:23
  • firstly thanks for quick response I gave this default route when i am starting app but i want give different route when user is coming post successful login i want same index when app is starting but I want to change index when cx coming via login page. – Ashwani Bhardwaj Jan 02 '22 at 07:51
  • You can do it via constructor – Tasnuva Tavasum oshin Jan 02 '22 at 08:20
  • i tried but can I get an example that based on next page route how can i construct like if there can be 4 or more me i just required 2. home screen and account setting but based on route how can i make a constructer – Ashwani Bhardwaj Jan 02 '22 at 08:35
  • For remaining same index you should work on AutomaticKeepAliveClientMixin – Developer Jan 02 '22 at 08:35
  • 1
    thanks @Vaidarbhi I will implement but as I get to know it is useful if I don't want to reload the already loaded page in homescreen attached to bottom nav bar. but how can i route exactly to 4th index of bottom nav bar from any another screen – Ashwani Bhardwaj Jan 02 '22 at 08:47

0 Answers0