diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 255c1bf5b9..39773d259c 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -613,6 +613,7 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { this.dragStartBehavior = DragStartBehavior.start, this.mouseCursor, this.onTap, + this.physics, }) : assert(tabs != null), assert(isScrollable != null), assert(dragStartBehavior != null), @@ -752,6 +753,14 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { /// interfere with the default tap handler. final ValueChanged onTap; + /// How the [TabBar]'s scroll view should respond to user input. + /// + /// For example, determines how the scroll view continues to animate after the + /// user stops dragging the scroll view. + /// + /// Defaults to matching platform conventions. + final ScrollPhysics physics; + /// A size whose height depends on if the tabs have both icons and text. /// /// [AppBar] uses this size to compute its own preferred size. @@ -1115,6 +1124,7 @@ class _TabBarState extends State { dragStartBehavior: widget.dragStartBehavior, scrollDirection: Axis.horizontal, controller: _scrollController, + physics: widget.physics, child: tabBar, ); } diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 7137707368..86b10fa8b1 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -215,6 +215,11 @@ class TestScrollPhysics extends ScrollPhysics { return TestScrollPhysics(parent: buildParent(ancestor)); } + @override + double applyPhysicsToUserOffset(ScrollMetrics position, double offset) { + return offset == 10 ? 20 : offset; + } + static final SpringDescription _kDefaultSpring = SpringDescription.withDampingRatio( mass: 0.5, stiffness: 500.0, @@ -1108,6 +1113,34 @@ void main() { expect(tabController.index, 0); }); + testWidgets('TabBar accepts custom physics', (WidgetTester tester) async { + final List tabs = List.generate(20, (int index) { + return Tab(text: 'TAB #$index'); + }); + + final TabController controller = TabController( + vsync: const TestVSync(), + length: tabs.length, + initialIndex: tabs.length - 1, + ); + + await tester.pumpWidget( + boilerplate( + child: TabBar( + isScrollable: true, + controller: controller, + tabs: tabs, + physics: const TestScrollPhysics(), + ), + ), + ); + + final TabBar tabBar = tester.widget(find.byType(TabBar)); + final double position = tabBar.physics.applyPhysicsToUserOffset(null, 10); + + expect(position, equals(20)); + }); + testWidgets('Scrollable TabBar with a non-zero TabController initialIndex', (WidgetTester tester) async { // This is a regression test for https://github.com/flutter/flutter/issues/9374