Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active May 19, 2023 14:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slightfoot/c7d675ad1ff0c3b366b7acf33fe643df to your computer and use it in GitHub Desktop.
Save slightfoot/c7d675ad1ff0c3b366b7acf33fe643df to your computer and use it in GitHub Desktop.
Fling PageScrollPhysics for Flutter
// MIT License
//
// Copyright (c) 2023 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, {super.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(ScrollMetrics 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;
}
@tarek360
Copy link

here is the same code but updated to Dart 2.x and Flutter 3.x

class FlingPageScrollPhysics extends ScrollPhysics {
  const FlingPageScrollPhysics(this.controller, {super.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(ScrollMetrics 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;
}

@slightfoot
Copy link
Author

ah ha, you found it. I'll update the gist thanks!

@tarek360
Copy link

@slightfoot yes, found it but still trying to update it to get what I'm looking for, please check Slack :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment