Skip to content

Commit 309ff52

Browse files
committed
fix: make tab selection responsive during swipe without scroll jitter
1 parent e3c923f commit 309ff52

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/components/chip_tab_switcher.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class _ChipTabSwitcherState extends State<ChipTabSwitcher> {
106106
TabController? _tabController;
107107
Animation<double>? _tabAnimation;
108108
int _activeIndex = 0;
109+
int _scrollRequestEpoch = 0;
109110
late List<GlobalKey> _tabKeys;
110111

111112
@override
@@ -198,10 +199,14 @@ class _ChipTabSwitcherState extends State<ChipTabSwitcher> {
198199
return 0;
199200
}
200201

202+
final maxIndex = widget.tabs.length - 1;
203+
if (controller.indexIsChanging) {
204+
return controller.index.clamp(0, maxIndex);
205+
}
206+
201207
final animationValue =
202208
controller.animation?.value ?? controller.index.toDouble();
203-
final roundedIndex = animationValue.round();
204-
return roundedIndex.clamp(0, widget.tabs.length - 1);
209+
return animationValue.round().clamp(0, maxIndex);
205210
}
206211

207212
void _handleChipTap(int index) {
@@ -219,7 +224,11 @@ class _ChipTabSwitcherState extends State<ChipTabSwitcher> {
219224
}
220225

221226
void _scrollTabIntoView(int index, {bool animate = true}) {
227+
final requestEpoch = ++_scrollRequestEpoch;
222228
WidgetsBinding.instance.addPostFrameCallback((_) {
229+
if (requestEpoch != _scrollRequestEpoch) {
230+
return;
231+
}
223232
if (!mounted || index < 0 || index >= _tabKeys.length) {
224233
return;
225234
}

0 commit comments

Comments
 (0)