Skip to content

Instantly share code, notes, and snippets.

@Kavantix
Created July 29, 2020 20:12
Show Gist options
  • Save Kavantix/edf5113443e3ddb097214cfb0ea18975 to your computer and use it in GitHub Desktop.
Save Kavantix/edf5113443e3ddb097214cfb0ea18975 to your computer and use it in GitHub Desktop.
Scroll page physics that only scrolls to next pages
class LimitingScrollPhysics extends PageScrollPhysics {
LimitingScrollPhysics({ScrollPhysics parent, @required this.controller})
: super(parent: parent);
final ScrollController controller;
@override
LimitingScrollPhysics applyTo(ScrollPhysics ancestor) {
return LimitingScrollPhysics(
parent: buildParent(ancestor), controller: controller);
}
@override
double applyBoundaryConditions(ScrollMetrics position, double value) {
if (controller.hasClients &&
controller.position.userScrollDirection == ScrollDirection.idle) {
return super.applyBoundaryConditions(position, value);
}
final page = (position.pixels / position.viewportDimension).floor();
if (value < page * position.viewportDimension) {
return value - position.pixels;
} else {
return super.applyBoundaryConditions(position, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment