[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route informed animation 2 #150031

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add delegate route logic
  • Loading branch information
MitchellGoodwin committed Jun 11, 2024
commit 00b7dda007eee4912575a6fa9bd30e6fb9677583
17 changes: 12 additions & 5 deletions packages/flutter/lib/src/cupertino/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mixin CupertinoRouteTransitionMixin<T> on PageRoute<T> {
@override
bool canTransitionTo(TransitionRoute<dynamic> nextRoute) {
// Don't perform outgoing animation if the next route is a fullscreen dialog.
return nextRoute is CupertinoRouteTransitionMixin && !nextRoute.fullscreenDialog;
return !(nextRoute is CupertinoRouteTransitionMixin && nextRoute.fullscreenDialog);
}

@override
Expand Down Expand Up @@ -487,10 +487,17 @@ class _CupertinoPageTransitionState extends State<CupertinoPageTransition> {
Widget build(BuildContext context) {
assert(debugCheckHasDirectionality(context));
final TextDirection textDirection = Directionality.of(context);
return SlideTransition(
position: _secondaryPositionAnimation,
textDirection: textDirection,
transformHitTests: false,
return DelegatedTransition(
context: context,
animation: widget.secondaryRouteAnimation,
builder: (BuildContext context, Widget? child) {
return SlideTransition(
position: _secondaryPositionAnimation,
textDirection: textDirection,
transformHitTests: false,
child: child,
);
},
child: SlideTransition(
position: _primaryPositionAnimation,
textDirection: textDirection,
Expand Down
119 changes: 97 additions & 22 deletions packages/flutter/lib/src/material/page_transitions_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ class _OpenUpwardsPageTransition extends StatefulWidget {
// Used by all of the transition animations.
static const Curve _transitionCurve = Cubic(0.20, 0.00, 0.00, 1.00);

static Widget delegateTransition(BuildContext context, Widget? child, Animation<double> secondaryAnimation) {
final Animation<Offset> secondaryTranslationAnimation = _secondaryTranslationTween.animate(
CurvedAnimation(
parent: secondaryAnimation,
curve: _transitionCurve,
reverseCurve: _transitionCurve.flipped,
),
);

final CurvedAnimation primaryAnimation = CurvedAnimation(
parent: ReverseAnimation(secondaryAnimation),
curve: _transitionCurve,
reverseCurve: _transitionCurve.flipped,
);

final Animation<Offset> primaryTranslationAnimation = _primaryTranslationTween.animate(primaryAnimation);

return AnimatedBuilder(
animation: secondaryAnimation,
child: FractionalTranslation(
translation: secondaryTranslationAnimation.value,
child: child,
),
builder: (BuildContext context, Widget? child) {
return FractionalTranslation(
translation: secondaryTranslationAnimation.value,
child: child,
);
},
);
}

final Animation<double> animation;
final Animation<double> secondaryAnimation;
final Widget child;
Expand Down Expand Up @@ -290,28 +322,35 @@ class _ZoomPageTransition extends StatelessWidget {
child: child,
);
},
child: DualTransitionBuilder(
animation: ReverseAnimation(secondaryAnimation),
forwardBuilder: (
BuildContext context,
Animation<double> animation,
Widget? child,
) {
return _ZoomEnterTransition(
animation: animation,
allowSnapshotting: allowSnapshotting && allowEnterRouteSnapshotting ,
reverse: true,
child: child,
);
},
reverseBuilder: (
BuildContext context,
Animation<double> animation,
Widget? child,
) {
return _ZoomExitTransition(
animation: animation,
allowSnapshotting: allowSnapshotting,
child: DelegatedTransition(
context: context,
animation: secondaryAnimation,
builder: (BuildContext context, Widget? child) {
return DualTransitionBuilder(
animation: ReverseAnimation(secondaryAnimation),
forwardBuilder: (
BuildContext context,
Animation<double> animation,
Widget? child,
) {
return _ZoomEnterTransition(
animation: animation,
allowSnapshotting: allowSnapshotting && allowEnterRouteSnapshotting ,
reverse: true,
child: child,
);
},
reverseBuilder: (
BuildContext context,
Animation<double> animation,
Widget? child,
) {
return _ZoomExitTransition(
animation: animation,
allowSnapshotting: allowSnapshotting,
child: child,
);
},
child: child,
);
},
Expand Down Expand Up @@ -624,6 +663,11 @@ class OpenUpwardsPageTransitionsBuilder extends PageTransitionsBuilder {
/// Android P.
const OpenUpwardsPageTransitionsBuilder();

/// Delegate animation
static Widget delegateTransition(BuildContext context, Widget? child, Animation<double> secondaryAnimation) {
return _OpenUpwardsPageTransition.delegateTransition(context, child, secondaryAnimation);
}

@override
Widget buildTransitions<T>(
PageRoute<T>? route,
Expand Down Expand Up @@ -702,6 +746,37 @@ class ZoomPageTransitionsBuilder extends PageTransitionsBuilder {
// for the Impeller backend.
static const bool _kProfileForceDisableSnapshotting = bool.fromEnvironment('flutter.benchmarks.force_disable_snapshot');

/// The delegated transition.
static Widget delegateTransition(BuildContext context, Widget? child, Animation<double> secondaryAnimation) {
return DualTransitionBuilder(
animation: ReverseAnimation(secondaryAnimation),
forwardBuilder: (
BuildContext context,
Animation<double> animation,
Widget? child,
) {
return _ZoomEnterTransition(
animation: animation,
allowSnapshotting: true ,
reverse: true,
child: child,
);
},
reverseBuilder: (
BuildContext context,
Animation<double> animation,
Widget? child,
) {
return _ZoomExitTransition(
animation: animation,
allowSnapshotting: true,
child: child,
);
},
child: child,
);
}

@override
Widget buildTransitions<T>(
PageRoute<T> route,
Expand Down
12 changes: 11 additions & 1 deletion packages/flutter/lib/src/widgets/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4847,8 +4847,9 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
/// * [restorablePush], which pushes a route that can be restored during
/// state restoration.
@optionalTypeArgs
Future<T?> push<T extends Object?>(Route<T> route) {
Future<T?> push<T extends Object?>(Route<T> route, [Widget Function(BuildContext context, Widget? child, Animation<double> animation)? delegateBuilder]) {
_pushEntry(_RouteEntry(route, pageBased: false, initialState: _RouteLifecycle.push));
delegateTransitionBuilder = delegateBuilder;
return route.popped;
}

Expand Down Expand Up @@ -5516,6 +5517,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
/// Notifies its listeners if the value of [userGestureInProgress] changes.
final ValueNotifier<bool> userGestureInProgressNotifier = ValueNotifier<bool>(false);

/// Notifies its listeners if there is a delegate transition from the top route.
final ValueNotifier<Widget Function(BuildContext context, Widget? child, Animation<double> animation)?> delegateTransitionBuilderNotifier = ValueNotifier<Widget Function(BuildContext context, Widget? child, Animation<double> animation)?>(null);

/// Sets the delegate transition.
set delegateTransitionBuilder(Widget Function(BuildContext context, Widget? child, Animation<double> animation)? builder) => delegateTransitionBuilderNotifier.value = builder;

/// Gets the delegate transition.
Widget Function(BuildContext context, Widget? child, Animation<double> animation)? get delegateTransitionBuilder => delegateTransitionBuilderNotifier.value;

/// The navigator is being controlled by a user gesture.
///
/// For example, called when the user beings an iOS back gesture.
Expand Down
27 changes: 27 additions & 0 deletions packages/flutter/lib/src/widgets/transitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter/rendering.dart';
import 'basic.dart';
import 'container.dart';
import 'framework.dart';
import 'navigator.dart';
import 'text.dart';

export 'package:flutter/rendering.dart' show RelativeRect;
Expand Down Expand Up @@ -138,6 +139,32 @@ class _AnimatedState extends State<AnimatedWidget> {
Widget build(BuildContext context) => widget.build(context);
}

/// Delegates to transition from navigator if available, or returns default.
class DelegatedTransition extends AnimatedBuilder {

/// Creates a DelegatedTransiton
const DelegatedTransition({
super.key,
super.child,
required super.animation,
required super.builder,
required this.context,
});

/// context
final BuildContext context;

@override
Widget build(BuildContext context) {
final NavigatorState navigatorState = Navigator.of(context);
final Widget Function(BuildContext context, Widget? child, Animation<double> animation)? delegateTransitionBuilder = navigatorState.delegateTransitionBuilder;
if (delegateTransitionBuilder == null) {
return builder(context, child);
}
return delegateTransitionBuilder(context, child, animation as Animation<double>);
}
}

/// Animates the position of a widget relative to its normal position.
///
/// The translation is expressed as an [Offset] scaled to the child's size. For
Expand Down