1

Question

Hi, I was searching a solution to allow user scroll on a list even when there is insufficient content. Looking throght Flutter documentation i found this page https://api.flutter.dev/flutter/widgets/ScrollView/physics.html

As the documentation said

To force the scroll view to always be scrollable even if there is insufficient content, as if primary was true but without necessarily setting it to true, provide an AlwaysScrollableScrollPhysics physics object, as in:

physics: const AlwaysScrollableScrollPhysics(),

so I tried to run a simple code an detect user scroll even when there isn't enough content

code

class Page extends StatefulWidget {
 @override
 _PageState createState() => _PageState();
}
class _PageState extends State<Page> {
 @override
 Widget build(BuildContext context) {
 final ScrollController scrollController = ScrollController();
 @override
 void initState(){
 scrollController.addListener((){
 print('listener called');
 });
 super.initState();
 }
 return Scaffold(
 body: ListView.builder(
 controller: scrollController,
 physics: const AlwaysScrollableScrollPhysics(),
 itemCount: 5,
 itemBuilder: (context, index){
 return Padding(
 padding: const EdgeInsets.only(bottom: 8.0),
 child: Container(
 color: Colors.black,
 height: 50,
 ),
 );
 },
 ),
 
 );
 }
}

Why this isn't working?

edit

Here is the design i'm looking forward

enter image description here

I have a list that is dynamically created. I want to be able to detect user vertical swipes on that list even if there is no scroll because there aren't enough element to overflow the screen height.

On a scrollable list I can simply add a scroll Listener and then every time a scroll is detected I can do my logic with scrollController.position info's. I want scroll listener to be called even when user swipes on list of this type

asked Nov 15, 2019 at 1:19
2
  • Can you please explain "scroll on a list even when there is insufficient content"? What is the exact outcome that you're looking for? Is there any design / gif that you can show? Commented Nov 15, 2019 at 1:59
  • I edited the thread. Hope I was clear Commented Nov 15, 2019 at 2:08

1 Answer 1

3

I do see the effect of scroll with the addition of AlwaysScrollableScrollPhysics so that part seems to be working. Maybe wrapping the scaffold on a NotificationListener can do what you're trying to do:

class _PageState extends State<Page> {
 @override
 Widget build(BuildContext context) {
 final ScrollController scrollController = ScrollController();
 return NotificationListener(
 child: Scaffold(
 body: ListView.builder(
 controller: scrollController,
 physics: const AlwaysScrollableScrollPhysics(),
 itemCount: 5,
 itemBuilder: (context, index) {
 return Padding(
 padding: const EdgeInsets.only(bottom: 8.0),
 child: Container(
 color: Colors.black,
 height: 50,
 ),
 );
 },
 ),
 ),
 onNotification: (scrollNotification) {
 if (scrollNotification is ScrollStartNotification) {
 print('Widget has started scrolling');
 }
 return true;
 },
 );
 }
}

NotificationListener has a property called onNotification that allows you to check for different kinds of scrollNotifications, you can check more here: NotificationListener Class and ScrollNotification class

answered Nov 15, 2019 at 2:29
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was the solution. Adding Notification Listener allowed me to catch any event.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.