diff --git a/packages/flutter/lib/animation.dart b/packages/flutter/lib/animation.dart index 5ff4ca258b..444d52ef83 100644 --- a/packages/flutter/lib/animation.dart +++ b/packages/flutter/lib/animation.dart @@ -15,6 +15,5 @@ export 'src/animation/animation.dart'; export 'src/animation/animation_controller.dart'; export 'src/animation/animations.dart'; export 'src/animation/curves.dart'; -export 'src/animation/forces.dart'; export 'src/animation/listener_helpers.dart'; export 'src/animation/tween.dart'; diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index 0636805892..0c3174969d 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -11,7 +11,6 @@ import 'package:flutter/scheduler.dart'; import 'animation.dart'; import 'curves.dart'; -import 'forces.dart'; import 'listener_helpers.dart'; /// The direction in which an animation is running. @@ -23,6 +22,17 @@ enum _AnimationDirection { reverse, } +final SpringDescription _kFlingSpringDescription = new SpringDescription.withDampingRatio( + mass: 1.0, + springConstant: 500.0, + ratio: 1.0, +); + +const Tolerance _kFlingTolerance = const Tolerance( + velocity: double.INFINITY, + distance: 0.01, +); + /// A controller for an animation. /// /// This class lets you perform tasks such as: @@ -305,13 +315,16 @@ class AnimationController extends Animation return animateWith(new _RepeatingSimulation(min, max, period)); } - /// Flings the timeline with an optional force (defaults to a critically - /// damped spring within [lowerBound] and [upperBound]) and initial velocity. + /// Drives the animation with a critically damped spring (within [lowerBound] and [upperBound]) and initial velocity. + /// /// If velocity is positive, the animation will complete, otherwise it will dismiss. - Future fling({ double velocity: 1.0, Force force }) { - force ??= kDefaultSpringForce.copyWith(left: lowerBound, right: upperBound); + Future fling({ double velocity: 1.0 }) { _direction = velocity < 0.0 ? _AnimationDirection.reverse : _AnimationDirection.forward; - return animateWith(force.release(value, velocity)); + final double target = velocity < 0.0 ? lowerBound - _kFlingTolerance.distance + : upperBound + _kFlingTolerance.distance; + Simulation simulation = new SpringSimulation(_kFlingSpringDescription, value, target, velocity) + ..tolerance = _kFlingTolerance; + return animateWith(simulation); } /// Drives the animation according to the given simulation. diff --git a/packages/flutter/lib/src/animation/forces.dart b/packages/flutter/lib/src/animation/forces.dart deleted file mode 100644 index c3464425bf..0000000000 --- a/packages/flutter/lib/src/animation/forces.dart +++ /dev/null @@ -1,75 +0,0 @@ -// 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/physics.dart'; - -export 'package:flutter/physics.dart' show SpringDescription; - -/// A factory for simulations. -abstract class Force { - /// Abstract const constructor. This constructor enables subclasses to provide - /// const constructors so that they can be used in const expressions. - const Force(); - - /// Creates a new physics simulation with the given initial conditions. - Simulation release(double position, double velocity); -} - -/// A factory for spring-based physics simulations. -class SpringForce extends Force { - /// Creates a spring force. - /// - /// The [spring], [left], and [right] arguments must not be null. The [left] - /// argument defaults to 0.0 and the [right] argument defaults to 1.0. - const SpringForce(this.spring, { this.left: 0.0, this.right: 1.0 }); - - /// The description of the spring to be used in the created simulations. - final SpringDescription spring; - - /// Where to put the spring's resting point when releasing left. - final double left; - - /// Where to put the spring's resting point when releasing right. - final double right; - - /// Creates a copy of this spring force but with the given fields replaced with the new values. - SpringForce copyWith({ - SpringDescription spring, - double left, - double right - }) { - return new SpringForce( - spring ?? this.spring, - left: left ?? this.left, - right: right ?? this.right - ); - } - - /// How pricely to terminate the simulation. - /// - /// We overshoot the target by this distance, but stop the simulation when - /// the spring gets within this distance (regardless of how fast it's moving). - /// This causes the spring to settle a bit faster than it otherwise would. - static const Tolerance tolerance = const Tolerance( - velocity: double.INFINITY, - distance: 0.01 - ); - - @override - Simulation release(double position, double velocity) { - double target = velocity < 0.0 ? this.left - tolerance.distance - : this.right + tolerance.distance; - return new SpringSimulation(spring, position, target, velocity) - ..tolerance = tolerance; - } -} - -final SpringDescription _kDefaultSpringDesc = new SpringDescription.withDampingRatio( - mass: 1.0, - springConstant: 500.0, - ratio: 1.0 -); - -/// A spring force with reasonable default values. -final SpringForce kDefaultSpringForce = new SpringForce(_kDefaultSpringDesc); diff --git a/packages/flutter/test/animation/forces_test.dart b/packages/flutter/test/animation/forces_test.dart deleted file mode 100644 index bf5d104c77..0000000000 --- a/packages/flutter/test/animation/forces_test.dart +++ /dev/null @@ -1,16 +0,0 @@ -// 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/animation.dart'; -import 'package:flutter/widgets.dart'; - -void main() { - test('copyWith defaults to unchanged values', () { - SpringForce force = kDefaultSpringForce.copyWith(); - expect(force.spring, kDefaultSpringForce.spring); - expect(force.left, kDefaultSpringForce.left); - expect(force.right, kDefaultSpringForce.right); - }); -}