Search code examples
flutterdartlistviewscrollscrollcontroller

How can I detect continuously if scrolling is disabled in Flutter?


I want to constantly check if scrolling is not possible.

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance!.addPostFrameCallback((duration) {

      print("${_scrollViewController.position.maxScrollExtent}");
      // prints true if scrollable else false
      print("isScrollable = ${_scrollViewController.position.maxScrollExtent != 0}");

    });
  }

I've tried this code, but it's only detected once and not continuously. What should I do?


Solution

  • Use NotificationListener widget.

    example:

    NotificationListener<ScrollNotification>(
      child: ListView(
         children: MyListChilren()),
      onNotification: (ScrollNotification scrollNotif) {
        print(scrollNotif.metrics.maxScrollExtent);
      },
    );