xxxxxxxxxx
FlutterWindowClose.setWindowShouldCloseHandler(() async {
return await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Do you really want to quit?'),
actions: [
ElevatedButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Yes')),
ElevatedButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('No')),
]);
});
});
xxxxxxxxxx
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Close App Example'),
),
body: Center(
child: ElevatedButton(
child: Text('Close App'),
onPressed: () {
SystemNavigator.pop(); // This closes the Flutter app
},
),
),
),
);
}
}