Files
pipelines/app/lib/ui/motion/container_transform_route.dart
T
alan 9adee6cc60 Remove Cupertino UI branches
Keep the public Cupertino-compatible parameters while retaining only the Material implementations and tests. The app is Material-only, so deleting unreachable Cupertino branches reduces dead code without changing the active path.
2026-11-11 02:41:26 +00:00

155 lines
4.4 KiB
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. Android's predictive back drives the shrink
/// with the finger. The animation-speed setting scales every duration
/// through the scheduler clock.
class ContainerTransformPageRoute<T> extends PageRoute<T>
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<dynamic> previousRoute) => false;
@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) => Semantics(
scopesRoute: true,
explicitChildNodes: true,
child: builder(context),
);
@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
final reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false;
final page = ContainerTransformFlight(
animation: animation,
origin: reduced ? null : link.resolve,
closing: () =>
animation.status == AnimationStatus.reverse || popGestureInProgress,
tracking: () => popGestureInProgress,
pageColor: Theme.of(context).scaffoldBackgroundColor,
scrimColor: Theme.of(context).colorScheme.scrim,
child: child,
);
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);
}
}