flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.23k stars 27.5k forks source link

onChange property on DefaultTabController #96962

Closed Velkamhell97 closed 2 years ago

Velkamhell97 commented 2 years ago

actually it can not be possible detect when a TabBarView change the current page with a swipe gesture, it only notifies in the onTap property of TabBar, this is frustrating because we can not sync up these events to trigger any function, so please add this feature in recent versions, i can make my own version of this pull request, but only if i set a custom TabController for TabBar and TabBarView

int _tab = 0;

@override
void initState() {
 super.initState();

 _tabController = TabController(
  length: _tabs.length, 
  vsync: this
 );

 _tabController.animation!.addListener(_tabListener);
}

void _tabListener(){
 if(_tab != _tabController.animation!.value.round()){
  setState(() {
   _tab = _tabController.animation!.value.round();
  });
 }
}

however i need set the listener in the initState of my widget, which means that if i use a state management approach and i need to change tabs lenght, the initState dont execute again and the controller does not update, so would be wonderful if you can add a function on DefaultTabController that handles the swipe page changes, thanks for all!.

darshankawar commented 2 years ago

@Velkamhell97 Check this similar issue which was fixed by adding an interactive example as how to listen for page updates using DefaultTabController and see if it helps to achieve your case.

github-actions[bot] commented 2 years ago

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. If you find this problem please file a new issue with the same description, what happens, logs and the output of 'flutter doctor -v'. All system setups can be slightly different so it's always better to open new issues and reference the related ones. Thanks for your contribution.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.