在 Flutter 中使用 GestureDetector 和 Button 进行可访问性有什么区别吗?
Is there any difference between using GestureDetector and Button for accesibility in Flutter?
在Flutter中,我们既可以使用GestureDetector onTap,也可以使用Button来捕捉用户的按下事件。它们之间在可访问性方面有什么区别吗,比如 HTML(div vs 按钮)?
另外,GestureDetector是怎么翻译成flutter web的?它是否转换为带有 onClick 处理程序或按钮的 div?
可访问性差异
如果你仔细观察它们中的每一个,它们都是由底层回调触发的。
- 按钮的 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.
See also:
enabled, which is true if the button is enabled.
- GestureDetector 的 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.
您会注意到两者没有太大区别,因为 GestureTapCallback and VoidCallback 的 typedef 等于 void Function();
,用于相同的目的。
flutter web 中onTap 和onPressed 的区别
关于你的第二个问题,你可以检查 html 输出中的 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"。
文档:
在Flutter中,我们既可以使用GestureDetector onTap,也可以使用Button来捕捉用户的按下事件。它们之间在可访问性方面有什么区别吗,比如 HTML(div vs 按钮)?
另外,GestureDetector是怎么翻译成flutter web的?它是否转换为带有 onClick 处理程序或按钮的 div?
可访问性差异
如果你仔细观察它们中的每一个,它们都是由底层回调触发的。
- 按钮的 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. See also: enabled, which is true if the button is enabled.
- GestureDetector 的 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.
您会注意到两者没有太大区别,因为 GestureTapCallback and VoidCallback 的 typedef 等于 void Function();
,用于相同的目的。
flutter web 中onTap 和onPressed 的区别
关于你的第二个问题,你可以检查 html 输出中的 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"。