Add padEnds option to SliverFillViewport (#48207)
This commit is contained in:
parent
5280dda15b
commit
c90b118247
@ -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,
|
||||
|
@ -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(
|
||||
<Widget>[
|
||||
Container(child: const Text('0')),
|
||||
],
|
||||
addAutomaticKeepAlives: false,
|
||||
addSemanticIndexes: false,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
SliverFillViewport(
|
||||
padEnds: true,
|
||||
viewportFraction: 0.5,
|
||||
delegate: delegate,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final RenderSliver boxWithPadding = tester.renderObject<RenderSliver>(find.byType(SliverFillViewport));
|
||||
expect(boxWithPadding.geometry.paintExtent, equals(600.0));
|
||||
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
SliverFillViewport(
|
||||
padEnds: false,
|
||||
viewportFraction: 0.5,
|
||||
delegate: delegate,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final RenderSliver boxWithoutPadding = tester.renderObject<RenderSliver>(find.byType(SliverFillViewport));
|
||||
expect(boxWithoutPadding.geometry.paintExtent, equals(300.0));
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user