'Disable scroll at negative index ListView, ScrollPhysics flutter
I am using this indexed_list_view extension to jump to a particular index in my finite ListView. This extension has limitations because of which if I scroll in the reverse direction, empty space is visible.
Limitation(as written on package readme): The list is always infinite both to positive and negative indexes. In other words, it can be scrolled indefinitely both to the top and to the bottom.
I want to disable reverse swipe when at first index in my ListView. I am using custom scroll physics to disable the left swipe, but I want to modify it so that the reverse scroll is disabled at top of the ListView, not always. Any help would be highly appreciable. Thanks.
import 'package:flutter/material.dart';
class CustomScrollPhysics extends ScrollPhysics {
CustomScrollPhysics({ScrollPhysics? parent}) : super(parent: parent);
bool isGoingLeft = false;
@override
ScrollPhysics applyTo(ScrollPhysics? ancestor) {
return CustomScrollPhysics(parent: buildParent(ancestor)!);
}
@override
double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
isGoingLeft = offset < 0;
return offset;
}
@override
double applyBoundaryConditions(ScrollMetrics position, double value) {
//print("applyBoundaryConditions");
assert(() {
if (value == position.pixels) {
throw FlutterError(
'$runtimeType.applyBoundaryConditions() was called redundantly.\n'
'The proposed new position, $value, is exactly equal to the current position of the '
'given ${position.runtimeType}, ${position.pixels}.\n'
'The applyBoundaryConditions method should only be called when the value is '
'going to actually change the pixels, otherwise it is redundant.\n'
'The physics object in question was:\n'
' $this\n'
'The position object in question was:\n'
' $position\n');
}
return true;
}());
if (value < position.pixels &&
position.pixels <= position.minScrollExtent) {
return value - position.pixels;
}
if (position.maxScrollExtent <= position.pixels &&
position.pixels < value) {
return value - position.pixels;
}
if (value < position.minScrollExtent &&
position.minScrollExtent < position.pixels) {
return value - position.minScrollExtent;
}
if (position.pixels < position.maxScrollExtent &&
position.maxScrollExtent < value) {
return value - position.maxScrollExtent;
}
if (!isGoingLeft) {
return value - position.pixels;
}
return 0.0;
}
}
Solution 1:[1]
I think if you just apply a controller to the ListView it should stop scrolling negatively.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ktnishide |