From 06f879b57ed40a192e4fafd1b6031c65754fdc5e Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 6 Feb 2017 22:15:36 -0800 Subject: [PATCH] Add ScrollController.initialScrollOffset (#7916) Also, move the creation of the ScrollPosition to ScrollController. Finally, remove TestScrollable in favor of CustomScrollView, which is the production version of this widget. --- .../lib/src/widgets/scroll_controller.dart | 28 ++++ .../lib/src/widgets/scroll_position.dart | 28 +--- .../flutter/lib/src/widgets/scrollable.dart | 17 +- packages/flutter/test/widgets/block_test.dart | 4 +- .../widgets/overscroll_indicator_test.dart | 30 ++-- .../test/widgets/scroll_controller_test.dart | 35 ++++ ...crollable_custom_scroll_behavior_test.dart | 33 ++-- .../flutter/test/widgets/scrollable_test.dart | 4 +- .../widgets/sliver_fill_remaining_test.dart | 4 +- .../widgets/slivers_appbar_floating_test.dart | 14 +- .../widgets/slivers_appbar_pinned_test.dart | 11 +- .../slivers_appbar_scrolling_test.dart | 8 +- .../test/widgets/slivers_evil_test.dart | 154 +++++++++--------- .../test/widgets/slivers_protocol_test.dart | 5 +- .../flutter/test/widgets/test_widgets.dart | 42 ----- 15 files changed, 207 insertions(+), 210 deletions(-) diff --git a/packages/flutter/lib/src/widgets/scroll_controller.dart b/packages/flutter/lib/src/widgets/scroll_controller.dart index f46accca9d..9f168ee02c 100644 --- a/packages/flutter/lib/src/widgets/scroll_controller.dart +++ b/packages/flutter/lib/src/widgets/scroll_controller.dart @@ -10,6 +10,17 @@ import 'package:meta/meta.dart'; import 'scroll_position.dart'; class ScrollController { + ScrollController({ + this.initialScrollOffset, + }); + + /// The initial value to use for [offset]. + /// + /// If [initialScrollOffset] is non-null, new [ScrollPosition] objects that + /// are created and attached to this controller will have their offset + /// initialized to this value. + final double initialScrollOffset; + final List _positions = []; double get offset { @@ -87,4 +98,21 @@ class ScrollController { assert(_positions.contains(position)); _positions.remove(position); } + + static ScrollPosition createDefaultScrollPosition(ScrollPhysics physics, AbstractScrollState state, ScrollPosition oldPosition) { + return new ScrollPosition( + physics: physics, + state: state, + oldPosition: oldPosition, + ); + } + + ScrollPosition createScrollPosition(ScrollPhysics physics, AbstractScrollState state, ScrollPosition oldPosition) { + return new ScrollPosition( + physics: physics, + state: state, + offset: initialScrollOffset, + oldPosition: oldPosition, + ); + } } diff --git a/packages/flutter/lib/src/widgets/scroll_position.dart b/packages/flutter/lib/src/widgets/scroll_position.dart index 50ad9a4a8c..0a88d75f90 100644 --- a/packages/flutter/lib/src/widgets/scroll_position.dart +++ b/packages/flutter/lib/src/widgets/scroll_position.dart @@ -39,12 +39,6 @@ abstract class ScrollPhysics { ScrollPhysics applyTo(ScrollPhysics parent); - ScrollPosition createScrollPosition(ScrollPhysics physics, AbstractScrollState state, ScrollPosition oldPosition) { - if (parent == null) - return new ScrollPosition(physics, state, oldPosition); - return parent.createScrollPosition(physics, state, oldPosition); - } - /// Used by [DragScrollActivity] and other user-driven activities to /// convert an offset in logical pixels as provided by the [DragUpdateDetails] /// into a delta to apply using [setPixels]. @@ -101,18 +95,6 @@ abstract class ScrollPhysics { Tolerance get tolerance => parent?.tolerance ?? _kDefaultTolerance; - @mustCallSuper - bool shouldUpdateScrollPosition(@checked ScrollPhysics other) { - if ((parent == null) != (other.parent == null)) - return true; - if (parent == null) { - assert(other.parent == null); - return false; - } - return parent.runtimeType != other.parent.runtimeType - || parent.shouldUpdateScrollPosition(other.parent); - } - @override String toString() { if (parent == null) @@ -122,10 +104,16 @@ abstract class ScrollPhysics { } class ScrollPosition extends ViewportOffset { - ScrollPosition(this.physics, this.state, ScrollPosition oldPosition) { + ScrollPosition({ + @required this.physics, + @required this.state, + double offset: 0.0, + ScrollPosition oldPosition, + }) : _pixels = offset ?? 0.0 { assert(physics != null); assert(state != null); assert(state.vsync != null); + assert(pixels != null); if (oldPosition != null) absorb(oldPosition); if (activity == null) @@ -140,7 +128,7 @@ class ScrollPosition extends ViewportOffset { @override double get pixels => _pixels; - double _pixels = 0.0; + double _pixels; Future ensureVisible(RenderObject object, { double alignment: 0.0, diff --git a/packages/flutter/lib/src/widgets/scrollable.dart b/packages/flutter/lib/src/widgets/scrollable.dart index b2a366767a..9e1b3610d1 100644 --- a/packages/flutter/lib/src/widgets/scrollable.dart +++ b/packages/flutter/lib/src/widgets/scrollable.dart @@ -128,17 +128,20 @@ class Scrollable2State extends State with TickerProviderStateMixin ScrollPhysics physics = _configuration.getScrollPhysics(context); if (config.physics != null) physics = config.physics.applyTo(physics); + final ScrollController controller = config.controller; final ScrollPosition oldPosition = position; if (oldPosition != null) { - config.controller?.detach(oldPosition); + controller?.detach(oldPosition); // It's important that we not dispose the old position until after the // viewport has had a chance to unregister its listeners from the old // position. So, schedule a microtask to do it. scheduleMicrotask(oldPosition.dispose); } - _position = physics.createScrollPosition(physics, this, oldPosition); + + _position = controller?.createScrollPosition(physics, this, oldPosition) + ?? ScrollController.createDefaultScrollPosition(physics, this, oldPosition); assert(position != null); - config.controller?.attach(position); + controller?.attach(position); } @override @@ -148,12 +151,8 @@ class Scrollable2State extends State with TickerProviderStateMixin } bool _shouldUpdatePosition(Scrollable2 oldConfig) { - if (config.physics == oldConfig.physics) - return false; - if ((config.physics == null) != (oldConfig.physics == null)) - return true; - return config.physics.runtimeType != oldConfig.physics.runtimeType - || config.physics.shouldUpdateScrollPosition(oldConfig.physics); + return config.physics?.runtimeType != oldConfig.physics?.runtimeType + || config.controller?.runtimeType != config.controller?.runtimeType; } @override diff --git a/packages/flutter/test/widgets/block_test.dart b/packages/flutter/test/widgets/block_test.dart index 9c49ed1cf2..7e1090fc58 100644 --- a/packages/flutter/test/widgets/block_test.dart +++ b/packages/flutter/test/widgets/block_test.dart @@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; -import 'test_widgets.dart'; - final Key blockKey = new Key('test'); void main() { @@ -132,7 +130,7 @@ void main() { new Container(), ]); - await tester.pumpWidget(new TestScrollable( + await tester.pumpWidget(new CustomScrollView( slivers: [ new SliverList( delegate: delegate, diff --git a/packages/flutter/test/widgets/overscroll_indicator_test.dart b/packages/flutter/test/widgets/overscroll_indicator_test.dart index 31ab8ad954..10b22e0d8a 100644 --- a/packages/flutter/test/widgets/overscroll_indicator_test.dart +++ b/packages/flutter/test/widgets/overscroll_indicator_test.dart @@ -9,7 +9,6 @@ import 'package:flutter/widgets.dart'; import 'package:flutter/rendering.dart'; import '../rendering/mock_canvas.dart'; -import 'test_widgets.dart'; final Matcher doesNotOverscroll = isNot(paints..circle()); @@ -25,7 +24,7 @@ Future slowDrag(WidgetTester tester, Point start, Offset offset) async { void main() { testWidgets('Overscroll indicator color', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( + new CustomScrollView( slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 2000.0)), ], @@ -58,7 +57,7 @@ void main() { testWidgets('Overscroll indicator changes side when you drag on the other side', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( + new CustomScrollView( slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 2000.0)), ], @@ -93,7 +92,7 @@ void main() { testWidgets('Overscroll indicator changes side when you shift sides', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( + new CustomScrollView( slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 2000.0)), ], @@ -126,8 +125,7 @@ void main() { group('Flipping direction of scrollable doesn\'t change overscroll behavior', () { testWidgets('down', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 20.0)), ], @@ -143,8 +141,8 @@ void main() { testWidgets('up', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.up, + new CustomScrollView( + reverse: true, slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 20.0)), ], @@ -161,8 +159,7 @@ void main() { testWidgets('Overscroll in both directions', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 20.0)), ], @@ -181,8 +178,8 @@ void main() { testWidgets('Overscroll horizontally', (WidgetTester tester) async { await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.right, + new CustomScrollView( + scrollDirection: Axis.horizontal, slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 20.0)), ], @@ -206,8 +203,9 @@ void main() { await tester.pumpWidget( new ScrollConfiguration2( behavior: new TestScrollBehavior1(), - child: new TestScrollable( - axisDirection: AxisDirection.left, + child: new CustomScrollView( + scrollDirection: Axis.horizontal, + reverse: true, slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 20.0)), ], @@ -223,8 +221,8 @@ void main() { await tester.pumpWidget( new ScrollConfiguration2( behavior: new TestScrollBehavior2(), - child: new TestScrollable( - axisDirection: AxisDirection.right, + child: new CustomScrollView( + scrollDirection: Axis.horizontal, slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 20.0)), ], diff --git a/packages/flutter/test/widgets/scroll_controller_test.dart b/packages/flutter/test/widgets/scroll_controller_test.dart index 81653ce7f4..58249a7726 100644 --- a/packages/flutter/test/widgets/scroll_controller_test.dart +++ b/packages/flutter/test/widgets/scroll_controller_test.dart @@ -108,4 +108,39 @@ void main() { expect(controller2.offset, equals(432.0)); expect(realOffset(), equals(controller2.offset)); }); + + testWidgets('ScrollController control test', (WidgetTester tester) async { + ScrollController controller = new ScrollController( + initialScrollOffset: 209.0, + ); + + await tester.pumpWidget(new GridView.count( + crossAxisCount: 4, + controller: controller, + children: kStates.map((String state) => new Text(state)).toList(), + )); + + double realOffset() { + return tester.state(find.byType(Scrollable2)).position.pixels; + } + + expect(controller.offset, equals(209.0)); + expect(realOffset(), equals(controller.offset)); + + controller.jumpTo(105.0); + + await tester.pump(); + + expect(controller.offset, equals(105.0)); + expect(realOffset(), equals(controller.offset)); + + await tester.pumpWidget(new GridView.count( + crossAxisCount: 2, + controller: controller, + children: kStates.map((String state) => new Text(state)).toList(), + )); + + expect(controller.offset, equals(105.0)); + expect(realOffset(), equals(controller.offset)); + }); } diff --git a/packages/flutter/test/widgets/scrollable_custom_scroll_behavior_test.dart b/packages/flutter/test/widgets/scrollable_custom_scroll_behavior_test.dart index d3d3d8aaf6..8eefbed6e9 100644 --- a/packages/flutter/test/widgets/scrollable_custom_scroll_behavior_test.dart +++ b/packages/flutter/test/widgets/scrollable_custom_scroll_behavior_test.dart @@ -6,14 +6,18 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; -import 'test_widgets.dart'; - class TestScrollPosition extends ScrollPosition { - TestScrollPosition( - TestScrollPhysics physics, + TestScrollPosition({ + ScrollPhysics physics, AbstractScrollState state, ScrollPosition oldPosition, - ) : _pixels = 100.0, super(physics, state, oldPosition); + }) : _pixels = 100.0, super( + physics: physics, + state: state, + oldPosition: oldPosition, + ) { + assert(physics is TestScrollPhysics); + } @override TestScrollPhysics get physics => super.physics; @@ -80,18 +84,23 @@ class TestScrollPosition extends ScrollPosition { } class TestScrollPhysics extends ScrollPhysics { - const TestScrollPhysics({ ScrollPhysics parent, this.extentMultiplier }) : super(parent); + const TestScrollPhysics({ this.extentMultiplier, ScrollPhysics parent }) : super(parent); final double extentMultiplier; @override - TestScrollPhysics applyTo(ScrollPhysics parent) { - return new TestScrollPhysics(parent: parent, extentMultiplier: extentMultiplier); + ScrollPhysics applyTo(ScrollPhysics parent) { + return new TestScrollPhysics( + extentMultiplier: extentMultiplier, + parent: parent, + ); } +} +class TestScrollController extends ScrollController { @override ScrollPosition createScrollPosition(ScrollPhysics physics, AbstractScrollState state, ScrollPosition oldPosition) { - return new TestScrollPosition(physics, state, oldPosition); + return new TestScrollPosition(physics: physics, state: state, oldPosition: oldPosition); } } @@ -120,7 +129,8 @@ void main() { testWidgets('Changing the scroll behavior dynamically', (WidgetTester tester) async { await tester.pumpWidget(new ScrollConfiguration2( behavior: new TestScrollBehavior(1.0), - child: new TestScrollable( + child: new CustomScrollView( + controller: new TestScrollController(), slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 2000.0)), ], @@ -131,7 +141,8 @@ void main() { expect(state.position.getMetrics().extentInside, 1.0); await tester.pumpWidget(new ScrollConfiguration2( behavior: new TestScrollBehavior(2.0), - child: new TestScrollable( + child: new CustomScrollView( + controller: new TestScrollController(), slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 2000.0)), ], diff --git a/packages/flutter/test/widgets/scrollable_test.dart b/packages/flutter/test/widgets/scrollable_test.dart index 23b46f358f..1dade3ccfa 100644 --- a/packages/flutter/test/widgets/scrollable_test.dart +++ b/packages/flutter/test/widgets/scrollable_test.dart @@ -6,14 +6,12 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; -import 'test_widgets.dart'; - Future pumpTest(WidgetTester tester, TargetPlatform platform) async { await tester.pumpWidget(new MaterialApp( theme: new ThemeData( platform: platform, ), - home: new TestScrollable( + home: new CustomScrollView( slivers: [ new SliverToBoxAdapter(child: new SizedBox(height: 2000.0)), ], diff --git a/packages/flutter/test/widgets/sliver_fill_remaining_test.dart b/packages/flutter/test/widgets/sliver_fill_remaining_test.dart index a540e6f475..1f9af8ccd2 100644 --- a/packages/flutter/test/widgets/sliver_fill_remaining_test.dart +++ b/packages/flutter/test/widgets/sliver_fill_remaining_test.dart @@ -5,8 +5,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; -import 'test_widgets.dart'; - void main() { testWidgets('SliverFillRemaining control test', (WidgetTester tester) async { List children = new List.generate(20, (int i) { @@ -14,7 +12,7 @@ void main() { }); await tester.pumpWidget( - new TestScrollable( + new CustomScrollView( slivers: [ new SliverFill( delegate: new SliverChildListDelegate(children), diff --git a/packages/flutter/test/widgets/slivers_appbar_floating_test.dart b/packages/flutter/test/widgets/slivers_appbar_floating_test.dart index c3df9bf8ec..1fc17565e4 100644 --- a/packages/flutter/test/widgets/slivers_appbar_floating_test.dart +++ b/packages/flutter/test/widgets/slivers_appbar_floating_test.dart @@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; -import 'test_widgets.dart'; - void verifyPaintPosition(GlobalKey key, Offset ideal, bool visible) { RenderSliver target = key.currentContext.findRenderObject(); expect(target.parent, new isInstanceOf()); @@ -28,8 +26,7 @@ void main() { testWidgets('Sliver appbars - floating - scroll offset doesn\'t change', (WidgetTester tester) async { const double bigHeight = 1000.0; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(height: bigHeight), new SliverPersistentHeader(delegate: new TestDelegate(), floating: true), @@ -56,8 +53,7 @@ void main() { const double bigHeight = 1000.0; GlobalKey key1, key2, key3; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey(), height: bigHeight), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: delegate, floating: true), @@ -126,8 +122,7 @@ void main() { const double bigHeight = 1000.0; GlobalKey key1, key2, key3; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey(), height: bigHeight), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: delegate, floating: true), @@ -159,8 +154,7 @@ void main() { const double bigHeight = 1000.0; GlobalKey key1, key2, key3; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey(), height: bigHeight), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: delegate, floating: true), diff --git a/packages/flutter/test/widgets/slivers_appbar_pinned_test.dart b/packages/flutter/test/widgets/slivers_appbar_pinned_test.dart index f01845d569..4d4c2946f5 100644 --- a/packages/flutter/test/widgets/slivers_appbar_pinned_test.dart +++ b/packages/flutter/test/widgets/slivers_appbar_pinned_test.dart @@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; -import 'test_widgets.dart'; - void verifyPaintPosition(GlobalKey key, Offset ideal, bool visible) { RenderSliver target = key.currentContext.findRenderObject(); expect(target.parent, new isInstanceOf()); @@ -29,8 +27,7 @@ void main() { const double bigHeight = 550.0; GlobalKey key1, key2, key3, key4, key5; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey(), height: bigHeight), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: new TestDelegate(), pinned: true), @@ -63,8 +60,7 @@ void main() { const double bigHeight = 550.0; GlobalKey key1, key2, key3, key4, key5; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey(), height: bigHeight), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: new TestDelegate(), pinned: true), @@ -153,8 +149,7 @@ void main() { const double bigHeight = 650.0; GlobalKey key1, key2, key3, key4, key5; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey(), height: bigHeight), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: new TestDelegate(), pinned: true), diff --git a/packages/flutter/test/widgets/slivers_appbar_scrolling_test.dart b/packages/flutter/test/widgets/slivers_appbar_scrolling_test.dart index a1d66e5c54..bff1619267 100644 --- a/packages/flutter/test/widgets/slivers_appbar_scrolling_test.dart +++ b/packages/flutter/test/widgets/slivers_appbar_scrolling_test.dart @@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; -import 'test_widgets.dart'; - void verifyPaintPosition(GlobalKey key, Offset ideal) { RenderObject target = key.currentContext.findRenderObject(); expect(target.parent, new isInstanceOf()); @@ -20,8 +18,7 @@ void main() { testWidgets('Sliver appbars - scrolling', (WidgetTester tester) async { GlobalKey key1, key2, key3, key4, key5; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey()), new SliverPersistentHeader(key: key2 = new GlobalKey(), delegate: new TestDelegate()), @@ -54,8 +51,7 @@ void main() { GlobalKey key = new GlobalKey(); TestDelegate delegate = new TestDelegate(); await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(), new SliverPersistentHeader(key: key, delegate: delegate), diff --git a/packages/flutter/test/widgets/slivers_evil_test.dart b/packages/flutter/test/widgets/slivers_evil_test.dart index 68b263436b..b9c34d1089 100644 --- a/packages/flutter/test/widgets/slivers_evil_test.dart +++ b/packages/flutter/test/widgets/slivers_evil_test.dart @@ -7,8 +7,6 @@ import 'package:flutter/rendering.dart'; import 'package:flutter/physics.dart'; import 'package:flutter/material.dart'; -import 'test_widgets.dart'; - class TestSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate { TestSliverPersistentHeaderDelegate(this._maxExtent); @@ -53,11 +51,11 @@ class TestScrollPhysics extends ClampingScrollPhysics { } class TestViewportScrollPosition extends ScrollPosition { - TestViewportScrollPosition( + TestViewportScrollPosition({ ScrollPhysics physics, AbstractScrollState state, ScrollPosition oldPosition, - ) : super(physics, state, oldPosition); + }) : super(physics: physics, state: state, oldPosition: oldPosition); @override bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) { @@ -74,79 +72,85 @@ void main() { new ScrollConfiguration2( behavior: new TestBehavior(), child: new Scrollbar2( - child: new TestScrollable( + child: new Scrollable2( axisDirection: AxisDirection.down, - center: centerKey, - anchor: 0.25, physics: const TestScrollPhysics(), - slivers: [ - new SliverToBoxAdapter(child: new Container(height: 5.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), pinned: true), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPadding( - padding: new EdgeInsets.all(50.0), - child: new SliverToBoxAdapter(child: new Container(height: 520.0)), - ), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(key: centerKey, child: new Container(height: 520.0)), // ------------------------ CENTER ------------------------ - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), pinned: true), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPadding( - padding: new EdgeInsets.all(50.0), - child: new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), - ), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), - new SliverToBoxAdapter(child: new Container(height: 5.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), - new SliverToBoxAdapter(child: new Container(height: 5.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), - new SliverToBoxAdapter(child: new Container(height: 5.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true), - new SliverToBoxAdapter(child: new Container(height: 5.0)), - new SliverList( - delegate: new SliverChildListDelegate([ - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - new Container(height: 50.0), - ]), - ), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), - new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), - new SliverPadding( - padding: new EdgeInsets.symmetric(horizontal: 50.0), - child: new SliverToBoxAdapter(child: new Container(height: 520.0)), - ), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(child: new Container(height: 520.0)), - new SliverToBoxAdapter(child: new Container(height: 5.0)), - ], + viewportBuilder: (BuildContext context, ViewportOffset offset) { + return new Viewport2( + axisDirection: AxisDirection.down, + anchor: 0.25, + offset: offset, + center: centerKey, + slivers: [ + new SliverToBoxAdapter(child: new Container(height: 5.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), pinned: true), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPadding( + padding: new EdgeInsets.all(50.0), + child: new SliverToBoxAdapter(child: new Container(height: 520.0)), + ), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(key: centerKey, child: new Container(height: 520.0)), // ------------------------ CENTER ------------------------ + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), pinned: true), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPadding( + padding: new EdgeInsets.all(50.0), + child: new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), + ), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), + new SliverToBoxAdapter(child: new Container(height: 5.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), + new SliverToBoxAdapter(child: new Container(height: 5.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), + new SliverToBoxAdapter(child: new Container(height: 5.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0), pinned: true), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(150.0), floating: true), + new SliverToBoxAdapter(child: new Container(height: 5.0)), + new SliverList( + delegate: new SliverChildListDelegate([ + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + new Container(height: 50.0), + ]), + ), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), + new SliverPersistentHeader(delegate: new TestSliverPersistentHeaderDelegate(250.0)), + new SliverPadding( + padding: new EdgeInsets.symmetric(horizontal: 50.0), + child: new SliverToBoxAdapter(child: new Container(height: 520.0)), + ), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(child: new Container(height: 520.0)), + new SliverToBoxAdapter(child: new Container(height: 5.0)), + ], + ); + }, ), ), ), diff --git a/packages/flutter/test/widgets/slivers_protocol_test.dart b/packages/flutter/test/widgets/slivers_protocol_test.dart index 78e85a30ac..3b36c01a43 100644 --- a/packages/flutter/test/widgets/slivers_protocol_test.dart +++ b/packages/flutter/test/widgets/slivers_protocol_test.dart @@ -8,8 +8,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; -import 'test_widgets.dart'; - void verifyPaintPosition(GlobalKey key, Offset ideal) { RenderObject target = key.currentContext.findRenderObject(); expect(target.parent, new isInstanceOf()); @@ -22,8 +20,7 @@ void main() { testWidgets('Sliver protocol', (WidgetTester tester) async { GlobalKey key1, key2, key3, key4, key5; await tester.pumpWidget( - new TestScrollable( - axisDirection: AxisDirection.down, + new CustomScrollView( slivers: [ new BigSliver(key: key1 = new GlobalKey()), new OverlappingSliver(key: key2 = new GlobalKey()), diff --git a/packages/flutter/test/widgets/test_widgets.dart b/packages/flutter/test/widgets/test_widgets.dart index 8bd9821253..d8314c90af 100644 --- a/packages/flutter/test/widgets/test_widgets.dart +++ b/packages/flutter/test/widgets/test_widgets.dart @@ -57,45 +57,3 @@ class FlipWidgetState extends State { void flipStatefulWidget(WidgetTester tester) { tester.state(find.byType(FlipWidget)).flip(); } - -class TestScrollable extends StatelessWidget { - TestScrollable({ - Key key, - this.axisDirection: AxisDirection.down, - this.physics, - this.anchor: 0.0, - this.center, - this.slivers: const [], - }) { - assert(slivers != null); - } - - final AxisDirection axisDirection; - - final ScrollPhysics physics; - - final double anchor; - - final Key center; - - final List slivers; - - Axis get axis => axisDirectionToAxis(axisDirection); - - @override - Widget build(BuildContext context) { - return new Scrollable2( - axisDirection: axisDirection, - physics: physics, - viewportBuilder: (BuildContext context, ViewportOffset offset) { - return new Viewport2( - axisDirection: axisDirection, - anchor: anchor, - offset: offset, - center: center, - slivers: slivers, - ); - } - ); - } -}