在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 ,那么返回到此為止 ,否則會一直往前面的頁面返回。