I've followed @CoastalB and its worked but the problem is it's take some delay to get index. so I've used two separate listener (animate and tap) for get index.
The current code is implemented with GetX, but you can easily apply this approach with stateful widget:
class GenerateController extends GetxController with GetSingleTickerProviderStateMixin {
late TabController tabController;
RxInt activeTabIndex = 0.obs;
@override
void onInit() {
super.onInit();
tabController = TabController(vsync: this, length: 2);
tabController.addListener(_onTabIndexChanged);
tabController.animation?.addListener(_onTabAnimation);
}
void _onTabIndexChanged() {
// Fires at the end of tab change animation
activeTabIndex.value = tabController.animation!.value.round();
print("Tab index finalized: ${tabController.index}");
}
void _onTabAnimation() {
// Fires during the animation
activeTabIndex.value = tabController.animation!.value.round();
}
@override
void onClose() {
tabController.dispose();
super.onClose();
}
}