nicover
nicover

Reputation: 2633

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

Upvotes: 3

Views: 1108

Answers (1)

croxx5f
croxx5f

Reputation: 5733

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))),
            ),
          ],
        ),
      ),
    );
  }

Upvotes: 6

Related Questions