Prevent PageView from ScrollNotification listener Flutter
194
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))),
),
],
),
),
);
}
Author by
nicover
Updated on January 01, 2023Comments
-
nicover over 1 year
In my use case, I have to listen my
CustomScrollView
scrolling withNotificationListener
and I would like to prevent myPageView
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
Is there a way to avoid the NotificationListener to observe a desired child ?
Thanks for help