阻止 PageView 从 ScrollNotification 侦听器 Flutter

Prevent PageView from ScrollNotification listener Flutter

在我的用例中,我必须用 NotificationListener 听我的 CustomScrollView 滚动,我想阻止我的 PageView 在滑动时更新像素指标值。

代码:

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

我想避免的结果

有没有办法避免 NotificationListener 观察到所需的 child?

感谢帮助

Notification Listener Widget 允许您在 return 为真时防止通知冒泡,所以。你可以只用一个 NotificationListener 包装你的 pageView() ,它只是完全删除通知,如下所示:

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