dart - 我们如何知道Dart中可触摸的屏幕?

标签 dart dart-html

要开发响应式应用程序,我们需要知道用户是使用手指还是鼠标来适应应用程序的行为。例如,如果屏幕是可触摸的,则按钮将更大。

我们可以听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/

相关文章:

flutter - 在CustomScrollView中检测项目退出 View

dart - Dart 屏幕键盘事件

dart - HttpRequest.request不起作用

flutter - 如何防止 Row 占用所有可用宽度?

dart - 我无法理解这段 Dart 代码中的 @required 注释

firebase - 我可以在stream.map()中使用异步函数吗?

dom - 如何使用 Dart 动态添加样式表?

dart - 将Web组件添加到DOM

dart - 在AngularDart中使用dart:html库

dart - 在Dart上使用Notification API时出错