{"id":9647,"date":"2024-02-20T12:30:44","date_gmt":"2024-02-20T12:30:44","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9647"},"modified":"2024-02-20T12:30:44","modified_gmt":"2024-02-20T12:30:44","slug":"get-current-tab-index-in-flutter","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/flutter\/get-current-tab-index-in-flutter","title":{"rendered":"How to Get Current Tab Index in Flutter"},"content":{"rendered":"<h3>Using DefaultTabController<\/h3>\n<p>DefaultTabController.of(context).index We can get the current tab index.<\/p>\n<h3>Using a custom TabController<\/h3>\n<p>If you created a TabController manually, either pass it down to the widget that needs the index or use a GlobalKey:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">final _tabController = TabController(length: 3);\r\n\r\n\/\/ Pass to a widget:\r\nMyWidget(tabController: _tabController);\r\n\r\n\/\/ Use a GlobalKey:\r\nGlobalKey&lt;TabBarState&gt; _tabKey = GlobalKey();\r\nint currentIndex = _tabKey.currentState.index;<\/pre>\n<h3>Read the index property<\/h3>\n<p>Use the currentIndex value to perform actions based on the selected tab:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\nint currentIndex = _tabController.index;\r\n<\/pre>\n<h3>Use the index<\/h3>\n<p>Use the currentIndex value to perform actions based on the selected tab:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\nif (currentIndex == 0) {\r\n  \/\/ Perform actions for the first tab\r\n} else if (currentIndex == 1) {\r\n  \/\/ Perform actions for the second tab\r\n}\r\n<\/pre>\n<h2>Additional considerations<\/h2>\n<h3>Listening to tab changes:<\/h3>\n<p>Use the addListener method on TabController to get notified when the tab index changes:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\n_tabController.addListener(() {\r\n  int currentIndex = _tabController.index;\r\n  \/\/ Perform actions based on the new index\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using DefaultTabController DefaultTabController.of(context).index We can get the current tab index. Using a custom TabController If you created a TabController manually, either pass it down to the widget that needs the index or use a GlobalKey: final _tabController = TabController(length: 3); \/\/ Pass to a widget: MyWidget(tabController: _tabController); \/\/ Use a GlobalKey: GlobalKey&lt;TabBarState&gt; _tabKey = GlobalKey(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9648,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-9647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flutter"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9647"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9647"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9647\/revisions"}],"predecessor-version":[{"id":9650,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9647\/revisions\/9650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9648"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}