title: Flutter/Dart: Navigator routes always take me to home screen with the selectedIndex = 0
body: <p>I am having an issue within my app where when I navigate to a screen within my HomeScreen, <code>e.g Navigator.of(context).push_________(Screen.routeName)</code> and click back on the app bar, it always takes me to the HomeScreen with the selectedIndex of the HomeScreen equal to 0. This might be an easy solution I'm fairly new to programming. I believe it has something to do with the fact that <code>selectedValue</code> is initialized to 0 in my <strong><code>HomeScreen&lt;State&gt; class</code></strong>.</p>
  
  <p>Here's my code. I think I just need to make that value depends on where I navigate BACK from..(I want to go back to whatever index I Navigated from.   </p>
  
  <p>For example, if I am on <code>_selectedIndex = 2</code>, and I click to go into a screen within <code>_selectedIndex = 2</code> when I click the back button, I want to go back to the HomeScreen but with <code>_selectedIndex = 2</code></p>
  
  <pre><code>class _HomeScreenState extends State&lt;HomeScreen&gt; {
  
    int _selectedIndex = 0; &lt;--------------------------
  static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static List&lt;Widget&gt; _widgetOptions = &lt;Widget&gt;[
  
    Screen1(), //index 0
  
    Screen2(), //index 1
  
    Screen3(), //index 2
  
    Screen4(), //index 3
  
    Screen5(), //index 4
  ];
  
  @override
  Widget build(BuildContext context) {
    final authData = Provider.of&lt;Auth&gt;(context, listen: true);
    final filters = Provider.of&lt;Filters&gt;(context, listen: true);
    return Scaffold(
      appBar: AppBar(
  body: Center(
        child:
           _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.black,
        elevation: 0,
        iconSize: 22,
        items: &lt;BottomNavigationBarItem&gt;[
          BottomNavigationBarItem(
            icon: Icon(Icons.home, 
            color: _selectedIndex == 0 ? Colors.greenAccent : Colors.grey,),
            title: Text(''),
          ),
          BottomNavigationBarItem(
            icon: Icon(FontAwesomeIcons.home, 
            color: _selectedIndex == 1 ? Colors.greenAccent : Colors.grey,),
            title: Text(''),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.home, 
            color: _selectedIndex == 2 ? Colors.greenAccent : Colors.grey,),
            title: Text(''),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.home, 
            color: _selectedIndex == 3 ? Colors.greenAccent : Colors.grey,),
            title: Text(''),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.home, 
            color: _selectedIndex == 4 ? Colors.greenAccent : Colors.grey,),
            title: Text(''),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.greenAccent,
        onTap: _onItemTapped,
      ),
  </code></pre>
  
  <p>I navigate from _selectedIndex = 2 (the third tab) to a screen within _selectedIndex = 2 like this </p>
  
  <pre><code>Navigator.of(context).pushNamed(ChatScreen.routeName);
  </code></pre>
  
  <p>And then from that screen when I click the back button on the appBar which onPressed is defined as </p>
  
  <pre><code>leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: () {
            Navigator.pop(context);
          } ),
  </code></pre>
  
  <p>It takes me back to the HomeScreen but with selectedIndex = 0. I want to go back to selectedIndex = 2.</p>