import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'container_transform_flight.dart'; import 'container_transform_gestures.dart'; import 'container_transform_source.dart'; import 'motion.dart'; /// How long a page released mid-swipe takes to settle, as in iOS. const Duration _droppedSwipeDuration = Duration(milliseconds: 350); /// A full-screen page that grows out of the item that opened it and shrinks /// back into it on Back (Material's container transform). /// /// [link] resolves the item each time the page animates, so closing after /// another item took the page shrinks into that one. When the item is not on /// screen, or the system asks for reduced motion, the page fades instead. /// The list behind stays put. A Cupertino host keeps the native swipe back /// from the start edge and Android its predictive back; both drive the /// shrink with the finger. The animation-speed setting scales every /// duration through the scheduler clock. class ContainerTransformPageRoute extends PageRoute implements ContainerTransformGestureRoute { ContainerTransformPageRoute({ required this.builder, required this.link, required this.cupertino, super.settings, }); final WidgetBuilder builder; final ContainerTransformLink link; final bool cupertino; bool get _reducedMotion { final context = navigator?.context; return context != null && (MediaQuery.maybeDisableAnimationsOf(context) ?? false); } @override Duration get transitionDuration => _reducedMotion ? Motion.quick : Motion.containerTransform; @override Duration get reverseTransitionDuration => _reducedMotion ? Motion.quick : Motion.containerTransformReverse; @override bool get opaque => true; @override bool get maintainState => true; @override Color? get barrierColor => null; @override String? get barrierLabel => null; /// The list this page grew from never moves under it. @override bool canTransitionFrom(TransitionRoute previousRoute) => false; @override Widget buildPage( BuildContext context, Animation animation, Animation secondaryAnimation, ) => Semantics( scopesRoute: true, explicitChildNodes: true, child: builder(context), ); @override Widget buildTransitions( BuildContext context, Animation animation, Animation secondaryAnimation, Widget child, ) { final reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false; final pageColor = cupertino ? CupertinoDynamicColor.resolve( CupertinoTheme.of(context).scaffoldBackgroundColor, context, ) : Theme.of(context).scaffoldBackgroundColor; Widget page = ContainerTransformFlight( animation: animation, origin: reduced ? null : link.resolve, closing: () => animation.status == AnimationStatus.reverse || popGestureInProgress, tracking: () => popGestureInProgress, pageColor: pageColor, scrimColor: cupertino ? CupertinoColors.black : Theme.of(context).colorScheme.scrim, child: child, ); if (cupertino) { // A page pushed over this one keeps the native parallax. page = CupertinoPageTransition( primaryRouteAnimation: kAlwaysCompleteAnimation, secondaryRouteAnimation: secondaryAnimation, linearTransition: popGestureInProgress, child: page, ); } return ContainerTransformBackGestures( route: this, cupertino: cupertino, child: page, ); } @override bool get backGestureEnabled => isCurrent && popGestureEnabled && !(navigator?.userGestureInProgress ?? true); @override void startSwipeBack() => navigator?.didStartUserGesture(); @override void updateSwipeBack(double fraction) { if (!isCurrent) return; controller?.value -= fraction; } @override void endSwipeBack(double velocity) { final controller = this.controller; final navigator = this.navigator; if (controller == null || navigator == null) return; // As in iOS: a fling decides; otherwise the page goes back past halfway. final bool stay; if (!isCurrent) { stay = isActive; } else if (velocity.abs() >= 1) { stay = velocity <= 0; } else { stay = controller.value > 0.5; } const curve = Curves.fastEaseInToSlowEaseOut; if (stay) { controller.animateTo(1, duration: _droppedSwipeDuration, curve: curve); } else { if (isCurrent) navigator.pop(); if (controller.isAnimating) { controller.animateBack( 0, duration: _droppedSwipeDuration, curve: curve, ); } } if (!controller.isAnimating) { navigator.didStopUserGesture(); return; } // The gesture lasts until the page settles, so the shrink keeps // following linearly instead of jumping onto the eased curve. late final AnimationStatusListener settle; settle = (_) { navigator.didStopUserGesture(); controller.removeStatusListener(settle); }; controller.addStatusListener(settle); } }