From f93daa4f3d14a2e69f8c2c1ec9058a8e07b8816a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 15 Feb 2017 21:01:13 -0800 Subject: [PATCH] Fix issues when correcting the scroll offset (#8211) Previously, the scroll correction code path was untested. This patch introduces a test for the code path and fixes two issues with it. There's likely more work we'll need to do in this area to nail down all the cases. Fixes #8155 --- .../flutter/lib/src/rendering/sliver.dart | 1 - .../lib/src/rendering/sliver_list.dart | 4 +- .../widgets/list_view_correction_test.dart | 56 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 packages/flutter/test/widgets/list_view_correction_test.dart diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index 57292985ba..e79f5579c6 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -481,7 +481,6 @@ class SliverGeometry { assert(visible != null); assert(hasVisualOverflow != null); assert(scrollOffsetCorrection != null); - assert(scrollOffsetCorrection == 0.0); return true; } diff --git a/packages/flutter/lib/src/rendering/sliver_list.dart b/packages/flutter/lib/src/rendering/sliver_list.dart index 98fcb6a5c7..c401a13ed6 100644 --- a/packages/flutter/lib/src/rendering/sliver_list.dart +++ b/packages/flutter/lib/src/rendering/sliver_list.dart @@ -73,8 +73,10 @@ class RenderSliverList extends RenderSliverMultiBoxAdaptor { // We must inform our parent that this sliver cannot fulfill // its contract and that we need a scroll offset correction. geometry = new SliverGeometry( - scrollOffsetCorrection: -childScrollOffset(firstChild), + scrollOffsetCorrection: -scrollOffset, ); + final SliverMultiBoxAdaptorParentData childParentData = firstChild.parentData; + childParentData.layoutOffset = 0.0; return; } final SliverMultiBoxAdaptorParentData childParentData = earliestUsefulChild.parentData; diff --git a/packages/flutter/test/widgets/list_view_correction_test.dart b/packages/flutter/test/widgets/list_view_correction_test.dart new file mode 100644 index 0000000000..243dc6f2de --- /dev/null +++ b/packages/flutter/test/widgets/list_view_correction_test.dart @@ -0,0 +1,56 @@ +// Copyright 2016 The Chromium 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_test/flutter_test.dart'; +import 'package:flutter/material.dart'; + +void main() { + testWidgets('ListView can handle shrinking top elements', (WidgetTester tester) async { + final ScrollController controller = new ScrollController(); + await tester.pumpWidget(new ListView( + controller: controller, + children: [ + new Container(height: 400.0, child: new Text('1')), + new Container(height: 400.0, child: new Text('2')), + new Container(height: 400.0, child: new Text('3')), + new Container(height: 400.0, child: new Text('4')), + new Container(height: 400.0, child: new Text('5')), + new Container(height: 400.0, child: new Text('6')), + ], + )); + + controller.jumpTo(1000.0); + await tester.pump(); + + expect(tester.getTopLeft(find.text('4')).y, equals(200.0)); + + await tester.pumpWidget(new ListView( + controller: controller, + children: [ + new Container(height: 200.0, child: new Text('1')), + new Container(height: 400.0, child: new Text('2')), + new Container(height: 400.0, child: new Text('3')), + new Container(height: 400.0, child: new Text('4')), + new Container(height: 400.0, child: new Text('5')), + new Container(height: 400.0, child: new Text('6')), + ], + )); + + expect(controller.offset, equals(1000.0)); + expect(tester.getTopLeft(find.text('4')).y, equals(200.0)); + + controller.jumpTo(300.0); + await tester.pump(); + + expect(tester.getTopLeft(find.text('2')).y, equals(100.0)); + + controller.jumpTo(50.0); + + await tester.pump(); + + expect(controller.offset, equals(0.0)); + expect(tester.getTopLeft(find.text('2')).y, equals(200.0)); + }); + +}