Prevent PageView from ScrollNotification listener Flutter

In my use case, I have to listen my CustomScrollView scrolling with NotificationListener and I would like to prevent my PageView updating the pixels metrics value while swiping.

Code :

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NotificationListener(
        onNotification: (ScrollNotification scroll) {
          pixelsScrolled = scroll.metrics.pixels.toInt().toString();
          setState(() {});
          return true;
        },
        child: CustomScrollView(
          slivers: [
            SliverPadding(padding: EdgeInsets.all(20)),
            SliverToBoxAdapter(
              child: pageView(),
            ),
            SliverPadding(padding: EdgeInsets.all(40)),
            SliverToBoxAdapter(
              child: Center(child: Text("Pixels scrolled : " + pixelsScrolled, style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold))),
            ),
          ],
        ),
      ),
    );
  }

Result I want to avoid

enter image description here

Is there a way to avoid the NotificationListener to observe a desired child ?

Thanks for help

1 answer

  • answered 2021-09-11 18:42 croxx5f

    The Notification Listener Widget permits you to prevent the notifications from bubbling up when you return true, so. You can just wrap your pageView() with a NotificationListener that just drops the notifications altogether as follows:

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: NotificationListener(
            onNotification: (ScrollNotification scroll) {
                        pixelsScrolled = scroll.metrics.pixels.toInt().toString();
              setState(() {});
              return true;
            },
            child: CustomScrollView(
              slivers: [
                SliverPadding(padding: EdgeInsets.all(20)),
                SliverToBoxAdapter(
                    child: NotificationListener(
                  onNotification: (_) => true,
                  child: pageView(),
                )),
                SliverPadding(padding: EdgeInsets.all(40)),
                SliverToBoxAdapter(
                  child: Center(
                      child: Text("Pixels scrolled : " + pixelsScrolled,
                          style: TextStyle(
                              fontSize: 20, fontWeight: FontWeight.bold))),
                ),
              ],
            ),
          ),
        );
      }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum