Unverified Commit 71a5c619 authored by Michel Feinstein's avatar Michel Feinstein Committed by GitHub

Adds physics to the TabBar (#57416) (#57644)

parent 94b7ff24
......@@ -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<int> 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<TabBar> {
dragStartBehavior: widget.dragStartBehavior,
scrollDirection: Axis.horizontal,
controller: _scrollController,
physics: widget.physics,
child: tabBar,
);
}
......
......@@ -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<Tab> tabs = List<Tab>.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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment