简体   繁体   English

在 Flutter 中使用 GestureDetector 和 Button 进行访问有什么区别?

[英]Is there any difference between using GestureDetector and Button for accesibility in Flutter?

In Flutter, we can either use GestureDetector onTap or Button to capture user press event.在 Flutter 中,我们可以使用 GestureDetector onTap 或 Button 来捕获用户按下事件。 Is there any difference between them for accessibility like in the case of HTML (div vs button)?在 HTML(div 与按钮)的情况下,它们之间的可访问性有什么区别吗?

Furthermore, how is GestureDetector translated into flutter web?此外,GestureDetector 是如何翻译成 flutter web 的? Is it translated into a div with a onClick handler or a button?它是翻译成带有 onClick 处理程序或按钮的 div 吗?

Difference in accessibility可访问性的差异

If you take a closer look at each of them, they both are triggered by the underlying callbacks.如果您仔细查看它们中的每一个,它们都由底层回调触发。

  • onPressed for the Button onPressed 按钮

VoidCallback onPressed VoidCallback onPressed
Called when the button is tapped or otherwise activated .按钮被点击或以其他方式激活时调用。 If this callback and onLongPress are null, then the button will be disabled.如果此回调和 onLongPress 为 null,则该按钮将被禁用。 See also: enabled, which is true if the button is enabled.另请参阅:启用,如果按钮已启用,则为 true。

  • onTap for GestureDetector手势检测器的 onTap

GestureTapCallback onTap GestureTapCallback onTap
A tap with a primary button has occurred.发生了带有主按钮的点击。 This triggers when the tap gesture wins .这会在点击手势获胜时触发 If the tap gesture did not win, onTapCancel is called instead.如果点击手势没有获胜,则改为调用 onTapCancel。

You will notice that there isn't much of a difference as both GestureTapCallback and VoidCallback are typedef equal to void Function();您会注意到没有太大区别,因为GestureTapCallbackVoidCallback的 typedef 等于void Function(); used for the same purpose.用于相同目的。


onTap and onPressed difference in flutter web flutter web 中的 onTap 和 onPressed 差异

As for your second question, you can check my personal website built in flutter and inspect the source code yourself, I use both GestureDetector and Button, they translate to the exact same role="button" in the html output.至于您的第二个问题,您可以查看我在 flutter 中构建的个人网站并自己检查源代码,我同时使用 GestureDetector 和 Button,它们在 html Z78E6221F6393D1356681DBCEDFZ 中转换为完全相同的role="button"


Documentation:文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM