Created
May 5, 2025 11:42
-
-
Save cybrox/d83c02c9e79c373ac1438af5ee060374 to your computer and use it in GitHub Desktop.
Flutter window_manager teardown issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:window_manager/window_manager.dart'; | |
Future<void> main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
await windowManager.ensureInitialized(); | |
WindowOptions windowOptions = WindowOptions( | |
size: Size(1280, 720), | |
minimumSize: Size(640, 480), | |
center: false, | |
backgroundColor: Colors.transparent, | |
skipTaskbar: false, | |
titleBarStyle: TitleBarStyle.normal, | |
); | |
windowManager.waitUntilReadyToShow(windowOptions, () async { | |
await windowManager.show(); | |
await windowManager.focus(); | |
windowManager.setPreventClose(true); | |
_CustomWindowListener _wmListener = _CustomWindowListener(); | |
_wmListener.setup(); | |
}); | |
runApp(const MyApp()); | |
} | |
class _CustomWindowListener extends WindowListener { | |
void setup() { | |
windowManager.addListener(this); | |
} | |
@override | |
void onWindowClose() { | |
windowManager.destroy(); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() => _counter++); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text('You have pushed the button this many times:'), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:window_manager/window_manager.dart'; | |
Future<void> main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
await windowManager.ensureInitialized(); | |
WindowOptions windowOptions = WindowOptions( | |
size: Size(1280, 720), | |
minimumSize: Size(640, 480), | |
center: false, | |
backgroundColor: Colors.transparent, | |
skipTaskbar: false, | |
titleBarStyle: TitleBarStyle.normal, | |
); | |
windowManager.waitUntilReadyToShow(windowOptions, () async { | |
await windowManager.show(); | |
await windowManager.focus(); | |
windowManager.setPreventClose(true); | |
_CustomWindowListener _wmListener = _CustomWindowListener(); | |
_wmListener.setup(); | |
}); | |
runApp(const MyApp()); | |
} | |
class _CustomWindowListener extends WindowListener { | |
void setup() { | |
windowManager.addListener(this); | |
} | |
@override | |
void onWindowClose() { | |
windowManager.setPreventClose(false); | |
windowManager.close(); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() => _counter++); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text('You have pushed the button this many times:'), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment