Skip to content

Commit

Permalink
Fixed TrackingScrollController cleaning offset on position's detach (f…
Browse files Browse the repository at this point in the history
…lutter#11798)

* Fixed TrackingScrollController cleaning stored offset on position's detach

* Fixed format.

* Fixed format.
  • Loading branch information
mogol authored and Hixie committed Sep 1, 2017
1 parent eae9e05 commit ec25f6b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Alexandre Ardhuin <[email protected]>
Luke Freeman <[email protected]>
Vincent Le Quéméner <[email protected]>
Mike Hoolehan <[email protected]>
German Saprykin <[email protected]>
10 changes: 8 additions & 2 deletions packages/flutter/lib/src/widgets/scroll_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class TrackingScrollController extends ScrollController {

final Map<ScrollPosition, VoidCallback> _positionToListener = <ScrollPosition, VoidCallback>{};
ScrollPosition _lastUpdated;
double _lastUpdatedOffset;

/// The last [ScrollPosition] to change. Returns null if there aren't any
/// attached scroll positions, or there hasn't been any scrolling yet, or the
Expand All @@ -336,13 +337,16 @@ class TrackingScrollController extends ScrollController {
///
/// * [ScrollController.initialScrollOffset], which this overrides.
@override
double get initialScrollOffset => _lastUpdated?.pixels ?? super.initialScrollOffset;
double get initialScrollOffset => _lastUpdatedOffset ?? super.initialScrollOffset;

@override
void attach(ScrollPosition position) {
super.attach(position);
assert(!_positionToListener.containsKey(position));
_positionToListener[position] = () { _lastUpdated = position; };
_positionToListener[position] = () {
_lastUpdated = position;
_lastUpdatedOffset = position.pixels;
};
position.addListener(_positionToListener[position]);
}

Expand All @@ -354,6 +358,8 @@ class TrackingScrollController extends ScrollController {
_positionToListener.remove(position);
if (_lastUpdated == position)
_lastUpdated = null;
if (_positionToListener.isEmpty)
_lastUpdatedOffset = null;
}

@override
Expand Down
57 changes: 57 additions & 0 deletions packages/flutter/test/widgets/tracking_scroll_controller_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2017 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('TrackingScrollController saves offset',
(WidgetTester tester) async {
final TrackingScrollController controller = new TrackingScrollController();

await tester.pumpWidget(
new PageView.builder(
itemBuilder: (BuildContext context, int index) {
return new ListView(
controller: controller,
children: new List<Widget>.generate(
10,
(int i) => new Container(
height: 100.0, child: new Text("Page$index-Item$i")),
).toList());
},
),
);

expect(find.text('Page0-Item1'), findsOneWidget);
expect(find.text('Page1-Item1'), findsNothing);
expect(find.text('Page2-Item0'), findsNothing);
expect(find.text('Page2-Item1'), findsNothing);

controller.jumpTo(110.0);
await (tester.pumpAndSettle());

await tester.fling(
find.text('Page0-Item1'), const Offset(-100.0, 0.0), 10000.0);
await (tester.pumpAndSettle());

expect(find.text('Page0-Item1'), findsNothing);
expect(find.text('Page1-Item1'), findsOneWidget);
expect(find.text('Page2-Item0'), findsNothing);
expect(find.text('Page2-Item1'), findsNothing);

await tester.fling(
find.text('Page1-Item1'), const Offset(-100.0, 0.0), 10000.0);
await (tester.pumpAndSettle());

expect(find.text('Page0-Item1'), findsNothing);
expect(find.text('Page1-Item1'), findsNothing);
expect(find.text('Page2-Item0'), findsNothing);
expect(find.text('Page2-Item1'), findsOneWidget);

await tester.pumpWidget(new Text("Another page"));

expect(controller.initialScrollOffset, 0.0);
});
}

0 comments on commit ec25f6b

Please sign in to comment.