title: Flutter how to get the value of a stateful widget's field?
body: <p>I have a Flutter app, which uses a <code>BottomNavigationBar</code>. I have made a class called <code>CustomBottomNavBar</code>, which describes my <code>BottomNavigationBar</code>. There, I have an integer field called <code>currentIndex</code>, which is the index of the currently selected icon on the navigation bar. I want to get this value from my main.dart class to display the indexth element of a <code>list&lt;Widget&gt;</code> called tabs, which contains the relative tabs.</p>
  <p>The <code>CustomNavigationBar</code> class:</p>
  <pre class="lang-dart prettyprint-override"><code>    class CustomBottomNavBar extends StatefulWidget {
        @override
        _CustomBottomNavBarState createState() =&gt; _CustomBottomNavBarState();
      }
      
      class _CustomBottomNavBarState extends State&lt;CustomBottomNavBar&gt; {
        int currentIndex = 0;
        @override
        Widget build(BuildContext context) {
          return SizedBox(
                    height: 50,
                                child: BottomNavigationBar(
                      type: BottomNavigationBarType.fixed,
                      selectedFontSize: 11,
                      unselectedFontSize: 11,
                      selectedItemColor: Colors.white,
                      backgroundColor: Colors.grey[850],
                      currentIndex: currentIndex,
                      items: &lt;BottomNavigationBarItem&gt;[
                        BottomNavigationBarItem(
                          activeIcon: Icon(Icons.home),
                          icon: Icon(
                            Icons.home,
                            color: currentIndex == 0 ? Colors.white : Colors.grey[500],
                          ),
                          title: Text(
                            &quot;Home&quot;,
                            style: TextStyle(color: currentIndex == 0 ? Colors.white : Colors.grey[500]),
                          ),
                        ),
                        BottomNavigationBarItem(
                            activeIcon: Icon(Icons.explore),
                            icon: Icon(
                              Icons.explore,
                              color: currentIndex == 1 ? Colors.white : Colors.grey[500],
                            ),
                            title: Text(
                              &quot;Explore&quot;,
                              style: TextStyle(color: currentIndex == 1 ? Colors.white : Colors.grey[500]),
                            )),
                        BottomNavigationBarItem(
                            activeIcon: Icon(Icons.subscriptions),
                            icon: Icon(Icons.subscriptions, color: currentIndex == 2 ? Colors.white : Colors.grey[500]),
                            title: Text(
                              &quot;Subscriptions&quot;,
                              style: TextStyle(color: currentIndex == 2 ? Colors.white : Colors.grey[500]),
                            )),
                        BottomNavigationBarItem(
                            activeIcon: Icon(Icons.mail),
                            icon: Icon(
                              Icons.mail,
                              color: currentIndex == 3 ? Colors.white : Colors.grey[500],
                            ),
                            title: Text(
                              &quot;Inbox&quot;,
                              style: TextStyle(color: currentIndex == 3 ? Colors.white : Colors.grey[500]),
                            )),
                        BottomNavigationBarItem(
                            activeIcon: Icon(Icons.video_library),
                            icon: Icon(
                              Icons.video_library,
                              color: currentIndex == 4 ? Colors.white : Colors.grey[500],
                            ),
                            title: Text(
                              &quot;Library&quot;,
                              style: TextStyle(color: currentIndex == 4 ? Colors.white : Colors.grey[500]),
                            ))
                      ],
                      onTap: (int index) {
                        setState(() {
                          currentIndex = index;
                        });
                      },
                    ),
                  );
        }
  
  }
  </code></pre>
  <p>The main.dart:</p>
  <pre><code>void main() {
    runApp(MyApp());
  }
  
  class MyApp extends StatefulWidget {
    @override
    _MyAppState createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
  
    final List&lt;Widget&gt; tabs = [
      Center(child: Text(&quot;Home&quot;),),
      Center(child: Text(&quot;Explore&quot;),),
      Center(child: Text(&quot;Subscriptions&quot;),),
      Center(child: Text(&quot;Inbox&quot;),),
      Center(child: Text(&quot;Library&quot;),),
    ];
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home:  Scaffold(
              appBar: CustomAppBar(),
              body: tabs[2], //Here I would like to do something like tabs[customBottomNavBar.currentIndex]
              //),
              bottomNavigationBar: CustomBottomNavBar()),
      );
    }
  }
  </code></pre>