gpt4 book ai didi

flutter - 如何检测 GestureDetector 是否悬停在 Flutter 中?

转载 作者:行者123 更新时间:2023-12-04 04:27:12 25 4
gpt4 key购买 nike

所以我有一个小部件 GestureDetector在 Flutter 中,我希望它在“悬停”上做一些事情,就像这样(向任何方向拖动):
example
可以用 GestureDetector 做吗? ?任何帮助表示赞赏。

最佳答案

鼠标区域
鼠标区域小部件
MouseRegion 公开了一些与鼠标相关的事件,你猜对了。这些事件是:

OnEnter, which will trigger when your mouse enters the Widget.
OnExit, which will trigger when your mouse leaves the Widget.
OnHover, which will trigger both when your mouse enters the Widget and on every subsequent move inside the Widget.
所有这些实际上都是 PointerEvents,它返回有关用户指针的各种信息。在这里,我们将查看用户鼠标在包含 Widget 内的当前位置,该 Widget 以 Offset 的形式存储。
 class _MyHoverableWidgetState extends State<MyHoverableWidget>{
// Our state
bool amIHovering = false;
Offset exitFrom = Offset(0,0);

return MouseRegion(
onEnter: (PointerDetails details) => setState(() => amIHovering = true),
onExit: (PointerDetails details) => setState(() {
amIHovering = false;
exitFrom = details.localPosition; // You can use details.position if you are interested in the global position of your pointer.
}),
child: Container(
width: 200,
height: 200,
child: Center(
child:Text(amIHovering ? "Look mom, I'm hovering" : "I exited from: $exitFrom"),
),
),
);

}

关于flutter - 如何检测 GestureDetector 是否悬停在 Flutter 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65025809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com