等待页面转换完成

Mic*_*ter 6 flutter

如何检测页面转换是否在 flutter 中完成?

背景:当我弹出一个页面时,我想在页面转换完成后启动一个功能。

Mig*_*ivo 5

您可以在新路由时注册回调,push该路由也可以包含弹出路由中的数据(例如,如果您需要从中传递数据)。

Navigator.of(context).push(/*Some route*/).then((data) {
 // You can call your function here. data will be null if nothing is passed from the popped route
});
Run Code Online (Sandbox Code Playgroud)

如果你想从弹出的路线通过data,就这样做

Navigator.of(context).pop(someData);
Run Code Online (Sandbox Code Playgroud)

已编辑

如果您想知道转换何时真正结束,您也可以为其注册一个回调侦听器

  Navigator.of(context).push(MaterialPageRoute(
          builder: (BuildContext context) => RouteWidget())
                   ..completed.then((_) {print('transition completed');},
          ),
  );
Run Code Online (Sandbox Code Playgroud)

  • 但是 ``.then`` 的回调会在页面弹出后立即触发。它不会等待转换完成。 (3认同)