Sliver Main Axis Group (#126596)
This widget implements the ability to place slivers one after another in a single ScrollView in a way that all child slivers are drawn within the bounds of the group itself (i.e. SliverPersistentHeaders aren't drawn outside of the scroll extent provided by all of the child slivers). The design document for SliverMainAxisGroup can be found [here](https://docs.google.com/document/d/1e2bdLSYV_Dq2h8aHpF8mda67aOmZocPiMyjCcTTZhTg/edit?resourcekey=0-Xj2X2XA3CAFae22Sv3hAiA). Fixes https://github.com/flutter/flutter/issues/33137. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --------- Co-authored-by: Kate Lovett <katelovett@google.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const SliverMainAxisGroupExampleApp());
|
||||
|
||||
class SliverMainAxisGroupExampleApp extends StatelessWidget {
|
||||
const SliverMainAxisGroupExampleApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('SliverMainAxisGroup Sample')),
|
||||
body: const SliverMainAxisGroupExample(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SliverMainAxisGroupExample extends StatelessWidget {
|
||||
const SliverMainAxisGroupExample({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
SliverMainAxisGroup(
|
||||
slivers: <Widget>[
|
||||
const SliverAppBar(
|
||||
title: Text('Section Title'),
|
||||
expandedHeight: 70.0,
|
||||
pinned: true,
|
||||
),
|
||||
SliverList.builder(
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return Container(
|
||||
color: index.isEven ? Colors.amber[300] : Colors.blue[300],
|
||||
height: 100.0,
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Item $index',
|
||||
style: const TextStyle(fontSize: 24),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
itemCount: 5,
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
color: Colors.cyan,
|
||||
height: 100,
|
||||
child: const Center(
|
||||
child: Text('Another sliver child', style: TextStyle(fontSize: 24)),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Container(
|
||||
height: 1000,
|
||||
decoration: const BoxDecoration(color: Colors.greenAccent),
|
||||
child: const Center(
|
||||
child: Text('Hello World!', style: TextStyle(fontSize: 24)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_api_samples/widgets/sliver/sliver_main_axis_group.0.dart'
|
||||
as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('SliverMainAxisGroup example', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.SliverMainAxisGroupExampleApp(),
|
||||
);
|
||||
|
||||
final RenderSliverMainAxisGroup renderSliverGroup = tester.renderObject(find.byType(SliverMainAxisGroup));
|
||||
expect(renderSliverGroup, isNotNull);
|
||||
|
||||
final RenderSliverPersistentHeader renderAppBar = tester.renderObject<RenderSliverPersistentHeader>(find.byType(SliverAppBar));
|
||||
final RenderSliverList renderSliverList = tester.renderObject<RenderSliverList>(find.byType(SliverList));
|
||||
final RenderSliverToBoxAdapter renderSliverAdapter = tester.renderObject<RenderSliverToBoxAdapter>(find.byType(SliverToBoxAdapter));
|
||||
|
||||
// renderAppBar, renderSliverList, and renderSliverAdapter1 are part of the same sliver group.
|
||||
expect(renderAppBar.geometry!.scrollExtent, equals(70.0));
|
||||
expect(renderSliverList.geometry!.scrollExtent, equals(100.0 * 5));
|
||||
expect(renderSliverAdapter.geometry!.scrollExtent, equals(100.0));
|
||||
expect(renderSliverGroup.geometry!.scrollExtent, equals(70.0 + 100.0 * 5 + 100.0));
|
||||
});
|
||||
}
|
||||
@@ -180,3 +180,160 @@ bool _assertOutOfExtent(double extent) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// A sliver that places multiple sliver children in a linear array along the
|
||||
/// main axis.
|
||||
///
|
||||
/// The layout algorithm lays out slivers one by one. If the sliver is at the top
|
||||
/// of the viewport or above the top, then we pass in a nonzero [SliverConstraints.scrollOffset]
|
||||
/// to inform the sliver at what point along the main axis we should start layout.
|
||||
/// For the slivers that come after it, we compute the amount of space taken up so
|
||||
/// far to be used as the [SliverPhysicalParentData.paintOffset] and the
|
||||
/// [SliverConstraints.remainingPaintExtent] to be passed in as a constraint.
|
||||
///
|
||||
/// Finally, this sliver will also ensure that all child slivers are painted within
|
||||
/// the total scroll extent of the group by adjusting the child's
|
||||
/// [SliverPhysicalParentData.paintOffset] as necessary. This can happen for
|
||||
/// slivers such as [SliverPersistentHeader] which, when pinned, positions itself
|
||||
/// at the top of the [Viewport] regardless of the scroll offset.
|
||||
class RenderSliverMainAxisGroup extends RenderSliver with ContainerRenderObjectMixin<RenderSliver, SliverPhysicalContainerParentData> {
|
||||
@override
|
||||
void setupParentData(RenderObject child) {
|
||||
if (child.parentData is! SliverPhysicalContainerParentData) {
|
||||
child.parentData = SliverPhysicalContainerParentData();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
double childMainAxisPosition(RenderSliver child) {
|
||||
switch (constraints.axisDirection) {
|
||||
case AxisDirection.up:
|
||||
case AxisDirection.down:
|
||||
return (child.parentData! as SliverPhysicalParentData).paintOffset.dy;
|
||||
case AxisDirection.left:
|
||||
case AxisDirection.right:
|
||||
return (child.parentData! as SliverPhysicalParentData).paintOffset.dx;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
double childCrossAxisPosition(RenderSliver child) => 0.0;
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
double offset = 0;
|
||||
double maxPaintExtent = 0;
|
||||
|
||||
RenderSliver? child = firstChild;
|
||||
|
||||
|
||||
while (child != null) {
|
||||
final double beforeOffsetPaintExtent = calculatePaintOffset(
|
||||
constraints,
|
||||
from: 0.0,
|
||||
to: offset,
|
||||
);
|
||||
child.layout(
|
||||
constraints.copyWith(
|
||||
scrollOffset: math.max(0.0, constraints.scrollOffset - offset),
|
||||
cacheOrigin: math.min(0.0, constraints.cacheOrigin + offset),
|
||||
overlap: math.max(0.0, constraints.overlap - beforeOffsetPaintExtent),
|
||||
remainingPaintExtent: constraints.remainingPaintExtent - beforeOffsetPaintExtent,
|
||||
remainingCacheExtent: constraints.remainingCacheExtent - calculateCacheOffset(constraints, from: 0.0, to: offset),
|
||||
precedingScrollExtent: offset + constraints.precedingScrollExtent,
|
||||
),
|
||||
parentUsesSize: true,
|
||||
);
|
||||
final SliverGeometry childLayoutGeometry = child.geometry!;
|
||||
final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
|
||||
switch (constraints.axis) {
|
||||
case Axis.vertical:
|
||||
childParentData.paintOffset = Offset(0.0, beforeOffsetPaintExtent);
|
||||
case Axis.horizontal:
|
||||
childParentData.paintOffset = Offset(beforeOffsetPaintExtent, 0.0);
|
||||
}
|
||||
offset += childLayoutGeometry.scrollExtent;
|
||||
maxPaintExtent += child.geometry!.maxPaintExtent;
|
||||
child = childAfter(child);
|
||||
}
|
||||
|
||||
final double totalScrollExtent = offset;
|
||||
offset = 0.0;
|
||||
child = firstChild;
|
||||
// Second pass to correct out of bound paintOffsets.
|
||||
while (child != null) {
|
||||
final double beforeOffsetPaintExtent = calculatePaintOffset(
|
||||
constraints,
|
||||
from: 0.0,
|
||||
to: offset,
|
||||
);
|
||||
final SliverGeometry childLayoutGeometry = child.geometry!;
|
||||
final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
|
||||
final double remainingExtent = totalScrollExtent - constraints.scrollOffset;
|
||||
if (childLayoutGeometry.paintExtent > remainingExtent) {
|
||||
final double paintCorrection = childLayoutGeometry.paintExtent - remainingExtent;
|
||||
switch (constraints.axis) {
|
||||
case Axis.vertical:
|
||||
childParentData.paintOffset = Offset(0.0, beforeOffsetPaintExtent - paintCorrection);
|
||||
case Axis.horizontal:
|
||||
childParentData.paintOffset = Offset(beforeOffsetPaintExtent - paintCorrection, 0.0);
|
||||
}
|
||||
}
|
||||
offset += child.geometry!.scrollExtent;
|
||||
child = childAfter(child);
|
||||
}
|
||||
geometry = SliverGeometry(
|
||||
scrollExtent: totalScrollExtent,
|
||||
paintExtent: calculatePaintOffset(constraints, from: 0, to: totalScrollExtent),
|
||||
maxPaintExtent: maxPaintExtent,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
RenderSliver? child = lastChild;
|
||||
|
||||
while (child != null) {
|
||||
final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
|
||||
context.paintChild(child, offset + childParentData.paintOffset);
|
||||
child = childBefore(child);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void applyPaintTransform(RenderSliver child, Matrix4 transform) {
|
||||
final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
|
||||
childParentData.applyPaintTransform(transform);
|
||||
}
|
||||
|
||||
@override
|
||||
bool hitTestChildren(SliverHitTestResult result, {required double mainAxisPosition, required double crossAxisPosition}) {
|
||||
RenderSliver? child = firstChild;
|
||||
while (child != null) {
|
||||
final bool isHit = result.addWithAxisOffset(
|
||||
mainAxisPosition: mainAxisPosition,
|
||||
crossAxisPosition: crossAxisPosition,
|
||||
paintOffset: null,
|
||||
mainAxisOffset: childMainAxisPosition(child),
|
||||
crossAxisOffset: childCrossAxisPosition(child),
|
||||
hitTest: child.hitTest,
|
||||
);
|
||||
if (isHit) {
|
||||
return true;
|
||||
}
|
||||
child = childAfter(child);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
void visitChildrenForSemantics(RenderObjectVisitor visitor) {
|
||||
RenderSliver? child = firstChild;
|
||||
while (child != null) {
|
||||
if (child.geometry!.visible) {
|
||||
visitor(child);
|
||||
}
|
||||
child = childAfter(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1561,6 +1561,8 @@ class SliverCrossAxisExpanded extends ParentDataWidget<SliverPhysicalContainerPa
|
||||
/// value to a widget.
|
||||
/// * [SliverConstrainedCrossAxis], which is a [RenderObjectWidget] for setting
|
||||
/// an extent to constrain the widget to.
|
||||
/// * [SliverMainAxisGroup], which is the [RenderObjectWidget] for laying out
|
||||
/// multiple slivers along the main axis.
|
||||
class SliverCrossAxisGroup extends MultiChildRenderObjectWidget {
|
||||
/// Creates a sliver that places sliver children in a linear array along
|
||||
/// the cross axis.
|
||||
@@ -1574,3 +1576,61 @@ class SliverCrossAxisGroup extends MultiChildRenderObjectWidget {
|
||||
return RenderSliverCrossAxisGroup();
|
||||
}
|
||||
}
|
||||
|
||||
/// A sliver that places multiple sliver children in a linear array along
|
||||
/// the main axis, one after another.
|
||||
///
|
||||
/// ## Layout algorithm
|
||||
///
|
||||
/// _This section describes how the framework causes [RenderSliverMainAxisGroup]
|
||||
/// to position its children._
|
||||
///
|
||||
/// Layout for a [RenderSliverMainAxisGroup] has four steps:
|
||||
///
|
||||
/// 1. Keep track of an offset variable which is the total [SliverGeometry.scrollExtent]
|
||||
/// of the slivers laid out so far.
|
||||
/// 2. To determine the constraints for the next sliver child to layout, calculate the
|
||||
/// amount of paint extent occupied from 0.0 to the offset variable and subtract this from
|
||||
/// [SliverConstraints.remainingPaintExtent] minus to use as the child's
|
||||
/// [SliverConstraints.remainingPaintExtent]. For the [SliverConstraints.scrollOffset],
|
||||
/// take the provided constraint's value and subtract out the offset variable, using
|
||||
/// 0.0 if negative.
|
||||
/// 3. Once we finish laying out all the slivers, this offset variable represents
|
||||
/// the total [SliverGeometry.scrollExtent] of the sliver group. Since it is possible
|
||||
/// for specialized slivers to try to paint itself outside of the bounds of the
|
||||
/// sliver group's scroll extent (see [SliverPersistentHeader]), we must do a
|
||||
/// second pass to set a [SliverPhysicalParentData.paintOffset] to make sure it
|
||||
/// is within the bounds of the sliver group.
|
||||
/// 4. Finally, set the [RenderSliverMainAxisGroup.geometry] with the total
|
||||
/// [SliverGeometry.scrollExtent], [SliverGeometry.paintExtent] calculated from
|
||||
/// the constraints and [SliverGeometry.scrollExtent], and [SliverGeometry.maxPaintExtent].
|
||||
///
|
||||
/// {@tool dartpad}
|
||||
/// In this sample the [CustomScrollView] renders a [SliverMainAxisGroup] and a
|
||||
/// [SliverToBoxAdapter] with some content. The [SliverMainAxisGroup] renders a
|
||||
/// [SliverAppBar], [SliverList], and [SliverToBoxAdapter]. Notice that when the
|
||||
/// [SliverMainAxisGroup] goes out of view, so does the pinned [SliverAppBar].
|
||||
///
|
||||
/// ** See code in examples/api/lib/widgets/sliver/sliver_main_axis_group.0.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [SliverPersistentHeader], which is a [RenderObjectWidget] which may require
|
||||
/// adjustment to its [SliverPhysicalParentData.paintOffset] to make it fit
|
||||
/// within the computed [SliverGeometry.scrollExtent] of the [SliverMainAxisGroup].
|
||||
/// * [SliverCrossAxisGroup], which is the [RenderObjectWidget] for laying out
|
||||
/// multiple slivers along the cross axis.
|
||||
class SliverMainAxisGroup extends MultiChildRenderObjectWidget {
|
||||
/// Creates a sliver that places sliver children in a linear array along
|
||||
/// the main axis.
|
||||
const SliverMainAxisGroup({
|
||||
super.key,
|
||||
required List<Widget> slivers,
|
||||
}) : super(children: slivers);
|
||||
|
||||
@override
|
||||
RenderSliverMainAxisGroup createRenderObject(BuildContext context) {
|
||||
return RenderSliverMainAxisGroup();
|
||||
}
|
||||
}
|
||||
|
||||
684
packages/flutter/test/widgets/sliver_main_axis_group_test.dart
Normal file
684
packages/flutter/test/widgets/sliver_main_axis_group_test.dart
Normal file
@@ -0,0 +1,684 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
const double VIEWPORT_HEIGHT = 600;
|
||||
const double VIEWPORT_WIDTH = 300;
|
||||
|
||||
void main() {
|
||||
testWidgets('SliverMainAxisGroup is laid out properly', (WidgetTester tester) async {
|
||||
final List<int> items = List<int>.generate(20, (int i) => i);
|
||||
final ScrollController controller = ScrollController();
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
_buildSliverList(itemMainAxisExtent: 300, items: items, label: (int item) => Text('Group 0 Tile $item')),
|
||||
_buildSliverList(itemMainAxisExtent: 200, items: items, label: (int item) => Text('Group 1 Tile $item')),
|
||||
],
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, 0);
|
||||
|
||||
expect(find.text('Group 0 Tile 0'), findsOneWidget);
|
||||
expect(find.text('Group 0 Tile 1'), findsOneWidget);
|
||||
expect(find.text('Group 0 Tile 2'), findsNothing);
|
||||
|
||||
expect(find.text('Group 1 Tile 0'), findsNothing);
|
||||
|
||||
const double scrollOffset = 19 * 300.0;
|
||||
controller.jumpTo(scrollOffset);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, scrollOffset);
|
||||
expect(find.text('Group 0 Tile 18'), findsNothing);
|
||||
expect(find.text('Group 0 Tile 19'), findsOneWidget);
|
||||
expect(find.text('Group 1 Tile 0'), findsOneWidget);
|
||||
|
||||
final List<RenderSliverList> renderSlivers = tester.renderObjectList<RenderSliverList>(find.byType(SliverList)).toList();
|
||||
final RenderSliverList first = renderSlivers[0];
|
||||
final RenderSliverList second = renderSlivers[1];
|
||||
|
||||
expect(first.geometry!.layoutExtent, equals(300.0));
|
||||
expect(second.geometry!.layoutExtent, equals(300.0));
|
||||
expect(first.geometry!.scrollExtent, equals(20 * 300.0));
|
||||
expect(second.geometry!.scrollExtent, equals(20 * 200.0));
|
||||
|
||||
expect((first.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
expect(first.constraints.scrollOffset, equals(19 * 300.0));
|
||||
expect((second.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(1 * 300.0));
|
||||
|
||||
final RenderSliverMainAxisGroup renderGroup =
|
||||
tester.renderObject<RenderSliverMainAxisGroup>(find.byType(SliverMainAxisGroup));
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(300 * 20 + 200 * 20));
|
||||
});
|
||||
|
||||
testWidgets('SliverMainAxisGroup is laid out properly when reversed', (WidgetTester tester) async {
|
||||
final List<int> items = List<int>.generate(20, (int i) => i);
|
||||
final ScrollController controller = ScrollController();
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
reverse: true,
|
||||
slivers: <Widget>[
|
||||
_buildSliverList(itemMainAxisExtent: 300, items: items, label: (int item) => Text('Group 0 Tile $item')),
|
||||
_buildSliverList(itemMainAxisExtent: 200, items: items, label: (int item) => Text('Group 1 Tile $item')),
|
||||
],
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, 0);
|
||||
|
||||
expect(find.text('Group 0 Tile 0'), findsOneWidget);
|
||||
expect(find.text('Group 0 Tile 1'), findsOneWidget);
|
||||
expect(find.text('Group 0 Tile 2'), findsNothing);
|
||||
|
||||
expect(find.text('Group 1 Tile 0'), findsNothing);
|
||||
|
||||
const double scrollOffset = 19 * 300.0;
|
||||
controller.jumpTo(scrollOffset);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, scrollOffset);
|
||||
expect(find.text('Group 0 Tile 18'), findsNothing);
|
||||
expect(find.text('Group 0 Tile 19'), findsOneWidget);
|
||||
expect(find.text('Group 1 Tile 0'), findsOneWidget);
|
||||
|
||||
final List<RenderSliverList> renderSlivers = tester.renderObjectList<RenderSliverList>(find.byType(SliverList)).toList();
|
||||
final RenderSliverList first = renderSlivers[0];
|
||||
final RenderSliverList second = renderSlivers[1];
|
||||
|
||||
expect(first.geometry!.layoutExtent, equals(300.0));
|
||||
expect(second.geometry!.layoutExtent, equals(300.0));
|
||||
expect(first.geometry!.scrollExtent, equals(20 * 300.0));
|
||||
expect(second.geometry!.scrollExtent, equals(20 * 200.0));
|
||||
|
||||
expect((first.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
expect(first.constraints.scrollOffset, equals(19 * 300.0));
|
||||
expect((second.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(1 * 300.0));
|
||||
|
||||
final RenderSliverMainAxisGroup renderGroup =
|
||||
tester.renderObject<RenderSliverMainAxisGroup>(find.byType(SliverMainAxisGroup));
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(300 * 20 + 200 * 20));
|
||||
});
|
||||
|
||||
testWidgets('SliverMainAxisGroup is laid out properly when horizontal', (WidgetTester tester) async {
|
||||
final List<int> items = List<int>.generate(20, (int i) => i);
|
||||
final ScrollController controller = ScrollController();
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
scrollDirection: Axis.horizontal,
|
||||
slivers: <Widget>[
|
||||
_buildSliverList(itemMainAxisExtent: 300, items: items, label: (int item) => Text('Group 0 Tile $item'), scrollDirection: Axis.horizontal),
|
||||
_buildSliverList(itemMainAxisExtent: 200, items: items, label: (int item) => Text('Group 1 Tile $item'), scrollDirection: Axis.horizontal),
|
||||
],
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, 0);
|
||||
|
||||
expect(find.text('Group 0 Tile 0'), findsOneWidget);
|
||||
expect(find.text('Group 0 Tile 1'), findsNothing);
|
||||
|
||||
expect(find.text('Group 1 Tile 0'), findsNothing);
|
||||
|
||||
const double scrollOffset = 19 * 300.0;
|
||||
controller.jumpTo(scrollOffset);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, scrollOffset);
|
||||
expect(find.text('Group 0 Tile 18'), findsNothing);
|
||||
expect(find.text('Group 0 Tile 19'), findsOneWidget);
|
||||
expect(find.text('Group 1 Tile 0'), findsNothing);
|
||||
|
||||
const double scrollOffset2 = 20 * 300.0;
|
||||
controller.jumpTo(scrollOffset2);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Group 0 Tile 19'), findsNothing);
|
||||
expect(find.text('Group 1 Tile 0'), findsOneWidget);
|
||||
|
||||
final List<RenderSliverList> renderSlivers = tester.renderObjectList<RenderSliverList>(find.byType(SliverList)).toList();
|
||||
final RenderSliverList first = renderSlivers[0];
|
||||
final RenderSliverList second = renderSlivers[1];
|
||||
|
||||
expect(first.geometry!.layoutExtent, equals(0.0));
|
||||
expect(second.geometry!.layoutExtent, equals(300.0));
|
||||
expect(first.geometry!.scrollExtent, equals(20 * 300.0));
|
||||
expect(second.geometry!.scrollExtent, equals(20 * 200.0));
|
||||
|
||||
expect((first.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
expect(first.constraints.scrollOffset, equals(20 * 300.0));
|
||||
expect((second.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
|
||||
final RenderSliverMainAxisGroup renderGroup =
|
||||
tester.renderObject<RenderSliverMainAxisGroup>(find.byType(SliverMainAxisGroup));
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(300 * 20 + 200 * 20));
|
||||
});
|
||||
|
||||
testWidgets('SliverMainAxisGroup is laid out properly when horizontal, reversed', (WidgetTester tester) async {
|
||||
final List<int> items = List<int>.generate(20, (int i) => i);
|
||||
final ScrollController controller = ScrollController();
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
scrollDirection: Axis.horizontal,
|
||||
reverse: true,
|
||||
slivers: <Widget>[
|
||||
_buildSliverList(itemMainAxisExtent: 300, items: items, label: (int item) => Text('Group 0 Tile $item'), scrollDirection: Axis.horizontal),
|
||||
_buildSliverList(itemMainAxisExtent: 200, items: items, label: (int item) => Text('Group 1 Tile $item'), scrollDirection: Axis.horizontal),
|
||||
],
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, 0);
|
||||
|
||||
expect(find.text('Group 0 Tile 0'), findsOneWidget);
|
||||
expect(find.text('Group 0 Tile 1'), findsNothing);
|
||||
|
||||
expect(find.text('Group 1 Tile 0'), findsNothing);
|
||||
|
||||
const double scrollOffset = 19 * 300.0;
|
||||
controller.jumpTo(scrollOffset);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(controller.offset, scrollOffset);
|
||||
expect(find.text('Group 0 Tile 18'), findsNothing);
|
||||
expect(find.text('Group 0 Tile 19'), findsOneWidget);
|
||||
expect(find.text('Group 1 Tile 0'), findsNothing);
|
||||
|
||||
const double scrollOffset2 = 20 * 300.0;
|
||||
controller.jumpTo(scrollOffset2);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Group 0 Tile 19'), findsNothing);
|
||||
expect(find.text('Group 1 Tile 0'), findsOneWidget);
|
||||
|
||||
final List<RenderSliverList> renderSlivers = tester.renderObjectList<RenderSliverList>(find.byType(SliverList)).toList();
|
||||
final RenderSliverList first = renderSlivers[0];
|
||||
final RenderSliverList second = renderSlivers[1];
|
||||
|
||||
expect(first.geometry!.layoutExtent, equals(0.0));
|
||||
expect(second.geometry!.layoutExtent, equals(300.0));
|
||||
expect(first.geometry!.scrollExtent, equals(20 * 300.0));
|
||||
expect(second.geometry!.scrollExtent, equals(20 * 200.0));
|
||||
|
||||
expect((first.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
expect(first.constraints.scrollOffset, equals(20 * 300.0));
|
||||
expect((second.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
|
||||
final RenderSliverMainAxisGroup renderGroup =
|
||||
tester.renderObject<RenderSliverMainAxisGroup>(find.byType(SliverMainAxisGroup));
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(300 * 20 + 200 * 20));
|
||||
});
|
||||
|
||||
testWidgets('Hit test works properly on various parts of SliverMainAxisGroup', (WidgetTester tester) async {
|
||||
final List<int> items = List<int>.generate(20, (int i) => i);
|
||||
final ScrollController controller = ScrollController();
|
||||
|
||||
String? clickedTile;
|
||||
|
||||
int group = 0;
|
||||
int tile = 0;
|
||||
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
_buildSliverList(
|
||||
itemMainAxisExtent: 300,
|
||||
items: items,
|
||||
label: (int item) => tile == item && group == 0
|
||||
? TextButton(
|
||||
onPressed: () => clickedTile = 'Group 0 Tile $item',
|
||||
child: Text('Group 0 Tile $item'),
|
||||
)
|
||||
: Text('Group 0 Tile $item'),
|
||||
),
|
||||
_buildSliverList(
|
||||
items: items,
|
||||
label: (int item) => tile == item && group == 1
|
||||
? TextButton(
|
||||
onPressed: () => clickedTile = 'Group 1 Tile $item',
|
||||
child: Text('Group 1 Tile $item'),
|
||||
)
|
||||
: Text('Group 1 Tile $item'),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byType(TextButton));
|
||||
await tester.pumpAndSettle();
|
||||
expect(clickedTile, equals('Group 0 Tile 0'));
|
||||
|
||||
clickedTile = null;
|
||||
group = 1;
|
||||
tile = 2;
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
_buildSliverList(
|
||||
itemMainAxisExtent: 300,
|
||||
items: items,
|
||||
label: (int item) => tile == item && group == 0
|
||||
? TextButton(
|
||||
onPressed: () => clickedTile = 'Group 0 Tile $item',
|
||||
child: Text('Group 0 Tile $item'),
|
||||
)
|
||||
: Text('Group 0 Tile $item'),
|
||||
),
|
||||
_buildSliverList(
|
||||
items: items,
|
||||
label: (int item) => tile == item && group == 1
|
||||
? TextButton(
|
||||
onPressed: () => clickedTile = 'Group 1 Tile $item',
|
||||
child: Text('Group 1 Tile $item'),
|
||||
)
|
||||
: Text('Group 1 Tile $item'),
|
||||
),
|
||||
]),
|
||||
);
|
||||
controller.jumpTo(300.0 * 20);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byType(TextButton));
|
||||
await tester.pumpAndSettle();
|
||||
expect(clickedTile, equals('Group 1 Tile 2'));
|
||||
});
|
||||
|
||||
testWidgets('applyPaintTransform is implemented properly', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
slivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: Text('first box')),
|
||||
const SliverToBoxAdapter(child: Text('second box')),
|
||||
]),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// localToGlobal calculates offset via applyPaintTransform
|
||||
final RenderBox first = tester.renderObject(find.text('first box')) as RenderBox;
|
||||
final RenderBox second = tester.renderObject(find.text('second box'));
|
||||
expect(first.localToGlobal(Offset.zero), Offset.zero);
|
||||
expect(second.localToGlobal(Offset.zero), Offset(0, first.size.height));
|
||||
});
|
||||
|
||||
testWidgets('visitChildrenForSemantics visits children in the correct order', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: const <Widget>[
|
||||
SliverToBoxAdapter(child: SizedBox(height: 200)),
|
||||
SliverToBoxAdapter(child: SizedBox(height: 300)),
|
||||
SliverToBoxAdapter(child: SizedBox(height: 500)),
|
||||
SliverToBoxAdapter(child: SizedBox(height: 400)),
|
||||
]),
|
||||
);
|
||||
controller.jumpTo(300);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final List<RenderSliver> visitedChildren = <RenderSliver>[];
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject<RenderSliverMainAxisGroup>(find.byType(SliverMainAxisGroup));
|
||||
void visitor(RenderObject child) {
|
||||
visitedChildren.add(child as RenderSliver);
|
||||
}
|
||||
renderGroup.visitChildrenForSemantics(visitor);
|
||||
expect(visitedChildren.length, equals(2));
|
||||
expect(visitedChildren[0].geometry!.scrollExtent, equals(300));
|
||||
expect(visitedChildren[1].geometry!.scrollExtent, equals(500));
|
||||
});
|
||||
|
||||
testWidgets('SliverPinnedPersistentHeader is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
SliverPersistentHeader(
|
||||
delegate: TestDelegate(),
|
||||
pinned: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
// Scroll extent is the total of the box sliver and the sliver persistent header.
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(600.0 + 60.0));
|
||||
controller.jumpTo(620);
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
// Paint extent after header's layout is 60.0, so we must offset by -20.0 to fit within the 40.0 remaining extent.
|
||||
expect(renderHeader.geometry!.paintExtent, equals(60.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-20.0));
|
||||
});
|
||||
|
||||
|
||||
testWidgets('SliverFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
SliverPersistentHeader(
|
||||
delegate: TestDelegate(),
|
||||
floating: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
controller.jumpTo(660.0);
|
||||
await tester.pumpAndSettle();
|
||||
final TestGesture gesture = await tester.startGesture(const Offset(150.0, 300.0));
|
||||
await gesture.moveBy(const Offset(0.0, 40));
|
||||
await tester.pump();
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
// Paint extent after header's layout is 40.0, so no need to correct the paintOffset.
|
||||
expect(renderHeader.geometry!.paintExtent, equals(40.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('SliverPinnedPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
SliverPersistentHeader(
|
||||
delegate: TestDelegate(minExtent: 40.0),
|
||||
pinned: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
controller.jumpTo(630);
|
||||
await tester.pumpAndSettle();
|
||||
// Paint extent of the header is 40.0, so we must provide an offset of -10.0 to make it fit in the 30.0 remaining paint extent of the group.
|
||||
expect(renderHeader.geometry!.paintExtent, equals(40.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-10.0));
|
||||
controller.jumpTo(610);
|
||||
await tester.pumpAndSettle();
|
||||
expect(renderHeader.geometry!.paintExtent, equals(40.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('SliverFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
SliverPersistentHeader(
|
||||
delegate: TestDelegate(minExtent: 40.0),
|
||||
floating: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
|
||||
controller.jumpTo(660);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final TestGesture gesture = await tester.startGesture(const Offset(150.0, 300.0));
|
||||
await gesture.moveBy(const Offset(0.0, 30.0));
|
||||
await tester.pump();
|
||||
// Paint extent after header's layout is 30.0, so no need to correct the paintOffset.
|
||||
expect(renderHeader.geometry!.paintExtent, equals(30.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
// Floating headers should expand to maximum extent as we continue scrolling.
|
||||
await gesture.moveBy(const Offset(0.0, 20.0));
|
||||
await tester.pump();
|
||||
expect(renderHeader.geometry!.paintExtent, equals(50.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('SliverPinnedFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
SliverPersistentHeader(
|
||||
delegate: TestDelegate(minExtent: 40.0),
|
||||
pinned: true,
|
||||
floating: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
|
||||
controller.jumpTo(660);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final TestGesture gesture = await tester.startGesture(const Offset(150.0, 300.0));
|
||||
await gesture.moveBy(const Offset(0.0, 30.0));
|
||||
await tester.pump();
|
||||
// Paint extent after header's layout is 40.0, so we need to adjust by -10.0.
|
||||
expect(renderHeader.geometry!.paintExtent, equals(40.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-10.0));
|
||||
// Pinned floating headers should expand to maximum extent as we continue scrolling.
|
||||
await gesture.moveBy(const Offset(0.0, 20.0));
|
||||
await tester.pump();
|
||||
expect(renderHeader.geometry!.paintExtent, equals(50.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('SliverAppBar with floating: false, pinned: false, snap: false is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
const SliverAppBar(
|
||||
toolbarHeight: 30,
|
||||
expandedHeight: 60,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
|
||||
controller.jumpTo(660);
|
||||
await tester.pumpAndSettle();
|
||||
controller.jumpTo(630);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// At a scroll offset of 630, a normal scrolling header should be out of view.
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
expect(renderHeader.constraints.scrollOffset, equals(630));
|
||||
expect(renderHeader.geometry!.layoutExtent, equals(0.0));
|
||||
});
|
||||
|
||||
testWidgets('SliverAppBar with floating: true, pinned: false, snap: true is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
const SliverAppBar(
|
||||
toolbarHeight: 30,
|
||||
expandedHeight: 60,
|
||||
floating: true,
|
||||
snap: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
|
||||
controller.jumpTo(660);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final TestGesture gesture = await tester.startGesture(const Offset(150.0, 300.0));
|
||||
await gesture.moveBy(const Offset(0.0, 10));
|
||||
await tester.pump();
|
||||
|
||||
// The snap animation does not go through until the gesture is released.
|
||||
expect(renderHeader.geometry!.paintExtent, equals(10));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
|
||||
|
||||
// Once it is released, the header's paint extent becomes the maximum and the group sets an offset of -50.0.
|
||||
await gesture.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(renderHeader.geometry!.paintExtent, equals(60));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-50.0));
|
||||
});
|
||||
|
||||
testWidgets('SliverAppBar with floating: true, pinned: true, snap: true is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
|
||||
final ScrollController controller = ScrollController();
|
||||
await tester.pumpWidget(_buildSliverMainAxisGroup(
|
||||
controller: controller,
|
||||
slivers: <Widget>[
|
||||
const SliverAppBar(
|
||||
toolbarHeight: 30,
|
||||
expandedHeight: 60,
|
||||
floating: true,
|
||||
pinned: true,
|
||||
snap: true,
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 600)),
|
||||
],
|
||||
otherSlivers: <Widget>[
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 2400)),
|
||||
],
|
||||
));
|
||||
await tester.pumpAndSettle();
|
||||
final RenderSliverMainAxisGroup renderGroup = tester.renderObject(find.byType(SliverMainAxisGroup)) as RenderSliverMainAxisGroup;
|
||||
final RenderSliverPersistentHeader renderHeader = tester.renderObject(find.byType(SliverPersistentHeader)) as RenderSliverPersistentHeader;
|
||||
expect(renderGroup.geometry!.scrollExtent, equals(660));
|
||||
|
||||
controller.jumpTo(660);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final TestGesture gesture = await tester.startGesture(const Offset(150.0, 300.0));
|
||||
await gesture.moveBy(const Offset(0.0, 10));
|
||||
await tester.pump();
|
||||
|
||||
expect(renderHeader.geometry!.paintExtent, equals(30.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-20.0));
|
||||
|
||||
// Once we lift the gesture up, the animation should finish.
|
||||
await gesture.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(renderHeader.geometry!.paintExtent, equals(60.0));
|
||||
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-50.0));
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildSliverList({
|
||||
double itemMainAxisExtent = 100,
|
||||
List<int> items = const <int>[],
|
||||
required Widget Function(int) label,
|
||||
Axis scrollDirection = Axis.vertical,
|
||||
}) {
|
||||
return SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(BuildContext context, int i) {
|
||||
return scrollDirection == Axis.vertical
|
||||
? SizedBox(
|
||||
key: ValueKey<int>(items[i]),
|
||||
height: itemMainAxisExtent,
|
||||
child: label(items[i]),
|
||||
)
|
||||
: SizedBox(
|
||||
key: ValueKey<int>(items[i]),
|
||||
width: itemMainAxisExtent,
|
||||
child: label(items[i]));
|
||||
},
|
||||
findChildIndexCallback: (Key key) {
|
||||
final ValueKey<int> valueKey = key as ValueKey<int>;
|
||||
final int index = items.indexOf(valueKey.value);
|
||||
return index == -1 ? null : index;
|
||||
},
|
||||
childCount: items.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSliverMainAxisGroup({
|
||||
required List<Widget> slivers,
|
||||
ScrollController? controller,
|
||||
double viewportHeight = VIEWPORT_HEIGHT,
|
||||
double viewportWidth = VIEWPORT_WIDTH,
|
||||
Axis scrollDirection = Axis.vertical,
|
||||
bool reverse = false,
|
||||
List<Widget> otherSlivers = const <Widget>[],
|
||||
}) {
|
||||
return MaterialApp(
|
||||
home: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Align(
|
||||
alignment: Alignment.topLeft,
|
||||
child: SizedBox(
|
||||
height: viewportHeight,
|
||||
width: viewportWidth,
|
||||
child: CustomScrollView(
|
||||
scrollDirection: scrollDirection,
|
||||
reverse: reverse,
|
||||
controller: controller,
|
||||
slivers: <Widget>[SliverMainAxisGroup(slivers: slivers), ...otherSlivers],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class TestDelegate extends SliverPersistentHeaderDelegate {
|
||||
TestDelegate({ this.maxExtent = 60.0, this.minExtent = 60.0 });
|
||||
|
||||
@override
|
||||
final double maxExtent;
|
||||
|
||||
@override
|
||||
final double minExtent;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
|
||||
return Container(height: maxExtent);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRebuild(TestDelegate oldDelegate) => true;
|
||||
}
|
||||
Reference in New Issue
Block a user