如何访问Flutter Back按钮功能?

Cha*_* Jr 10 function dart flutter

我想在用户返回上一页之前提供AdWords插页式广告.按下返回按钮后如何执行此操作?

Hem*_*Raj 21

我想你可以利用WillPopScopewidget.您可以传递一个回调函数,该函数将在视图关于pop时调用.只需在pop之前完成任何要完成的任务,然后返回true.

例:

Future<bool> _willPopCallback() async {
    // await showDialog or Show add banners or whatever
    // then
    return true; // return true if the route to be popped
}


//then pass the callback to WillPopScope
new WillPopScope(child: new Scaffold(), onWillPop: _willPopCallback)
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • 使用 WillPopScope 时有什么方法可以保持滑动到弹出功能吗?当我添加它时,我的停止工作。我正在尝试使用它,这样当有人滑动弹出时,键盘就会消失,但我还没有弄清楚。我尝试使用平移手势,但它可以在任何地方滑动,但在滑动弹出的边缘除外。 (3认同)