onFocusChanged property
Returns a stream broadcasting the application's window focus changes.
Implementation
Stream<bool> get onFocusChanged {
if (_focusController != null) {
return _focusController!.stream;
}
if (isWeb) {
return WebUtils.onFocusChanged;
} else if (isDesktop) {
_WindowListener listener = _WindowListener(
onBlur: () => _focusController!.add(false),
onFocus: () => _focusController!.add(true),
);
_focusController = StreamController<bool>.broadcast(
onListen: () => WindowManager.instance.addListener(listener),
onCancel: () {
WindowManager.instance.removeListener(listener);
_focusController?.close();
_focusController = null;
},
);
} else {
Worker? worker;
_focusController = StreamController<bool>.broadcast(
onListen:
() =>
worker = ever(
router.lifecycle,
(AppLifecycleState a) =>
_focusController?.add(a.inForeground),
),
onCancel: () {
worker?.dispose();
_focusController?.close();
_focusController = null;
},
);
}
return _focusController!.stream;
}