7

When I click a different window in my desktop manager, my flutter desktop app is no longer the active / focused window (title bar goes grey).

How can I listen for this event?

I've tried a MouseRegion but it seems that onExit isn't fired when the mouse leaves the window.

I'm specifically looking for a Linux and Windows solution but would love something that also works on Mac.

phimath
  • 1,322
  • 2
  • 12
  • 22

2 Answers2

0

There is not yet an event wired through to Dart for that in any of the desktop embeddings. Currently the only way to do that would be to write a plugin that sent a custom message for window activation and deactivation.

smorgan
  • 20,228
  • 3
  • 47
  • 55
0

You can use window_manager, which helps to find the focus and inactive/blur state

import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WindowListener {
  @override
  void initState() {
    windowManager.addListener(this);
    super.initState();
  }

  @override
  void dispose() {
    windowManager.removeListener(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // ...
  }

 
  @override
  void onWindowFocus() {
    // do something when app gets into focus state
  }

  @override
  void onWindowBlur() {
    // do something when app gets into inactive/blur state
  }

}