Flutter: How do I detect tab onclick when the current tab is selected?

2.5k views Asked by At

While I can detect tab changes with:

controller.addListener((){
   print('my index is'+ controller.index.toString());
});

How do I detect if the same tab is tapped? I'd like to have an action when user clicks on the home tab to go to the top of the scroll page while user is at home.

2

There are 2 answers

2
KuKu On

You can implement to use onTap method in TabBar class.

TabBar(
   onTap: (index) {
      print(controller.index);
      print(index);
      if (controller.index == index) {
         print('Same Tab Clicked');
      }
   },
   tabs: [
      ...
   ]

});
0
M Karimi On

You can use onItemSelected option in your TabView widget.