Skip to content

Instantly share code, notes, and snippets.

@Ahmadre
Forked from slightfoot/fling_physics.dart
Created August 3, 2020 20:12
Show Gist options
  • Save Ahmadre/b9554b2bc98e8eb4f5295fa7ce4e49b7 to your computer and use it in GitHub Desktop.
Save Ahmadre/b9554b2bc98e8eb4f5295fa7ce4e49b7 to your computer and use it in GitHub Desktop.
Fling PageScrollPhysics for Flutter
// MIT License
//
// Copyright (c) 2019 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:flutter/material.dart';
class FlingPageScrollPhysics extends ScrollPhysics {
const FlingPageScrollPhysics(this.controller, {ScrollPhysics parent}) : super(parent: parent);
final PageController controller;
@override
FlingPageScrollPhysics applyTo(ScrollPhysics ancestor) {
return FlingPageScrollPhysics(controller, parent: buildParent(ancestor));
}
@override
Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
// If we're out of range and not headed back in range, defer to the parent
// ballistics, which should put us back in range at a page boundary.
if ((velocity <= 0.0 && position.pixels <= position.minScrollExtent) ||
(velocity >= 0.0 && position.pixels >= position.maxScrollExtent))
return super.createBallisticSimulation(position, velocity);
final Tolerance tolerance = this.tolerance;
final double target = _getTargetPixels(position, tolerance, velocity);
if (target != position.pixels) {
return ScrollSpringSimulation(spring, position.pixels, target, velocity, tolerance: tolerance);
}
return null;
}
double _getTargetPixels(ScrollPosition position, Tolerance tolerance, double velocity) {
final sim = ClampingScrollSimulation(position: position.pixels, velocity: velocity);
final width = position.viewportDimension * controller.viewportFraction;
double page = sim.x(1.0) / width;
if (velocity < -tolerance.velocity) {
page -= 0.5;
} else if (velocity > tolerance.velocity) {
page += 0.5;
}
return page.floorToDouble() * width;
}
@override
bool get allowImplicitScrolling => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment