Return 来自 Flutter 屏幕的数据,未使用 Navigator.pop

Return data from a screen on Flutter, without using Navigator.pop

我有一个 DetailPage 必须 return 调用它的代码。文档 Return data from a screen 说明您必须在某个按钮上调用 Navigator.pop(context, whateverYouMustReturn)

到目前为止一切顺利,但是如果用户点击 AppBar 上的 返回 按钮会发生什么?那我怎么return点东西呢??

PS 我正在使用 Navigator 1.0,顺便说一句

提供您自己的领先小部件并覆盖回调。

      AppBar(
        leading: BackButton(
          onPressed: () {
            Navigator.of(context).pop(myData);
          },
        ),
      ),
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    leading: Container(), //Make leading an empty container to hide the default back button 
  ),
  body: WillPopScope(
    onWillPop: () async {
      Navigator.pop(context); //This method returns future boolean, based on your condition you can either 
      return true; //allow you to go back or you can show a toast and restrict in this page
    },
    child: Container(), //Your details page widget instead of container
  ),
);
}