Created
July 29, 2020 20:12
-
-
Save Kavantix/edf5113443e3ddb097214cfb0ea18975 to your computer and use it in GitHub Desktop.
Scroll page physics that only scrolls to next pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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