How to change navigation animation using Flutter

前端 未结 5 1507
南笙
南笙 2020-12-12 16:30

Is there any way to change the default animation when navigating to/from a page in Flutter?

5条回答
  •  情话喂你
    2020-12-12 16:59

    You can subclass MaterialPageRouteand override buildTransitions.

    Eg:

    class MyCustomRoute extends MaterialPageRoute {
      MyCustomRoute({ WidgetBuilder builder, RouteSettings settings })
          : super(builder: builder, settings: settings);
    
      @override
      Widget buildTransitions(BuildContext context,
          Animation animation,
          Animation secondaryAnimation,
          Widget child) {
        if (settings.isInitialRoute)
          return child;
        // Fades between routes. (If you don't want any animation,
        // just return child.)
        return new FadeTransition(opacity: animation, child: child);
      }
    }
    

    to use :

    new RaisedButton(
                    child: new Text('Goto'),
                    onPressed: (){
                      Navigator.push(
                        context,
                        new MyCustomRoute(builder: (context) => new SecondPage()),
                      );
                    }),
    

    Replace fade transition with your animation

提交回复
热议问题