Created
July 21, 2021 02:24
-
-
Save justinemailone/8ab75b47f728c67e3cb7102cacae93e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// overscroll-physics.dart | |
/// | |
/// Scrollphysics that allow overscrolling | |
/// | |
/// Justin Hampton | |
/// 07/20/21 | |
/// | |
/// Adapted from | |
/// https://gist.github.com/makoConstruct/d069651b51d573a7a94bae13c8730656 | |
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
class AlwaysScrollableOverscrollPhysics extends AlwaysScrollableScrollPhysics { | |
final double overscrollStart; | |
final double overscrollEnd; | |
AlwaysScrollableOverscrollPhysics({ | |
this.overscrollStart = 0, | |
this.overscrollEnd = 0, | |
ScrollPhysics parent, | |
}) : super(parent: parent); | |
ScrollMetrics expandScrollMetrics(ScrollMetrics metrics) { | |
return FixedScrollMetrics( | |
pixels: metrics.pixels, | |
axisDirection: metrics.axisDirection, | |
minScrollExtent: min(metrics.minScrollExtent, -overscrollStart), | |
maxScrollExtent: metrics.maxScrollExtent + overscrollEnd, | |
viewportDimension: metrics.viewportDimension, | |
); | |
} | |
AlwaysScrollableOverscrollPhysics applyTo(ScrollPhysics ancestor) { | |
return AlwaysScrollableOverscrollPhysics( | |
parent: buildParent(ancestor), | |
overscrollStart: overscrollStart, | |
overscrollEnd: overscrollEnd, | |
); | |
} | |
@override | |
double applyPhysicsToUserOffset(ScrollMetrics position, double offset) { | |
return super.applyPhysicsToUserOffset(expandScrollMetrics(position), offset); | |
} | |
@override | |
bool shouldAcceptUserOffset(ScrollMetrics position) { | |
return super.shouldAcceptUserOffset(expandScrollMetrics(position)); | |
} | |
@override | |
double adjustPositionForNewDimensions({ | |
ScrollMetrics oldPosition, | |
ScrollMetrics newPosition, | |
bool isScrolling, | |
double velocity, | |
}) { | |
return super.adjustPositionForNewDimensions( | |
oldPosition: expandScrollMetrics(oldPosition), | |
newPosition: expandScrollMetrics(newPosition), | |
isScrolling: isScrolling, | |
velocity: velocity, | |
); | |
} | |
@override | |
double applyBoundaryConditions(ScrollMetrics position, double value) { | |
return super.applyBoundaryConditions( | |
expandScrollMetrics(position), | |
value, | |
); | |
} | |
@override | |
Simulation createBallisticSimulation(ScrollMetrics position, double velocity) { | |
return super.createBallisticSimulation(expandScrollMetrics(position), velocity); | |
} | |
} | |
class NeverScrollableOverscrollPhysics extends NeverScrollableScrollPhysics { | |
final double overscrollStart; | |
final double overscrollEnd; | |
NeverScrollableOverscrollPhysics({ | |
this.overscrollStart = 0, | |
this.overscrollEnd = 0, | |
ScrollPhysics parent, | |
}) : super(parent: parent); | |
ScrollMetrics expandScrollMetrics(ScrollMetrics metrics) { | |
return FixedScrollMetrics( | |
pixels: metrics.pixels, | |
axisDirection: metrics.axisDirection, | |
minScrollExtent: min(metrics.minScrollExtent, -overscrollStart), | |
maxScrollExtent: metrics.maxScrollExtent + overscrollEnd, | |
viewportDimension: metrics.viewportDimension, | |
); | |
} | |
NeverScrollableOverscrollPhysics applyTo(ScrollPhysics ancestor) { | |
return NeverScrollableOverscrollPhysics( | |
parent: buildParent(ancestor), | |
overscrollStart: overscrollStart, | |
overscrollEnd: overscrollEnd, | |
); | |
} | |
@override | |
double applyPhysicsToUserOffset(ScrollMetrics position, double offset) { | |
return super.applyPhysicsToUserOffset(expandScrollMetrics(position), offset); | |
} | |
@override | |
bool shouldAcceptUserOffset(ScrollMetrics position) { | |
return super.shouldAcceptUserOffset(expandScrollMetrics(position)); | |
} | |
@override | |
double adjustPositionForNewDimensions({ | |
ScrollMetrics oldPosition, | |
ScrollMetrics newPosition, | |
bool isScrolling, | |
double velocity, | |
}) { | |
return super.adjustPositionForNewDimensions( | |
oldPosition: expandScrollMetrics(oldPosition), | |
newPosition: expandScrollMetrics(newPosition), | |
isScrolling: isScrolling, | |
velocity: velocity, | |
); | |
} | |
@override | |
double applyBoundaryConditions(ScrollMetrics position, double value) { | |
return super.applyBoundaryConditions( | |
expandScrollMetrics(position), | |
value, | |
); | |
} | |
@override | |
Simulation createBallisticSimulation(ScrollMetrics position, double velocity) { | |
return super.createBallisticSimulation(expandScrollMetrics(position), velocity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment