Created
September 22, 2023 12:51
-
-
Save proteye/db3e5f5a3d41a8ae23c6fbe06ebb06d0 to your computer and use it in GitHub Desktop.
AllwaysScrollableWithKeepPositionScrollPhysics - allways scrollable with keep position scroll physics in Flutter
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
import 'package:flutter/material.dart'; | |
class AllwaysScrollableWithKeepPositionScrollPhysics extends ScrollPhysics { | |
final double? Function(double)? _onGetScrollPosition; | |
const AllwaysScrollableWithKeepPositionScrollPhysics( | |
{ScrollPhysics? parent, double? Function(double)? onGetScrollPosition}) | |
: _onGetScrollPosition = onGetScrollPosition, | |
super(parent: parent); | |
@override | |
AllwaysScrollableWithKeepPositionScrollPhysics applyTo( | |
ScrollPhysics? ancestor) { | |
return AllwaysScrollableWithKeepPositionScrollPhysics( | |
parent: buildParent(ancestor), | |
onGetScrollPosition: _onGetScrollPosition); | |
} | |
@override | |
double adjustPositionForNewDimensions({ | |
required ScrollMetrics oldPosition, | |
required ScrollMetrics newPosition, | |
required bool isScrolling, | |
required double velocity, | |
}) { | |
final position = _onGetScrollPosition?.call(newPosition.maxScrollExtent); | |
if (position != null) { | |
return position; | |
} | |
return super.adjustPositionForNewDimensions( | |
oldPosition: oldPosition, | |
newPosition: newPosition, | |
isScrolling: isScrolling, | |
velocity: velocity, | |
); | |
} | |
@override | |
bool shouldAcceptUserOffset(ScrollMetrics position) => true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment