【发布时间】:2021-11-07 16:14:09
【问题描述】:
我在 CustomScrollView 中有多个小部件和列表,我想在某些像素绑定条件下滚动时停止 CustomScrollView 滚动。
我可以使用NeverScrollPhysics() 来阻止它,但我不想在这里使用setState() 函数,因为带有列表的CustomScrollview 内容大到足以在滚动时重新加载屏幕。
也尝试使用Provider,但构建器仅提供了一个子小部件,该小部件不适用于 sliver 列表。
这是使用setState()的代码:
NotificationListener(
onNotification: (ScrollNotification notif) {
if(notif is ScrollUpdateNotification) {
if (canScroll && notif.metrics.pixels > 100) {
canScroll = false;
setState(() {});
}
}
if(notif is ScrollEndNotification) {
if(!canScroll) {
canScroll = true;
setState(() {});
}
}
return true;
},
child: CustomScrollView(
shrinkWrap: true,
physics: canScroll ? BouncingScrollPhysics() : NeverScrollableScrollPhysics(),
slivers: [
SliverToBoxAdapter(),
List(),
List(),
],
),
),
有没有办法只重新加载 CustomScrollView 而没有它的孩子?否则在这种情况下是否有任何解决方法来防止滚动?
感谢您的帮助
【问题讨论】:
-
使用 Stream 而不是 setState。
-
您的意思是
StreamBuilder吗?在这种情况下与Provider相同,将重新加载CustomScrollView的全部内容。 -
您需要一个状态管理解决方案,例如 bloc 或 riverpod。
-
@7mada 我已经在使用 Provider 但它不能解决这个问题
-
我知道 Provider 不会解决这个问题,但是 RiverPod 和 Bloc 可以,如果你想使用 RiverPod 我可以给你写一个答案来解决这个问题。
标签: flutter dart scrollview physics reload