You can use WillPopScope
widget:
(您可以使用WillPopScope
小部件:)
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
final value = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
content: Text('Are you sure you want to exit?'),
actions: <Widget>[
FlatButton(
child: Text('No'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
FlatButton(
child: Text('Yes, exit'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
}
);
return value == true;
},
child: Scaffold(
appBar: AppBar(),
body: SafeArea(
child: Container()
),
),
);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…