如何访问Flutter Back按钮功能?

时间:2018-03-03 01:02:48

标签: function dart flutter

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

2 个答案:

答案 0 :(得分:10)

我认为您可以使用WillPopScope小部件。您可以传递一个回调函数,该函数将在视图关于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)

希望有所帮助!

答案 1 :(得分:-1)

使用以下代码:

Future<bool> _willPopHandler() async {

// if(con)
// showDialog(){ exit button click => return true for exit; }

return false;

}