diff --git a/packages/flutter/lib/src/widgets/sliver_fill.dart b/packages/flutter/lib/src/widgets/sliver_fill.dart index be9c59b7e7..cd06bb767a 100644 --- a/packages/flutter/lib/src/widgets/sliver_fill.dart +++ b/packages/flutter/lib/src/widgets/sliver_fill.dart @@ -30,9 +30,11 @@ class SliverFillViewport extends StatelessWidget { Key key, @required this.delegate, this.viewportFraction = 1.0, + this.padEnds = true, }) : assert(viewportFraction != null), - assert(viewportFraction > 0.0), - super(key: key); + assert(viewportFraction > 0.0), + assert(padEnds != null), + super(key: key); /// The fraction of the viewport that each child should fill in the main axis. /// @@ -41,13 +43,26 @@ class SliverFillViewport extends StatelessWidget { /// the viewport in the main axis. final double viewportFraction; + /// Whether to add padding to both ends of the list. + /// + /// If this is set to true and [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. + /// You may want to set this to false if this [SliverFillViewport] is not the only + /// widget along this main axis, such as in a [CustomScrollView] with multiple + /// children. + /// + /// This option cannot be [null]. If [viewportFraction] >= 1.0, this option has no + /// effect. Defaults to [true]. + final bool padEnds; + /// {@macro flutter.widgets.sliverMultiBoxAdaptor.delegate} final SliverChildDelegate delegate; @override Widget build(BuildContext context) { return _SliverFractionalPadding( - viewportFraction: (1 - viewportFraction).clamp(0, 1) / 2, + viewportFraction: padEnds ? (1 - viewportFraction).clamp(0, 1) / 2 : 0, sliver: _SliverFillViewportRenderObjectWidget( viewportFraction: viewportFraction, delegate: delegate, diff --git a/packages/flutter/test/widgets/sliver_fill_viewport_test.dart b/packages/flutter/test/widgets/sliver_fill_viewport_test.dart index 272e4df046..18427e4fc8 100644 --- a/packages/flutter/test/widgets/sliver_fill_viewport_test.dart +++ b/packages/flutter/test/widgets/sliver_fill_viewport_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; @@ -145,4 +146,50 @@ void main() { ), ); }); + + testWidgets('SliverFillViewport padding test', (WidgetTester tester) async { + final SliverChildListDelegate delegate = SliverChildListDelegate( + [ + Container(child: const Text('0')), + ], + addAutomaticKeepAlives: false, + addSemanticIndexes: false, + ); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: CustomScrollView( + slivers: [ + SliverFillViewport( + padEnds: true, + viewportFraction: 0.5, + delegate: delegate, + ), + ], + ), + ), + ); + + final RenderSliver boxWithPadding = tester.renderObject(find.byType(SliverFillViewport)); + expect(boxWithPadding.geometry.paintExtent, equals(600.0)); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: CustomScrollView( + slivers: [ + SliverFillViewport( + padEnds: false, + viewportFraction: 0.5, + delegate: delegate, + ), + ], + ), + ), + ); + + final RenderSliver boxWithoutPadding = tester.renderObject(find.byType(SliverFillViewport)); + expect(boxWithoutPadding.geometry.paintExtent, equals(300.0)); + }); }