我有一个
SingleChildScrollView
,里面有多个元素。其中一些是 MouseRegion
小部件。它监听悬停并开始一些动画。
但是,当它在
SingleChildScrollView
小部件上向下/向上滚动时悬停时,有点混乱。
现在我只想听滚动状态。并且在使用时不允许设置动画 scrolling
。
到目前为止,我已经尝试了两种不同的方法来做到这一点 -
听
ScrollNotification
onNotification: (notification) {
if (notification is ScrollStartNotification) {
_canPoint = false;
} else if (notification is ScrollUpdateNotification) {
_canPoint = false;
} else if (notification is ScrollEndNotification) {
_canPoint = true;
}
return true;
},
并将
_canPoint
变量与 IgnorePointer
小部件一起使用。
IgnorePointer(
ignoring: !_canPoint,
child: ...(Section that listens to MouseHover),
),
检查当前
context
的可滚动项的滚动状态。并且不会转发基于此的动画。
_runAnimation() {
final notifier = Scrollable.of(context).position.isScrollingNotifier;
if(notifier.value) return;
//* Continue with the animation.
}
但到目前为止,它们都没有发挥作用。我正在寻找更好的方法来处理这种情况。 提前致谢。 💙
试试这个
按照您的描述保留 ScrollNotification 侦听器。用它来更新布尔状态 (_canPoint),指示是否可以继续悬停动画。
对于可悬停元素,将它们包装在自定义小部件中,该小部件将 _canPoint 作为参数,并使用它来决定是否播放动画。
class CustomMouse extends StatelessWidget {
final Widget child;
final bool canAnimate; // This is your _canPoint variable passed in
CustomMouse({required this.child, required this.canAnimate});
@override
Widget build(BuildContext context) {
return MouseRegion(
onEnter: (event) {
if (canAnimate) {
// Trigger your animation
}
},
child: child,
);
}
}
将此 CustomMouse 小部件用于侦听鼠标悬停的部分,并传递 _canPoint 的当前值。