diff --git a/examples/flutter_gallery/lib/demo/animation/home.dart b/examples/flutter_gallery/lib/demo/animation/home.dart index e669e0f3fd..11bde3419e 100644 --- a/examples/flutter_gallery/lib/demo/animation/home.dart +++ b/examples/flutter_gallery/lib/demo/animation/home.dart @@ -365,6 +365,64 @@ class _AllSectionsView extends AnimatedWidget { } } +// Support snapping scrolls to the midScrollOffset: the point at which the +// app bar's height is _kAppBarMidHeight and only one section heading is +// visible. +class _SnappingScrollPhysics extends ClampingScrollPhysics { + _SnappingScrollPhysics({ ScrollPhysics parent, this.midScrollOffset }) : super(parent: parent); + + final double midScrollOffset; + + @override + _SnappingScrollPhysics applyTo(ScrollPhysics parent) { + return new _SnappingScrollPhysics( + parent: parent, + midScrollOffset: midScrollOffset + ); + } + + Simulation _toMidScrollOffsetSimulation(double offset, double dragVelocity) { + final double velocity = math.max(dragVelocity, minFlingVelocity); + return new ScrollSpringSimulation(spring, offset, midScrollOffset, velocity, tolerance: tolerance); + } + + Simulation _toZeroScrollOffsetSimulation(double offset, double dragVelocity) { + final double velocity = math.max(dragVelocity, minFlingVelocity); + return new ScrollSpringSimulation(spring, offset, 0.0, velocity, tolerance: tolerance); + } + + @override + Simulation createBallisticSimulation(ScrollMetrics position, double dragVelocity) { + final Simulation simulation = super.createBallisticSimulation(position, dragVelocity); + final double offset = position.pixels; + + if (simulation != null) { + // The drag ended with sufficient velocity to trigger creating a simulation. + // If the simulation is headed up towards midScrollOffset but will not reach it, + // then snap it there. Similarly if the simulation is headed down past + // midScrollOffset but will not reach zero, then snap it to zero. + final double simulationEnd = simulation.x(double.INFINITY); + if (simulationEnd >= midScrollOffset) + return simulation; + if (dragVelocity > 0.0) + return _toMidScrollOffsetSimulation(offset, dragVelocity); + if (dragVelocity < 0.0) + return _toZeroScrollOffsetSimulation(offset, dragVelocity); + } else { + // The user ended the drag with little or no velocity. If they + // didn't leave the the offset above midScrollOffset, then + // snap to midScrollOffset if they're more than halfway there, + // otherwise snap to zero. + final double snapThreshold = midScrollOffset / 2.0; + if (offset >= snapThreshold && offset < midScrollOffset) + return _toMidScrollOffsetSimulation(offset, dragVelocity); + if (offset > 0.0 && offset < snapThreshold) + return _toZeroScrollOffsetSimulation(offset, dragVelocity); + } + return simulation; + } +} + class AnimationDemoHome extends StatefulWidget { const AnimationDemoHome({ Key key }) : super(key: key); @@ -478,6 +536,7 @@ class _AnimationDemoHomeState extends State { children: [ new CustomScrollView( controller: _scrollController, + physics: new _SnappingScrollPhysics(midScrollOffset: appBarMidScrollOffset), slivers: [ // Start out below the status bar, gradually move to the top of the screen. new _StatusBarPaddingSliver( diff --git a/packages/flutter/lib/src/widgets/scroll_physics.dart b/packages/flutter/lib/src/widgets/scroll_physics.dart index e015a332d4..66494a6ab8 100644 --- a/packages/flutter/lib/src/widgets/scroll_physics.dart +++ b/packages/flutter/lib/src/widgets/scroll_physics.dart @@ -13,7 +13,7 @@ import 'overscroll_indicator.dart'; import 'scroll_metrics.dart'; import 'scroll_simulation.dart'; -export 'package:flutter/physics.dart' show Tolerance; +export 'package:flutter/physics.dart' show Simulation, ScrollSpringSimulation, Tolerance; /// Determines the physics of a [Scrollable] widget. ///