最佳答案
您需要的小部件是WillPopScope
。用它包裹您的脚手架,并使用onWillPop
属性来做您需要的事情。这是一个例子:
class Hello extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new WillPopScope(
child: new Scaffold(
appBar: new AppBar(
title: new Text('Hi'),
),
body: new Center(
child: new Text('Hello'),
),
),
onWillPop: () async {
_doWhateverIWant();
},
);
}
}
DOCS:https://api.flutter.dev/flutter/widgets/WillPopScope-class.html
关于flutter - 如何知道用户何时按下返回页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64425941/