buildTransitions method

  1. @override
Widget buildTransitions(
  1. BuildContext context,
  2. Animation<double> animation,
  3. Animation<double> secondaryAnimation,
  4. Widget child
)
override

Override this method to wrap the child with one or more transition widgets that define how the route arrives on and leaves the screen.

By default, the child (which contains the widget returned by buildPage) is not wrapped in any transition widgets.

The buildTransitions method, in contrast to buildPage, is called each time the Route's state changes while it is visible (e.g. if the value of canPop changes on the active route).

The buildTransitions method is typically used to define transitions that animate the new topmost route's comings and goings. When the Navigator pushes a route on the top of its stack, the new route's primary animation runs from 0.0 to 1.0. When the Navigator pops the topmost route, e.g. because the use pressed the back button, the primary animation runs from 1.0 to 0.0.

{@tool snippet} The following example uses the primary animation to drive a SlideTransition that translates the top of the new route vertically from the bottom of the screen when it is pushed on the Navigator's stack. When the route is popped the SlideTransition translates the route from the top of the screen back to the bottom.

We've used PageRouteBuilder to demonstrate the buildTransitions method here. The body of an override of the buildTransitions method would be defined in the same way.

PageRouteBuilder(
  pageBuilder: (BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
  ) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hello')),
      body: const Center(
        child: Text('Hello World'),
      ),
    );
  },
  transitionsBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child,
   ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(0.0, 1.0),
        end: Offset.zero,
      ).animate(animation),
      child: child, // child is the value returned by pageBuilder
    );
  },
)

{@end-tool}

When the Navigator pushes a route on the top of its stack, the secondaryAnimation can be used to define how the route that was on the top of the stack leaves the screen. Similarly when the topmost route is popped, the secondaryAnimation can be used to define how the route below it reappears on the screen. When the Navigator pushes a new route on the top of its stack, the old topmost route's secondaryAnimation runs from 0.0 to 1.0. When the Navigator pops the topmost route, the secondaryAnimation for the route below it runs from 1.0 to 0.0.

{@tool snippet} The example below adds a transition that's driven by the secondaryAnimation. When this route disappears because a new route has been pushed on top of it, it translates in the opposite direction of the new route. Likewise when the route is exposed because the topmost route has been popped off.

PageRouteBuilder(
  pageBuilder: (BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
  ) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hello')),
      body: const Center(
        child: Text('Hello World'),
      ),
    );
  },
  transitionsBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child,
  ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(0.0, 1.0),
        end: Offset.zero,
      ).animate(animation),
      child: SlideTransition(
        position: Tween<Offset>(
          begin: Offset.zero,
          end: const Offset(0.0, 1.0),
        ).animate(secondaryAnimation),
        child: child,
      ),
     );
  },
)

{@end-tool}

In practice the secondaryAnimation is used pretty rarely.

The arguments to this method are as follows:

  • context: The context in which the route is being built.
  • animation: When the Navigator pushes a route on the top of its stack, the new route's primary animation runs from 0.0 to 1.0. When the Navigator pops the topmost route this animation runs from 1.0 to 0.0.
  • secondaryAnimation: When the Navigator pushes a new route on the top of its stack, the old topmost route's secondaryAnimation runs from 0.0 to 1.0. When the Navigator pops the topmost route, the secondaryAnimation for the route below it runs from 1.0 to 0.0.
  • child, the page contents, as returned by buildPage.

See also:

  • buildPage, which is used to describe the actual contents of the page, and whose result is passed to the child argument of this method.

Implementation

@override
Widget buildTransitions(BuildContext context, Animation<double> animation,
    Animation<double> secondaryAnimation, Widget child) {
  final finalCurve = curve ?? Get.defaultTransitionCurve;
  final hasCurve = curve != null;
  if (fullscreenDialog && transition == null) {
    /// by default, if no curve is defined, use Cupertino transition in the
    /// default way (no linearTransition)... otherwise take the curve passed.
    return CupertinoFullscreenDialogTransition(
        primaryRouteAnimation: hasCurve
            ? CurvedAnimation(parent: animation, curve: finalCurve)
            : animation,
        secondaryRouteAnimation: secondaryAnimation,
        child: child,
        linearTransition: hasCurve);
  }
  if (customTransition != null) {
    return customTransition.buildTransition(
      context,
      finalCurve,
      alignment,
      animation,
      secondaryAnimation,
      popGesture ?? Get.defaultPopGesture
          ? _CupertinoBackGestureDetector<T>(
              enabledCallback: () => _isPopGestureEnabled<T>(this),
              onStartPopGesture: () => _startPopGesture<T>(this),
              child: child)
          : child,
    );
  }

  /// Apply the curve by default...
  final iosAnimation = animation;
  animation = CurvedAnimation(parent: animation, curve: finalCurve);

  switch (transition ?? Get.defaultTransition) {
    case Transition.leftToRight:
      return SlideLeftTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.downToUp:
      return SlideDownTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.upToDown:
      return SlideTopTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.noTransition:
      return popGesture ?? Get.defaultPopGesture
          ? _CupertinoBackGestureDetector<T>(
              enabledCallback: () => _isPopGestureEnabled<T>(this),
              onStartPopGesture: () => _startPopGesture<T>(this),
              child: child)
          : child;

    case Transition.rightToLeft:
      return SlideRightTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.zoom:
      return ZoomInTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.fadeIn:
      return FadeInTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.rightToLeftWithFade:
      return RightToLeftFadeTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.leftToRightWithFade:
      return LeftToRightFadeTransition().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.cupertino:
      return CupertinoPageTransitionsBuilder().buildTransitions(
          this,
          context,
          iosAnimation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.size:
      return SizeTransitions().buildTransitions(
          context,
          curve,
          alignment,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.fade:
      return FadeUpwardsPageTransitionsBuilder().buildTransitions(
          this,
          context,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.topLevel:
      return ZoomPageTransitionsBuilder().buildTransitions(
          this,
          context,
          animation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    case Transition.native:
      return PageTransitionsTheme().buildTransitions(
          this,
          context,
          iosAnimation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);

    default:
      if (Get.customTransition != null) {
        return Get.customTransition.buildTransition(
            context, curve, alignment, animation, secondaryAnimation, child);
      }

      return PageTransitionsTheme().buildTransitions(
          this,
          context,
          iosAnimation,
          secondaryAnimation,
          popGesture ?? Get.defaultPopGesture
              ? _CupertinoBackGestureDetector<T>(
                  enabledCallback: () => _isPopGestureEnabled<T>(this),
                  onStartPopGesture: () => _startPopGesture<T>(this),
                  child: child)
              : child);
  }
}