nic*_*er 3 scroll dart flutter notification-listener flutter-pageview
在我的用例中,我必须监听我的CustomScrollView
滚动,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: pageView(),
),
SliverPadding(padding: EdgeInsets.all(40)),
SliverToBoxAdapter(
child: Center(child: Text("Pixels scrolled : " + pixelsScrolled, style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold))),
),
],
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
我想避免的结果
有没有办法避免NotificationListener观察所需的孩子?
感谢帮助
通知监听器小部件允许您在返回 true 时防止通知冒泡。你可以用一个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))),
),
],
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
839 次 |
最近记录: |