smooth_window_close 0.2.0
smooth_window_close: ^0.2.0 copied to clipboard
Hide Flutter Windows apps before graceful asynchronous shutdown.
import 'package:flutter/material.dart';
import 'package:smooth_window_close/smooth_window_close.dart';
import 'package:window_manager/window_manager.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await windowManager.ensureInitialized();
runApp(const ExampleApp());
}
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
bool _closeToTray = false;
@override
Widget build(BuildContext context) {
return SmoothWindowCloseScope(
shouldCloseToTray: () => _closeToTray,
onSaveState: () {
debugPrint('Saving window state');
},
onCleanup: () async {
debugPrint('Cleaning up');
// Replace with database, socket, server, or other required cleanup.
await Future<void>.value();
},
onError: (error, stackTrace) {
debugPrint('Shutdown error: $error\n$stackTrace');
},
child: MaterialApp(
title: 'smooth_window_close example',
theme: ThemeData(colorSchemeSeed: Colors.blue, useMaterial3: true),
home: ExamplePage(
closeToTray: _closeToTray,
onCloseToTrayChanged: (value) {
setState(() => _closeToTray = value);
},
),
),
);
}
}
class ExamplePage extends StatelessWidget {
const ExamplePage({
super.key,
required this.closeToTray,
required this.onCloseToTrayChanged,
});
final bool closeToTray;
final ValueChanged<bool> onCloseToTrayChanged;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('smooth_window_close')),
body: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 520),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Native X and Alt+F4 are handled automatically. The Windows '
'window hides before state saving and cleanup.',
),
const SizedBox(height: 20),
SwitchListTile(
contentPadding: EdgeInsets.zero,
title: const Text('Close to tray'),
subtitle: const Text(
'Demo has no tray icon; enable only to inspect hide '
'behavior. Process remains alive after X.',
),
value: closeToTray,
onChanged: onCloseToTrayChanged,
),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: SmoothWindowCloseScope.of(context).exit,
icon: const Icon(Icons.exit_to_app),
label: const Text('Explicit Exit'),
),
],
),
),
),
),
);
}
}