Commit ec9ab324 authored by Adam Barth's avatar Adam Barth

ScrollBehavior should support negative min scroll offset

Fixes #1730
parent 96405fb4
...@@ -21,7 +21,6 @@ abstract class ScrollBehavior { ...@@ -21,7 +21,6 @@ abstract class ScrollBehavior {
/// This function is called when a drag gesture ends and toSnapOffset is specified. /// This function is called when a drag gesture ends and toSnapOffset is specified.
Simulation createSnapScrollSimulation(double startOffset, double endOffset, double startVelocity, double endVelocity) => null; Simulation createSnapScrollSimulation(double startOffset, double endOffset, double startVelocity, double endVelocity) => null;
/// Returns the scroll offset to use when the user attempts to scroll /// Returns the scroll offset to use when the user attempts to scroll
/// from the given offset by the given delta. /// from the given offset by the given delta.
double applyCurve(double scrollOffset, double scrollDelta); double applyCurve(double scrollOffset, double scrollDelta);
...@@ -70,10 +69,31 @@ abstract class ExtentScrollBehavior extends ScrollBehavior { ...@@ -70,10 +69,31 @@ abstract class ExtentScrollBehavior extends ScrollBehavior {
/// A scroll behavior that prevents the user from exceeding scroll bounds. /// A scroll behavior that prevents the user from exceeding scroll bounds.
class BoundedBehavior extends ExtentScrollBehavior { class BoundedBehavior extends ExtentScrollBehavior {
BoundedBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 }) BoundedBehavior({
: super(contentExtent: contentExtent, containerExtent: containerExtent); 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 get maxScrollOffset => math.max(minScrollOffset, minScrollOffset + _contentExtent - _containerExtent);
double applyCurve(double scrollOffset, double scrollDelta) { double applyCurve(double scrollOffset, double scrollDelta) {
...@@ -118,8 +138,8 @@ class UnboundedBehavior extends ExtentScrollBehavior { ...@@ -118,8 +138,8 @@ class UnboundedBehavior extends ExtentScrollBehavior {
/// A scroll behavior that lets the user scroll beyond the scroll bounds with some resistance. /// A scroll behavior that lets the user scroll beyond the scroll bounds with some resistance.
class OverscrollBehavior extends BoundedBehavior { class OverscrollBehavior extends BoundedBehavior {
OverscrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 }) OverscrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0, double minScrollOffset: 0.0 })
: super(contentExtent: contentExtent, containerExtent: containerExtent); : super(contentExtent: contentExtent, containerExtent: containerExtent, minScrollOffset: minScrollOffset);
Simulation createFlingScrollSimulation(double position, double velocity) { Simulation createFlingScrollSimulation(double position, double velocity) {
return _createFlingScrollSimulation(position, velocity, minScrollOffset, maxScrollOffset); return _createFlingScrollSimulation(position, velocity, minScrollOffset, maxScrollOffset);
...@@ -148,6 +168,9 @@ class OverscrollBehavior extends BoundedBehavior { ...@@ -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. /// A scroll behavior that lets the user scroll beyond the scroll bounds only when the bounds are disjoint.
class OverscrollWhenScrollableBehavior extends OverscrollBehavior { 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; bool get isScrollable => contentExtent > containerExtent;
Simulation createFlingScrollSimulation(double position, double velocity) { Simulation createFlingScrollSimulation(double position, double velocity) {
......
// 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));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment