2

In PageRouteBuilder's transitionBuilder method I'm using the code bellow but the secondary animation is ALWAYS the reverse of the animation. How can we have a slide in transition on a page and fade out on exit?

Widget leftToRightSlideTransition(context, animation, secondaryAnimation, child) {
  final Tween<double> doubleTween = Tween<double>(begin: 1.0, end: 0.0);
  final Animation<double> animDouble = doubleTween.animate(secondaryAnimation);
  final fadeTransition = FadeTransition(opacity: animDouble, child: child);

  var begin = Offset(-1.0, 0.0);
  var end = Offset.zero;
  var tween = Tween(begin: begin, end: end).chain(
    CurveTween(curve: Curves.easeIn),
  );
  return SlideTransition(
    position: tween.animate(animation),
    child: fadeTransition,
  );
}
Sergio
  • 2,346
  • 2
  • 24
  • 28

1 Answers1

3

If you want custom animations for enter and exit pages you need put SlideTransition to Stack widget for enter page and exit page. Here is exemple how should look iOS SlideTransition (Cupertino).

import 'package:flutter/material.dart';

class CupertinoRoute extends PageRouteBuilder {
  final Widget enterPage;
  final Widget exitPage;
  CupertinoRoute({this.exitPage, this.enterPage})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) {
            return enterPage;
          },
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) {
            return Stack(
              children: <Widget>[
                SlideTransition(
                  position: Tween<Offset>(
                    begin: const Offset(0.0, 0.0),
                    end: const Offset(-0.33, 0.0),
                  ).animate(
                      CurvedAnimation(
                      parent: animation,
                      curve: Curves.linearToEaseOut,
                      reverseCurve: Curves.easeInToLinear,
                    ),
                      ),
                  child: exitPage,
                ),
                SlideTransition(
                  position: Tween<Offset>(
                    begin: const Offset(1.0, 0.0),
                    end: Offset.zero,
                  ).animate(
                      CurvedAnimation(
                      parent: animation,
                      curve: Curves.linearToEaseOut,
                      reverseCurve: Curves.easeInToLinear,
                    ),
                      ),
                  child: enterPage,
                )
              ],
            );
          },
        );
}

and here you can customize animations how you want to both SlideTransition.

// And use it like this
Navigator.push(
        context,
        CupertinoRoute(
            exitPage: HomeScreen(),
            enterPage: SecondScreen()));
Arnas
  • 802
  • 1
  • 6
  • 14
  • Thanks, Will try this solution. It'll require to know previously which routes are currently loaded to pass to the enter page and exit page, so it's another thing to maintain. – Sergio Oct 09 '20 at 15:04
  • I have the same problem [here](https://stackoverflow.com/questions/67164148/how-to-animate-routes-transtion-relative-to-the-previous-one-in-flutter/67280651#67280651) and this is a hack, it looks bad when the previous route has a state change like scroll...an imporoved answer is apreciated – bihire boris Apr 27 '21 at 16:44
  • I have added generic parameter to class decloration – Georgiy Chebotarev Mar 27 '23 at 12:52