Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save proteye/db3e5f5a3d41a8ae23c6fbe06ebb06d0 to your computer and use it in GitHub Desktop.
Save proteye/db3e5f5a3d41a8ae23c6fbe06ebb06d0 to your computer and use it in GitHub Desktop.
AllwaysScrollableWithKeepPositionScrollPhysics - allways scrollable with keep position scroll physics in Flutter
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