smooth_window_close 0.1.0
smooth_window_close: ^0.1.0 copied to clipboard
Hide Flutter Windows apps before graceful asynchronous shutdown.
import 'dart:async';
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 StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'smooth_window_close example',
theme: ThemeData(colorSchemeSeed: Colors.blue, useMaterial3: true),
home: const ExamplePage(),
);
}
}
class ExamplePage extends StatefulWidget {
const ExamplePage({super.key});
@override
State<ExamplePage> createState() => _ExamplePageState();
}
class _ExamplePageState extends State<ExamplePage> {
late final SmoothWindowCloser _closer;
bool _closeToTray = false;
@override
void initState() {
super.initState();
_closer = SmoothWindowCloser(
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');
},
);
unawaited(_closer.initialize());
}
@override
void dispose() {
unawaited(_closer.dispose());
super.dispose();
}
@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: (value) => setState(() => _closeToTray = value),
),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: _closer.exit,
icon: const Icon(Icons.exit_to_app),
label: const Text('Explicit Exit'),
),
],
),
),
),
),
);
}
}