diff --git a/packages/flutter/lib/src/widgets/page_view.dart b/packages/flutter/lib/src/widgets/page_view.dart index 47dde733dc..b4c0135c78 100644 --- a/packages/flutter/lib/src/widgets/page_view.dart +++ b/packages/flutter/lib/src/widgets/page_view.dart @@ -637,6 +637,7 @@ class PageView extends StatefulWidget { this.restorationId, this.clipBehavior = Clip.hardEdge, this.scrollBehavior, + this.padEnds = true, }) : assert(allowImplicitScrolling != null), assert(clipBehavior != null), controller = controller ?? _defaultPageController, @@ -676,6 +677,7 @@ class PageView extends StatefulWidget { this.restorationId, this.clipBehavior = Clip.hardEdge, this.scrollBehavior, + this.padEnds = true, }) : assert(allowImplicitScrolling != null), assert(clipBehavior != null), controller = controller ?? _defaultPageController, @@ -780,6 +782,7 @@ class PageView extends StatefulWidget { this.restorationId, this.clipBehavior = Clip.hardEdge, this.scrollBehavior, + this.padEnds = true, }) : assert(childrenDelegate != null), assert(allowImplicitScrolling != null), assert(clipBehavior != null), @@ -841,6 +844,10 @@ class PageView extends StatefulWidget { final ScrollPhysics? physics; /// Set to false to disable page snapping, useful for custom scroll behavior. + /// + /// If the [padEnds] is false and [PageController.viewportFraction] < 1.0, + /// the page will snap to the beginning of the viewport; otherwise, the page + /// will snap to the center of the viewport. final bool pageSnapping; /// Called whenever the page in the center of the viewport changes. @@ -873,6 +880,17 @@ class PageView extends StatefulWidget { /// modified by default to not apply a [Scrollbar]. final ScrollBehavior? scrollBehavior; + /// Whether to add padding to both ends of the list. + /// + /// If this is set to true and [PageController.viewportFraction] < 1.0, padding will be added + /// such that the first and last child slivers will be in the center of + /// the viewport when scrolled all the way to the start or end, respectively. + /// + /// If [PageController.viewportFraction] >= 1.0, this property has no effect. + /// + /// This property defaults to true and must not be null. + final bool padEnds; + @override _PageViewState createState() => _PageViewState(); } @@ -942,6 +960,7 @@ class _PageViewState extends State { SliverFillViewport( viewportFraction: widget.controller.viewportFraction, delegate: widget.childrenDelegate, + padEnds: widget.padEnds, ), ], ); diff --git a/packages/flutter/test/widgets/page_view_test.dart b/packages/flutter/test/widgets/page_view_test.dart index 84e85e0c81..c680e872de 100644 --- a/packages/flutter/test/widgets/page_view_test.dart +++ b/packages/flutter/test/widgets/page_view_test.dart @@ -985,4 +985,29 @@ void main() { renderObject.paint(context, Offset.zero); expect(context.clipBehavior, equals(Clip.antiAlias)); }); + + testWidgets('PageView.padEnds tests', (WidgetTester tester) async { + Finder viewportFinder() => find.byType(SliverFillViewport, skipOffstage: false); + + // PageView() defaults to true. + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: PageView( + children: const [], + ), + )); + + expect(tester.widget(viewportFinder()).padEnds, true); + + // PageView(padEnds: false) is propagated properly. + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: PageView( + padEnds: false, + children: const [], + ), + )); + + expect(tester.widget(viewportFinder()).padEnds, false); + }); }