Skip to content

Commit 115b953

Browse files
authored
Fix index of TabBarView when decrementing (flutter#88878)
Small change to the calculation of the page to move to when flinging on a TabBarView
1 parent 155d607 commit 115b953

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/flutter/lib/src/material/tabs.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ class _TabBarViewState extends State<TabBarView> {
14401440
_warpUnderwayCount += 1;
14411441
if (notification is ScrollUpdateNotification && !_controller!.indexIsChanging) {
14421442
if ((_pageController.page! - _controller!.index).abs() > 1.0) {
1443-
_controller!.index = _pageController.page!.floor();
1443+
_controller!.index = _pageController.page!.round();
14441444
_currentIndex =_controller!.index;
14451445
}
14461446
_controller!.offset = (_pageController.page! - _controller!.index).clamp(-1.0, 1.0);

packages/flutter/test/material/tabs_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,51 @@ void main() {
600600
expect(find.text('RIGHT CHILD'), findsNothing);
601601
});
602602

603+
// A regression test for https://github.com/flutter/flutter/pull/88878.
604+
testWidgets('TabController notifies the index to change when left flinging', (WidgetTester tester) async {
605+
final List<String> tabs = <String>['A', 'B', 'C'];
606+
late TabController tabController;
607+
608+
Widget buildTabControllerFrame(BuildContext context, TabController controller) {
609+
tabController = controller;
610+
return MaterialApp(
611+
theme: ThemeData(platform: TargetPlatform.iOS),
612+
home: Scaffold(
613+
appBar: AppBar(
614+
title: const Text('tabs'),
615+
bottom: TabBar(
616+
controller: controller,
617+
tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
618+
),
619+
),
620+
body: TabBarView(
621+
controller: controller,
622+
children: const <Widget>[
623+
Center(child: Text('CHILD A')),
624+
Center(child: Text('CHILD B')),
625+
Center(child: Text('CHILD C')),
626+
],
627+
),
628+
),
629+
);
630+
}
631+
632+
await tester.pumpWidget(TabControllerFrame(
633+
builder: buildTabControllerFrame,
634+
length: tabs.length,
635+
initialIndex: tabs.indexOf('C'),
636+
));
637+
expect(tabController.index, tabs.indexOf('C'));
638+
639+
tabController.addListener(() {
640+
final int indexOfB = tabs.indexOf('B');
641+
expect(tabController.index, indexOfB);
642+
});
643+
final Offset flingStart = tester.getCenter(find.text('CHILD C'));
644+
await tester.flingFrom(flingStart, const Offset(600, 0.0), 10000.0);
645+
await tester.pumpAndSettle();
646+
});
647+
603648
// A regression test for https://github.com/flutter/flutter/issues/7133
604649
testWidgets('TabBar fling velocity', (WidgetTester tester) async {
605650
final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];

0 commit comments

Comments
 (0)