要开发响应式应用程序,我们需要知道用户是使用手指还是鼠标来适应应用程序的行为。例如,如果屏幕是可触摸的,则按钮将更大。
我们可以听Window.onTouch
事件和Window.onMouse
事件,以猜测屏幕是否可触摸。
但是我想在应用程序加载时知道它:在用户进行任何交互之前。
我怎样才能做到这一点 ?
最佳答案
您可以使用dart:js
并使用此答案中显示的方法https://stackoverflow.com/a/2915912/217408
将此添加到您的index.html
<script type="application/javascript">
function hasTouchSupport() {
return 'ontouchstart' in document.documentElement;
}
</script>
然后从Dart调用它
import 'dart:html';
import 'dart:js' as js;
bool _hasTouchSupport;
bool get hasTouchSupport {
if(_hasTouchSupport == null) {
_hasTouchSupport = js.context.callMethod('hasTouchSupport');
}
return _hasTouchSupport;
}
void main() {
print(hasTouchSupport);
}
Linux中Dartium / Chrome中的
false
我的Android手机上的true
关于dart - 我们如何知道Dart中可触摸的屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28188035/