How to push multiple routes with Flutter Navigator

前端 未结 2 2116
时光取名叫无心
时光取名叫无心 2021-02-19 09:02

If I push a route in flutter to a deep part of my app is there any way to supply additional routes so that the back/up navigation can be customized?

2条回答
  •  囚心锁ツ
    2021-02-19 09:46

    I solved this by pushing several routes in a row without animation to solve transition visibility issues. So far, it works fine on iOS for me. Here's a way to do it.

    Create a NoAnimationPageRoute by extending MaterialPageRoute and overriding buildTransitions:

    class NoAnimationPageRoute extends MaterialPageRoute {
      NoAnimationPageRoute({ WidgetBuilder builder }) : super(builder: builder);
    
      @override
      Widget buildTransitions(
          BuildContext context,
          Animation animation,
          Animation secondaryAnimation,
          Widget child) {
        return child;
      }
    }
    

    Create a function that uses NoAnimationPageRoute:

    Future pushWithoutAnimation(Widget page) {
      Route route = NoAnimationPageRoute(builder: (BuildContext context) => page);
      return Navigator.push(context, route);
    }
    

    Call the function several times in a row:

    pushWithoutAnimation(Screen1());
    pushWithoutAnimation(Screen2());
    pushWithoutAnimation(Screen3());
    

提交回复
热议问题