效果GIF
route.gif
解決
WillPopScope
class RoutePageWithValue extends StatelessWidget {
final String lastPageName;
BuildContext context;
RoutePageWithValue(this.lastPageName);
_showDialog() {
showDialog<Null>(
context: context,
child: new AlertDialog(content: new Text('退出當(dāng)前界面'), actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
Navigator.of(context).pop();
},
child: new Text('確定'))
]),
);
}
Future<bool> _requestPop() {
_showDialog();
return new Future.value(false);
}
@override
Widget build(BuildContext context) {
this.context = context;
//監(jiān)聽左上角返回和實體返回
return new WillPopScope(
child: new Scaffold(
appBar: new AppBar(
title: new Text('RoutePageWithValue'),
centerTitle: true,
),
body: new Center(
child: new Text('$lastPageName'),
)),
onWillPop: _requestPop);
}
}
如果當(dāng)前是棧內(nèi)最后一個頁面妇穴,或者主界面
_showDialog() {
showDialog<Null>(
context: context,
child: new AlertDialog(content: new Text('退出app'), actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
},
child: new Text('確定'))
]),
);
}