1

To develop a responsive application we need to know if the user is using his finger or a mouse to adapt the behavior of the application. For instance, if the screen is touchable, the buttons will be bigger...

We can listen to Window.onTouch events and Window.onMouse events to guess if the screen is touchable.

But I would like to know it when the application is loading : before any interaction by the user.

How can I do that ?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Gérald Reinhart
  • 3,101
  • 1
  • 13
  • 14

2 Answers2

1

You could use dart:js and use an approach shown in this answer https://stackoverflow.com/a/2915912/217408

Add this to your index.html

<script type="application/javascript">
  function hasTouchSupport() {
    return 'ontouchstart' in document.documentElement;
  }
</script>

And call it from Dart like

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);
}

false in Dartium/Chrome in Linux
true on my Android phone

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

According to What's the best way to detect a 'touch screen' device using JavaScript? it's not easy to have something that works well for all browser.

If you go with Modernizr you can use:

 import 'dart:js' as js;

 bool get isTouchDevice => js.context['Modernizr']['touch'];
Community
  • 1
  • 1
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132