Is it possible to detect source of ScrollNotification in NotificationListener

Viewed 701

I have 4 Scrollable Widget in my page 1 Vertical (Main) and 3 Horizontal.

And 2 Scrollable Widgets (1 vertical & 1 horizontal) trigger parallax animation for background.

How can i detect which Scrollable widget is dispatching ScrollNotification.

Here's the example:

class MYPage extends StatefulWidget {
  MYPage({Key key}) : super(key: key);

  @override
  _MYPageState createState() => _MYPageState();
}

class _MYPageState extends State<MYPage> {
  double scrollX = 0.0;
  double scrollY = 0.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          ParallaxBackground(scrollX, scrollY),
          Positioned.fill(
            child: NotificationListener<ScrollNotification>(
              onNotification: (notification) {
                double scrollOffset = notification.metrics.pixels;
                setState(() {
                  if (notification.metrics.axis == Axis.horizontal) {
                    this.scrollX = scrollOffset;
                  } else {
                    this.scrollY = scrollOffset;
                  }
                });
                return true;
              },
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    Content(),
                    HorizontalScrollableContent(),
                    Content(),
                    HorizontalScrollableContentWithParallaxControl(),
                    Content(),
                    HorizontalScrollableContent(),
                    Content(),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
1 Answers

I didn't notice any way to detect the source of the scroll. So, I just surrounded each scrollable child in its own NotificationListener. I didn't want any of the child scrolls to bubble all the way up because I just wanted the parent scrollView notifications to bubble up. So, I wrapped each child with:

NotificationListener<ScrollNotification>(
    onNotification: (boolval) { return true; },
    child: ScrollableChildOfChoice()
)
Related