Listen to all events on Flutter's TabController
Adding a listener to the tabController
will only listen to the tab index change but to listen to the changing value (the index in double) you can add the listener to the animation of the tabController
.
Here a full code, I am using flutter_hooks
to create the tabController
.
class TestTabListener extends HookWidget {
const TestTabListener();
@override
Widget build(BuildContext context) {
final tabController = useTabController(initialLength: 3);
tabController.animation!.addListener(() {
if (tabController.animation!.value != tabController.index) {
{
//code
print("runing");
}
}
});
return Scaffold(
body: TabBarView(
controller: tabController,
children: pages,
),
);
}
static final pages = [
Container(color: Colors.red),
Container(color: Colors.black),
Container(color: Colors.blue),
];
}
Be careful using a function here because it will be called multiple times, you might add condition.