【发布时间】:2021-04-25 19:10:24
【问题描述】:
我一直试图在颤动中制作一个列表视图并获取视图滚动了多少像素。 不幸的是,我使用了 NotificationListener
像这样:
NotificationListener<ScrollUpdateNotification>(
child: ListView(
children: [
//content here
],
),
onNotification: (notification) {
//How many pixels scrolled from pervious frame
// print("scrollDelta: ${notification.scrollDelta}");
setState(() {
this.scrollValue = notification.metrics.pixels;
});
//List scroll position
// print("metrics.pixels: ${notification.metrics.pixels}");
return true;
},
),
问题是它给我的应用程序带来了一些非常糟糕的性能。
所以我注意到您可以直接将控制器与 ListView 一起使用 我做了以下代码:
ScrollController controller;
controller.addListener(() {
print("i'm here");
});
var scaffold = Scaffold(
backgroundColor: Color(0xFFFFFFFF),
resizeToAvoidBottomPadding: false,
body: Stack(
children: [
ListView(
controller: controller,
children: [
//content here
],
),
],
),
);
然后我得到以下错误:
NoSuchMethodError:null 上的无效成员:'addListener'
我目前不知道如何在不严重影响性能的情况下获取列表视图中滚动了多少像素。
【问题讨论】: