From ec9ab32435c794f7c062577124dc95aadf2b140f Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 10 Feb 2016 12:13:20 -0800 Subject: [PATCH] ScrollBehavior should support negative min scroll offset Fixes #1730 --- .../lib/src/widgets/scroll_behavior.dart | 35 +++++++++++++++--- .../test/widget/scroll_behavior_test.dart | 37 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 packages/flutter/test/widget/scroll_behavior_test.dart diff --git a/packages/flutter/lib/src/widgets/scroll_behavior.dart b/packages/flutter/lib/src/widgets/scroll_behavior.dart index 815a17e244..5eacf1d228 100644 --- a/packages/flutter/lib/src/widgets/scroll_behavior.dart +++ b/packages/flutter/lib/src/widgets/scroll_behavior.dart @@ -21,7 +21,6 @@ abstract class ScrollBehavior { /// This function is called when a drag gesture ends and toSnapOffset is specified. Simulation createSnapScrollSimulation(double startOffset, double endOffset, double startVelocity, double endVelocity) => null; - /// Returns the scroll offset to use when the user attempts to scroll /// from the given offset by the given delta. double applyCurve(double scrollOffset, double scrollDelta); @@ -70,10 +69,31 @@ abstract class ExtentScrollBehavior extends ScrollBehavior { /// A scroll behavior that prevents the user from exceeding scroll bounds. class BoundedBehavior extends ExtentScrollBehavior { - BoundedBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 }) - : super(contentExtent: contentExtent, containerExtent: containerExtent); + BoundedBehavior({ + double contentExtent: 0.0, + double containerExtent: 0.0, + double minScrollOffset: 0.0 + }) : _minScrollOffset = minScrollOffset, + super(contentExtent: contentExtent, containerExtent: containerExtent); - double minScrollOffset = 0.0; + double _minScrollOffset; + + double updateExtents({ + double contentExtent, + double containerExtent, + double minScrollOffset, + double scrollOffset: 0.0 + }) { + if (minScrollOffset != null) + _minScrollOffset = minScrollOffset; + return super.updateExtents( + contentExtent: contentExtent, + containerExtent: containerExtent, + scrollOffset: scrollOffset + ); + } + + double get minScrollOffset => _minScrollOffset; double get maxScrollOffset => math.max(minScrollOffset, minScrollOffset + _contentExtent - _containerExtent); double applyCurve(double scrollOffset, double scrollDelta) { @@ -118,8 +138,8 @@ class UnboundedBehavior extends ExtentScrollBehavior { /// A scroll behavior that lets the user scroll beyond the scroll bounds with some resistance. class OverscrollBehavior extends BoundedBehavior { - OverscrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 }) - : super(contentExtent: contentExtent, containerExtent: containerExtent); + OverscrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0, double minScrollOffset: 0.0 }) + : super(contentExtent: contentExtent, containerExtent: containerExtent, minScrollOffset: minScrollOffset); Simulation createFlingScrollSimulation(double position, double velocity) { return _createFlingScrollSimulation(position, velocity, minScrollOffset, maxScrollOffset); @@ -148,6 +168,9 @@ class OverscrollBehavior extends BoundedBehavior { /// A scroll behavior that lets the user scroll beyond the scroll bounds only when the bounds are disjoint. class OverscrollWhenScrollableBehavior extends OverscrollBehavior { + OverscrollWhenScrollableBehavior({ double contentExtent: 0.0, double containerExtent: 0.0, double minScrollOffset: 0.0 }) + : super(contentExtent: contentExtent, containerExtent: containerExtent, minScrollOffset: minScrollOffset); + bool get isScrollable => contentExtent > containerExtent; Simulation createFlingScrollSimulation(double position, double velocity) { diff --git a/packages/flutter/test/widget/scroll_behavior_test.dart b/packages/flutter/test/widget/scroll_behavior_test.dart new file mode 100644 index 0000000000..096be78ceb --- /dev/null +++ b/packages/flutter/test/widget/scroll_behavior_test.dart @@ -0,0 +1,37 @@ +// Copyright 2015 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:test/test.dart'; + +void main() { + test('BoundedBehavior min scroll offset', () { + BoundedBehavior behavior = new BoundedBehavior( + contentExtent: 150.0, + containerExtent: 75.0, + minScrollOffset: -100.0 + ); + expect(behavior.minScrollOffset, equals(-100.0)); + expect(behavior.maxScrollOffset, equals(-25.0)); + + double scrollOffset = behavior.updateExtents( + contentExtent: 125.0, + containerExtent: 50.0, + scrollOffset: -80.0 + ); + + expect(behavior.minScrollOffset, equals(-100.0)); + expect(behavior.maxScrollOffset, equals(-25.0)); + expect(scrollOffset, equals(-80.0)); + + scrollOffset = behavior.updateExtents( + minScrollOffset: 50.0, + scrollOffset: scrollOffset + ); + + expect(behavior.minScrollOffset, equals(50.0)); + expect(behavior.maxScrollOffset, equals(125.0)); + expect(scrollOffset, equals(50.0)); + }); +}