3

How to handle lost focus in dart? For example, if some code runs on the page and users (for example) minimizes the window or switches to another tab, the current page looses focus. Is there some method that fires up in this case I can override in my code?

Vilda
  • 1,675
  • 1
  • 20
  • 50

1 Answers1

1
import 'dart:html' as dom;
import 'dart:async';

void main() {
  dom.document.onVisibilityChange.listen(visibilityChangeHandler);
  dom.window.onFocus.listen(focusHandler);
  dom.window.onBlur.listen(blurHandler);
}

void visibilityChangeHandler(dom.Event e) {
  print('visibility changed: $e');
}

void focusHandler(dom.Event e) {
  print('focus: $e');
}

void blurHandler(dom.Event e) {
  print('blur: $e');
}

see also Is there a way to detect if a browser window is not currently active?

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