在項目中遇到了一個場景,A頁面必須返回某個tab頁,但是A頁面可能會調到B,再跳到C,最后回到A。這個時候A的返回肯定是C。
想了一些解決方案,都不如監聽A頁面的實體鍵返回或者虛擬鍵返回來的快速便捷。
代碼:
@override Widget build(BuildContext context) { return WillPopScope( child: Scaffold( backgroundColor: Color(0xfff7f7f7), appBar: AppBar( title: Text('優惠券'), centerTitle: true, elevation: 0.0, ), ), onWillPop: _requestPop ); }
WillPopScope: 監聽返回
// 監聽返回 Future<bool> _requestPop() { pint('點擊了返回按鈕'); // dosomething() return new Future.value(false); }
這樣就可以在返回之前處理一些你想做的事情