gpt4 book ai didi

flutter 桌面 : How to catch closure button?

转载 作者:行者123 更新时间:2023-12-02 19:20:35 25 4
gpt4 key购买 nike

我正在使用 flutter (Windows) 开发桌面应用程序,如果用户尝试使用关闭按钮关闭窗口,我需要设置一个 AlertDialog。

如果有人有想法甚至解决方案,我会采纳 :D

我在下面附上了一张引用图片。

This button

最佳答案

window_manager 现在支持这个。

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);
_init();
super.initState();
}

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

void _init() async {
// Add this line to override the default close handler
await windowManager.setPreventClose(true);
setState(() {});
}

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

@override
void onWindowClose() async {
bool _isPreventClose = await windowManager.isPreventClose();
if (_isPreventClose) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text('Are you sure you want to close this window?'),
actions: [
TextButton(
child: Text('No'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('Yes'),
onPressed: () {
Navigator.of(context).pop();
await windowManager.destroy();
},
),
],
);
},
);
}
}
}

https://github.com/leanflutter/window_manager#confirm-before-closing

关于 flutter 桌面 : How to catch closure button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63115627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com