PageTransitionSwitcher class

A widget that transitions from an old child to a new child whenever child changes using an animation specified by transitionBuilder.

This is a variation of an AnimatedSwitcher, but instead of using the same transition for enter and exit, two separate transitions can be specified, similar to how the enter and exit transitions of a PageRoute are defined.

When a new child is specified, the transitionBuilder is effectively applied twice, once to the old child and once to the new one. When reverse is false, the old child's secondaryAnimation runs forward, and the value of its primaryAnimation is usually fixed at 1.0. The new child's primaryAnimation runs forward and the value of its secondaryAnimation is usually fixed at 0.0. The widget returned by the transitionBuilder can incorporate both animations. It will use the primary animation to define how its child appears, and the secondary animation to define how its child disappears. This is similar to the transition associated with pushing a new PageRoute on top of another.

When reverse is true, the old child's primaryAnimation runs in reverse and the value of its secondaryAnimation is usually fixed at 0.0. The new child's secondaryAnimation runs in reverse and the value of its primaryAnimation is usually fixed at 1.0. This is similar to popping a PageRoute to reveal another PageRoute underneath it.

This process is the same as the one used by PageRoute.buildTransitions.

The following example shows a transitionBuilder that slides out the old child to the right (driven by the secondaryAnimation) while the new child fades in (driven by the primaryAnimation):

transitionBuilder: (
  Widget child,
  Animation<double> primaryAnimation,
  Animation<double> secondaryAnimation,
) {
  return SlideTransition(
    position: Tween<Offset>(
      end: const Offset(1.5, 0.0),
    child: FadeTransition(
      opacity: Tween<double>(
        begin: 0.0,
        end: 1.0,
      child: child,

If the children are swapped fast enough (i.e. before duration elapses), more than one old child can exist and be transitioning out while the newest one is transitioning in.

If the new child is the same widget type and key as the old child, but with different parameters, then PageTransitionSwitcher will not do a transition between them, since as far as the framework is concerned, they are the same widget and the existing widget can be updated with the new parameters. To force the transition to occur, set a Key on each child widget that you wish to be considered unique (typically a ValueKey on the widget data that distinguishes this child from the others). For example, changing the child from SizedBox(width: 10) to SizedBox(width: 100) would not trigger a transition but changing the child from SizedBox(width: 10) to SizedBox(key: Key('foo'), width: 100) would. Similarly, changing the child to Container(width: 10) would trigger a transition.

The same key can be used for a new child as was used for an already-outgoing child; the two will not be considered related. For example, if a progress indicator with key A is first shown, then an image with key B, then another progress indicator with key A again, all in rapid succession, then the old progress indicator and the image will be fading out while a new progress indicator is fading in.

PageTransitionSwitcher uses the layoutBuilder property to lay out the old and new child widgets. By default, defaultLayoutBuilder is used. See the documentation for layoutBuilder for suggestions on how to configure the layout of the incoming and outgoing child widgets if defaultLayoutBuilder is not your desired layout.



PageTransitionSwitcher({Key? key, Duration duration = const Duration(milliseconds: 300), bool reverse = false, required PageTransitionSwitcherTransitionBuilder transitionBuilder, PageTransitionSwitcherLayoutBuilder layoutBuilder = defaultLayoutBuilder, Widget? child})
Creates a PageTransitionSwitcher.


child Widget?
The current child widget to display.
duration Duration
The duration of the transition from the old child value to the new one.
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
layoutBuilder PageTransitionSwitcherLayoutBuilder
A function that wraps all of the children that are transitioning out, and the child that's transitioning in, with a widget that lays all of them out. This is called every time this widget is built. The function must not return null.
reverse bool
Indicates whether the new child will visually appear on top of or underneath the old child.
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
transitionBuilder PageTransitionSwitcherTransitionBuilder
A function that wraps a new child with a primary and secondary animation set define how the child appears and disappears.


createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
createState() State<PageTransitionSwitcher>
Creates the mutable state for this widget at a given location in the tree.
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
toString({DiagnosticLevel minLevel =}) String
A string representation of this object.
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
toStringShort() String
A short, textual description of this widget.


operator ==(Object other) bool
The equality operator.

Static Methods

defaultLayoutBuilder(List<Widget> entries) Widget
The default layout builder for PageTransitionSwitcher.