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?
Asked
Active
Viewed 648 times
3
-
Can you please provide more details about what you are doing. Some code example that allows to reproduce the problem? – Günter Zöchbauer Sep 28 '14 at 07:05
-
Well it's not some specific problem. I was asking if there is some method I can override that fires when the window looses focus. – Vilda Sep 28 '14 at 07:06
-
If someone already had the same problem he might provide some help. From your question I have not the slightest idea what your problem is about. – Günter Zöchbauer Sep 28 '14 at 07:08
-
I'll try to specify it a bit more. – Vilda Sep 28 '14 at 07:17
-
The question has been edited. – Vilda Sep 28 '14 at 07:45
-
Something like http://stackoverflow.com/questions/1060008 ? – Günter Zöchbauer Sep 28 '14 at 08:21
-
Yes, but are the same functions available in dart? – Vilda Sep 28 '14 at 08:22
-
I didn't take a closer look because I dont't know if this is what you are looking for but if you can do it in JS there is a way in Dart too (for the worst case there is dart-js-interop) – Günter Zöchbauer Sep 28 '14 at 08:24
-
I was looking for something more 'built-in' than dart-js-interop. – Vilda Sep 28 '14 at 08:24
1 Answers
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
-
-
Great! I already tried it and it works for minimizing but not for lost focus. I'm currently trying `document.onFocus`. – Günter Zöchbauer Sep 28 '14 at 08:34
-
I've tried switching to other tabs and it fires the method. Thats basically what I wanted. – Vilda Sep 28 '14 at 08:37
-
The other events are on `window` not `document`. See my updated answer. – Günter Zöchbauer Sep 28 '14 at 08:38
-
Yes. Looks like onBlur works even better for my case. Thanks very much :) – Vilda Sep 28 '14 at 08:41