I meet problem with the ListView. When i scroll listview to top I will disable Scroll. But when I scroll down again, the listview not scroll down. I want when I scroll up it will able scroll down again. Pls help me !
class PageTwo extends StatefulWidget {
const PageTwo({Key? key}) : super(key: key);
@override
State<PageTwo> createState() => _PageTwoState();
}
class _PageTwoState extends State<PageTwo> {
ScrollController scrollController = ScrollController();
ScrollPhysics scrollPhysics = const BouncingScrollPhysics();
scrollControllerListener() {
scrollController.addListener(() {
if (scrollController.position.pixels <= 0) {
setState(() {
scrollPhysics = const NeverScrollableScrollPhysics();
});
}
});
}
@override
void initState() {
scrollControllerListener();
super.initState();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onLongPress: () {
setState(() {
scrollPhysics = const BouncingScrollPhysics();
});
},
onVerticalDragDown: (details) {
setState(() {
scrollPhysics = const NeverScrollableScrollPhysics();
});
},
child: ListView.builder(
controller: scrollController,
scrollDirection: Axis.vertical,
physics: scrollPhysics,
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return const NewWidget();
},
),
);
}
}
Tell me about how to fix.
2
Answers
That’s becuz when u reach the top, you u
This makes your ListView won’t be able to scroll anymore. I don’t know why you want to disable scroll when u reach the top, it seems weird. But if you still want to do that way, I have small trick for you: Use
Future.delayed
with a few milisecs after assigedscrollPhysics = const NeverScrollableScrollPhysics();
. Like this:A small advice relates to clean code: write a separated function for adding scroll controller’s listener.
To achieve this, you can use the
NotificationListener
widget to intercept scroll notifications and dynamically change theNeverScrollableScrollPhysics
toBouncingScrollPhysics
and vice versa. Here’s how you can modify your code to achieve this behavior