diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index 272c11edbcd..02f610813be 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -403,7 +403,16 @@ abstract class RenderViewportBase tester.element(findKey(i)); + Future prepare(double offset) async { + tester.state(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('center'), + slivers: [ + new SliverToBoxAdapter(child: new Container(key: const ValueKey(-6), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(-5), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(-4), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(-3), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(-2), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(-1), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(0), width: 200.0, height: 200.0), key: const ValueKey('center')), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(1), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(2), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(3), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(4), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(5), width: 200.0, height: 200.0)), + new SliverToBoxAdapter(child: new Container(key: const ValueKey(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)); + }); + }); }