您可以在 Page 2 上用 WillPopScope 包围您的脚手架,将 onWillPop 设置为 false 以防止页面被系统弹出,然后将您自己的后退按钮添加到应用栏的前导小部件中并在那里执行您的弹出操作。
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: new Scaffold(
appBar: new AppBar(
title: new Text("data"),
leading: new IconButton(
icon: new Icon(Icons.ac_unit),
onPressed: () => Navigator.of(context).pop(),
),
),
),
);
}
this post的答案代码
编辑:添加到第 2 页以控制导航
除了上面的代码,你将把下面的代码添加到第 2 页。更改
Navigator.of(context).pop()
到
Navigator.of(context).pop('upload_files')
然后在您导航的第 1 页中,您将等待导航并使用从第 2 页的弹出窗口返回的结果并运行您的逻辑
var navigationResult = await Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Page2()));
if(navigationResult == 'upload_files') {
uploadFiles(); // Perform your custom functionality here.
}