Navigator.pop()回调?

时间:2019-12-11 17:43:11

标签: flutter dart

是否有回调函数或一种方法来知道Navigator.of(context).pop()的动画返回到先前的屏幕已完成?目前我正在使用await Future.delayed(Duration(milliseconds: 500));完成此操作。有更好的方法吗?

我将其用于showModalBottomSheet(useRootNavigator: true),然后Navigator.pop()关闭底部工作表,然后推一条新路线。

代码:

Navigator.of(context).pop();
await Future.delayed(Duration(milliseconds: 500));
Navigator.of(context).push(...) // push new screen

3 个答案:

答案 0 :(得分:1)

您可以尝试

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['C:\\Users\\WallaceSilva\\Desktop\\pyinstaller'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='setup',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='setup')

答案 1 :(得分:1)

按一条路线已经维护了一个Future,当弹出发生时,它会完成。这意味着您可以执行以下操作:

Navigator.of(context).pushNamed('/someRoute').then((completion){
    //do something
});

名为完成的参数也可以是要从显示的屏幕中检索的任何数据。例如,假设我有一个群聊,我想向其中添加一个用户。为此,我想显示一个屏幕,允许我选择一个用户,然后将该用户传递回上一个屏幕。

Navigator.of(context).pushNamed('/InviteToChatScreen').then((object) {
    if (object != null) {
        var user = object as User;
        //add them to the chat
    }
});

答案 2 :(得分:0)

您可以尝试使用 RouteAware,它提供的覆盖方法很少

didPop() → void
Called when the current route has been popped off.
didPopNext() → void
Called when the top route has been popped off, and the current route shows up.
didPush() → void
Called when the current route has been pushed.
didPushNext() → void

https://api.flutter.dev/flutter/widgets/RouteAware-class.html