如何在按下appbar后退按钮时确认应用程序退出



我想在按下应用程序栏上的后退按钮时确认退出应用程序。

appBar: AppBar(
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Save and Exit?'),
content: Text('are you sure you want to save and exit?'),
actions: [
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text('Yes'),
),
],
);
},
);
// Navigator.pop(context);
},
),

我试过了,但没有成功。我已经找到了一些关于如何使用WillPopScope按下系统后退按钮的答案,但在我的情况下,这些答案都不起作用。

帮我解决

我想你可以用Navigator来检查它。canPop(上下文(。它将返回true或false。在onPressed中,你可以检查它,如果它是真的,你可以做Navigator.pop(context(,否则调用showDialog。

有doc的链接https://api.flutter.dev/flutter/widgets/Navigator/canPop.html

最新更新