Flutter GETX 返回指定页面

2022-05-24  本文已影响0人  文豪英杰

在Flutter中,如果你的页面由 A->B->C->D->E ,然后在E页面需要返回到B页面,其余的页面依次返回,你可以在D页面跳转到E页面的时候这样写:


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');  这里其实是在判断,如果routeName == /BPage ,那么返回到此为止 ,否则会一直往前面的页面返回。

上一篇下一篇

猜你喜欢

热点阅读