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

This commit is contained in:
Michel Feinstein 2020-06-05 10:38:03 -03:00 committed by GitHub
parent 94b7ff241e
commit 71a5c61907
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View file

@ -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,
);
}

View file

@ -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