Skip to content

Commit efc8379

Browse files
committed
add shorthands for scroll physics subclasses in framework
1 parent 19946f9 commit efc8379

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/scroll_physics.dart‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import 'package:flutter/physics.dart';
2222

2323
import 'binding.dart' show WidgetsBinding;
2424
import 'framework.dart';
25+
import 'list_wheel_scroll_view.dart' show FixedExtentScrollPhysics;
2526
import 'overscroll_indicator.dart';
27+
import 'page_view.dart' show PageScrollPhysics;
2628
import 'scroll_metrics.dart';
2729
import 'scroll_simulation.dart';
2830
import 'view.dart';
@@ -89,6 +91,27 @@ class ScrollPhysics {
8991
/// Creates an object with the default scroll physics.
9092
const ScrollPhysics({this.parent});
9193

94+
/// Creates a [AlwaysScrollableScrollPhysics] instance.
95+
const factory ScrollPhysics.alwaysScrollable() = AlwaysScrollableScrollPhysics;
96+
97+
/// Creates a [BouncingScrollPhysics] instance.
98+
const factory ScrollPhysics.bouncing() = BouncingScrollPhysics;
99+
100+
/// Creates a [ClampingScrollPhysics] instance.
101+
const factory ScrollPhysics.clamping() = ClampingScrollPhysics;
102+
103+
/// Creates a [FixedExtentScrollPhysics] instance.
104+
const factory ScrollPhysics.fixedExtent() = FixedExtentScrollPhysics;
105+
106+
/// Creates a [NeverScrollableScrollPhysics] instance.
107+
const factory ScrollPhysics.neverScrollable() = NeverScrollableScrollPhysics;
108+
109+
/// Creates a [PageScrollPhysics] instance.
110+
const factory ScrollPhysics.page() = PageScrollPhysics;
111+
112+
/// Creates a [RangeMaintainingScrollPhysics] instance.
113+
const factory ScrollPhysics.rangeMaintaining() = RangeMaintainingScrollPhysics;
114+
92115
/// If non-null, determines the default behavior for each method.
93116
///
94117
/// If a subclass of [ScrollPhysics] does not override a method, that subclass

‎packages/flutter/test/widgets/list_wheel_scroll_view_test.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,4 +2059,21 @@ void main() {
20592059
expect(tester.getSize(find.byType(ListWheelViewport)), Size.zero);
20602060
},
20612061
);
2062+
2063+
testWidgets('ScrollPhysics supports shorthands for FixedExtentScrollPhysics', (
2064+
WidgetTester tester,
2065+
) async {
2066+
await tester.pumpWidget(
2067+
ListWheelScrollView(
2068+
itemExtent: 20.0,
2069+
physics: const .fixedExtent(),
2070+
children: <Widget>[for (int i = 0; i < 20; i++) const SizedBox()],
2071+
),
2072+
);
2073+
2074+
expect(
2075+
tester.widget<ListWheelScrollView>(find.byType(ListWheelScrollView)).physics,
2076+
isA<FixedExtentScrollPhysics>(),
2077+
);
2078+
});
20622079
}

‎packages/flutter/test/widgets/scroll_physics_test.dart‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,99 @@ FlutterError
577577
expect(identical(physicsB1, physicsC2), isTrue);
578578
expect(physicsB1.shouldUpdate(physicsC2), isFalse);
579579
});
580+
581+
testWidgets('ScrollPhysics supports shorthands for AlwaysScrollableScrollPhysics', (
582+
WidgetTester tester,
583+
) async {
584+
await tester.pumpWidget(
585+
Directionality(
586+
textDirection: .ltr,
587+
child: ListView(physics: const .alwaysScrollable()),
588+
),
589+
);
590+
591+
expect(
592+
tester.widget<Scrollable>(find.byType(Scrollable)).physics,
593+
isA<AlwaysScrollableScrollPhysics>(),
594+
);
595+
});
596+
597+
testWidgets('ScrollPhysics supports shorthands for BouncingScrollPhysics', (
598+
WidgetTester tester,
599+
) async {
600+
await tester.pumpWidget(
601+
Directionality(
602+
textDirection: .ltr,
603+
child: ListView(physics: const .bouncing()),
604+
),
605+
);
606+
607+
expect(
608+
tester.widget<Scrollable>(find.byType(Scrollable)).physics,
609+
isA<BouncingScrollPhysics>(),
610+
);
611+
});
612+
613+
testWidgets('ScrollPhysics supports shorthands for ClampingScrollPhysics', (
614+
WidgetTester tester,
615+
) async {
616+
await tester.pumpWidget(
617+
Directionality(
618+
textDirection: .ltr,
619+
child: ListView(physics: const .clamping()),
620+
),
621+
);
622+
623+
expect(
624+
tester.widget<Scrollable>(find.byType(Scrollable)).physics,
625+
isA<ClampingScrollPhysics>(),
626+
);
627+
});
628+
629+
testWidgets('ScrollPhysics supports shorthands for NeverScrollableScrollPhysics', (
630+
WidgetTester tester,
631+
) async {
632+
await tester.pumpWidget(
633+
Directionality(
634+
textDirection: .ltr,
635+
child: ListView(physics: const .neverScrollable()),
636+
),
637+
);
638+
639+
expect(
640+
tester.widget<Scrollable>(find.byType(Scrollable)).physics,
641+
isA<NeverScrollableScrollPhysics>(),
642+
);
643+
});
644+
645+
testWidgets('ScrollPhysics supports shorthands for PageScrollPhysics', (
646+
WidgetTester tester,
647+
) async {
648+
await tester.pumpWidget(
649+
Directionality(
650+
textDirection: .ltr,
651+
child: ListView(physics: const .page()),
652+
),
653+
);
654+
655+
expect(tester.widget<Scrollable>(find.byType(Scrollable)).physics, isA<PageScrollPhysics>());
656+
});
657+
658+
testWidgets('ScrollPhysics supports shorthands for RangeMaintainingScrollPhysics', (
659+
WidgetTester tester,
660+
) async {
661+
await tester.pumpWidget(
662+
Directionality(
663+
textDirection: .ltr,
664+
child: ListView(physics: const .rangeMaintaining()),
665+
),
666+
);
667+
668+
expect(
669+
tester.widget<Scrollable>(find.byType(Scrollable)).physics,
670+
isA<RangeMaintainingScrollPhysics>(),
671+
);
672+
});
580673
}
581674

582675
class ReactiveScrollPhysics extends ScrollPhysics {

0 commit comments

Comments
 (0)