【问题标题】:Return data from a screen on Flutter, without using Navigator.pop从 Flutter 上的屏幕返回数据,而不使用 Navigator.pop
【发布时间】:2021-07-30 15:32:13
【问题描述】:

我有一个 DetailPage,它必须向调用它的代码返回一些内容。文档Return data from a screen 解释说您必须在某个按钮上调用Navigator.pop(context, whateverYouMustReturn)

到目前为止一切都很好,但是如果用户点击AppBar 上的返回 按钮会发生什么?那我怎么退货呢??

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

【问题讨论】:

  • 那么限制用户返回上一个屏幕怎么样?
  • 您可以轻松地从 Appbar 中移除后退按钮,并且系统导航栏可以通过小部件树中脚手架上方的 WillPopScope 小部件进行控制。
  • 你能把这个变成答案吗?如果没有其他办法,我会接受。
  • 嘿!我已经回答过了。请让我知道它是否有效?

标签: flutter dart flutter-navigation


【解决方案1】:

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

      AppBar(
        leading: BackButton(
          onPressed: () {
            Navigator.of(context).pop(myData);
          },
        ),
      ),

【讨论】:

    【解决方案2】:
    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
      ),
    );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2018-10-06
      • 2020-09-25
      • 2022-12-15
      • 2021-08-24
      相关资源
      最近更新 更多