Flutter AlwaysScrollableScrollPhysics() not working

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

Upvotes: 1

Views: 4826

Answers (1)

Juan V
Juan V

Reputation: 508

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

Upvotes: 3

Related Questions