mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
ensureVisible works with viewports that have a center child (#11835)
* ensureVisible works with viewports that have a center child * review feedback
This commit is contained in:
parent
067048ab64
commit
268ec4b374
@ -403,7 +403,16 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
|
||||
final GrowthDirection growthDirection = pivotParent.constraints.growthDirection;
|
||||
switch (applyGrowthDirectionToAxisDirection(axisDirection, growthDirection)) {
|
||||
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;
|
||||
break;
|
||||
case AxisDirection.right:
|
||||
@ -415,7 +424,16 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
|
||||
targetMainAxisExtent = bounds.height;
|
||||
break;
|
||||
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;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -612,4 +612,92 @@ void main() {
|
||||
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));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user