Commit 268ec4b3 authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

ensureVisible works with viewports that have a center child (#11835)

* ensureVisible works with viewports that have a center child

* review feedback
parent 067048ab
...@@ -403,7 +403,16 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix ...@@ -403,7 +403,16 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
final GrowthDirection growthDirection = pivotParent.constraints.growthDirection; final GrowthDirection growthDirection = pivotParent.constraints.growthDirection;
switch (applyGrowthDirectionToAxisDirection(axisDirection, growthDirection)) { switch (applyGrowthDirectionToAxisDirection(axisDirection, growthDirection)) {
case AxisDirection.up: case AxisDirection.up:
leadingScrollOffset = pivot.size.height - bounds.bottom; double offset;
switch (growthDirection) {
case GrowthDirection.forward:
offset = bounds.bottom;
break;
case GrowthDirection.reverse:
offset = bounds.top;
break;
}
leadingScrollOffset = pivot.size.height - offset;
targetMainAxisExtent = bounds.height; targetMainAxisExtent = bounds.height;
break; break;
case AxisDirection.right: case AxisDirection.right:
...@@ -415,7 +424,16 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix ...@@ -415,7 +424,16 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
targetMainAxisExtent = bounds.height; targetMainAxisExtent = bounds.height;
break; break;
case AxisDirection.left: case AxisDirection.left:
leadingScrollOffset = pivot.size.width - bounds.right; double offset;
switch (growthDirection) {
case GrowthDirection.forward:
offset = bounds.right;
break;
case GrowthDirection.reverse:
offset = bounds.left;
break;
}
leadingScrollOffset = pivot.size.width - offset;
targetMainAxisExtent = bounds.width; targetMainAxisExtent = bounds.width;
break; break;
} }
......
...@@ -612,4 +612,92 @@ void main() { ...@@ -612,4 +612,92 @@ void main() {
expect(tester.getBottomRight(findKey(3)).dx, equals(700.0)); expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
}); });
}); });
group('Scrollable with center', () {
testWidgets('ensureVisible', (WidgetTester tester) async {
BuildContext findContext(int i) => tester.element(findKey(i));
Future<Null> prepare(double offset) async {
tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
await tester.pump();
}
await tester.pumpWidget(new Center(
child: new SizedBox(
width: 600.0,
height: 400.0,
child: new Scrollable(
viewportBuilder: (BuildContext context, ViewportOffset offset) {
return new Viewport(
offset: offset,
center: const ValueKey<String>('center'),
slivers: <Widget>[
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(-6), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(-5), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(-4), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(-3), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(-2), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(-1), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(0), width: 200.0, height: 200.0), key: const ValueKey<String>('center')),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(1), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(2), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(3), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(4), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(5), width: 200.0, height: 200.0)),
new SliverToBoxAdapter(child: new Container(key: const ValueKey<int>(6), width: 200.0, height: 200.0)),
],
);
},
),
),
));
await prepare(480.0);
Scrollable.ensureVisible(findContext(3));
await tester.pump();
expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
await prepare(1083.0);
Scrollable.ensureVisible(findContext(6));
await tester.pump();
expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
await prepare(735.0);
Scrollable.ensureVisible(findContext(4), alignment: 1.0);
await tester.pump();
expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
await prepare(123.0);
Scrollable.ensureVisible(findContext(0), alignment: 1.0);
await tester.pump();
expect(tester.getBottomRight(findKey(0)).dy, equals(500.0));
await prepare(523.0);
Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
await tester.pump();
await tester.pump(const Duration(milliseconds: 1020));
expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
await prepare(-480.0);
Scrollable.ensureVisible(findContext(-3));
await tester.pump();
expect(tester.getTopLeft(findKey(-3)).dy, equals(100.0));
await prepare(-1083.0);
Scrollable.ensureVisible(findContext(-6));
await tester.pump();
expect(tester.getTopLeft(findKey(-6)).dy, equals(100.0));
await prepare(-735.0);
Scrollable.ensureVisible(findContext(-4), alignment: 1.0);
await tester.pump();
expect(tester.getBottomRight(findKey(-4)).dy, equals(500.0));
await prepare(-523.0);
Scrollable.ensureVisible(findContext(-3), duration: const Duration(seconds: 1));
await tester.pump();
await tester.pump(const Duration(milliseconds: 1020));
expect(tester.getTopLeft(findKey(-3)).dy, equals(100.0));
});
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment