Flutter GETX 返回指定頁面

在Flutter中,如果你的頁面由 A->B->C->D->E ,然后在E頁面需要返回到B頁面,其余的頁面依次返回,你可以在D頁面跳轉(zhuǎn)到E頁面的時(shí)候這樣寫:


Get.offUntil(

? ? ? ? ? GetPageRoute<EPage>(

? ? ? ? ? ? ? settings: RouteSettings(

? ? ? ? ? ? ? ? name: '/EPage',

? ? ? ? ? ? ? ? arguments: arguments,

? ? ? ? ? ? ? ),

? ? ? ? ? ? ? page: () => EPage()),

? ? ? ? ? (route) =>

? ? ? ? ? ? ? (route as GetPageRoute).routeName ==

? ? ? ? ? ? ?'/BPage');


查看Get.offUntil的源碼,它是這樣寫的:


/// **Navigation.pushAndRemoveUntil()** shortcut.<br><br>

? ///

? /// Push the given `page`, and then pop several pages in the stack until

? /// [predicate] returns true

? ///

? /// [id] is for when you are using nested navigation,

? /// as explained in documentation

? ///

? /// Obs: unlike other get methods, this one you need to send a function

? /// that returns the widget to the page argument, like this:

? /// Get.offUntil(GetPageRoute(page: () => HomePage()), predicate)

? ///

? /// [predicate] can be used like this:

? /// `Get.offUntil(page, (route) => (route as GetPageRoute).routeName == '/home')`

? /// to pop routes in stack until home,

? /// or also like this:

? /// `Get.until((route) => !Get.isDialogOpen())`, to make sure the dialog

? /// is closed

? Future<T?>? offUntil<T>(Route<T> page, RoutePredicate predicate, {int? id}) {

? ? // if (key.currentState.mounted) // add this if appear problems on future with route navigate

? ? // when widget don't mounted

? ? return global(id).currentState?.pushAndRemoveUntil<T>(page, predicate);

? }


由于我本地沒有寫路由,所以我用GetPageRoute生成了它需要的route。

?(route) => ?(route as GetPageRoute).routeName == ?'/BPage'); ?這里其實(shí)是在判斷,如果routeName == /BPage ,那么返回到此為止 ,否則會一直往前面的頁面返回。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容