From 65fbd52b1afc2abe7241e9121702037748a67860 Mon Sep 17 00:00:00 2001 From: Dimil Kalathiya <102401667+Dimilkalathiya@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:10:43 +0530 Subject: [PATCH] fixes cupertino page transition leak (#147133) --- packages/flutter/lib/src/cupertino/route.dart | 141 ++++++++++++------ 1 file changed, 97 insertions(+), 44 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index b9a0659bf9..f698d1aad7 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -378,57 +378,110 @@ class CupertinoPage extends Page { /// /// The page slides in from the right and exits in reverse. It also shifts to the left in /// a parallax motion when another page enters to cover it. -class CupertinoPageTransition extends StatelessWidget { +class CupertinoPageTransition extends StatefulWidget { /// Creates an iOS-style page transition. /// - /// * `primaryRouteAnimation` is a linear route animation from 0.0 to 1.0 - /// when this screen is being pushed. - /// * `secondaryRouteAnimation` is a linear route animation from 0.0 to 1.0 - /// when another screen is being pushed on top of this one. - /// * `linearTransition` is whether to perform the transitions linearly. - /// Used to precisely track back gesture drags. - CupertinoPageTransition({ + const CupertinoPageTransition({ super.key, - required Animation primaryRouteAnimation, - required Animation secondaryRouteAnimation, + required this.primaryRouteAnimation, + required this.secondaryRouteAnimation, required this.child, - required bool linearTransition, - }) : _primaryPositionAnimation = - (linearTransition - ? primaryRouteAnimation - : CurvedAnimation( - parent: primaryRouteAnimation, - curve: Curves.fastEaseInToSlowEaseOut, - reverseCurve: Curves.fastEaseInToSlowEaseOut.flipped, - ) - ).drive(_kRightMiddleTween), - _secondaryPositionAnimation = - (linearTransition - ? secondaryRouteAnimation - : CurvedAnimation( - parent: secondaryRouteAnimation, - curve: Curves.linearToEaseOut, - reverseCurve: Curves.easeInToLinear, - ) - ).drive(_kMiddleLeftTween), - _primaryShadowAnimation = - (linearTransition - ? primaryRouteAnimation - : CurvedAnimation( - parent: primaryRouteAnimation, - curve: Curves.linearToEaseOut, - ) - ).drive(_CupertinoEdgeShadowDecoration.kTween); - - // When this page is coming in to cover another page. - final Animation _primaryPositionAnimation; - // When this page is becoming covered by another page. - final Animation _secondaryPositionAnimation; - final Animation _primaryShadowAnimation; + required this.linearTransition, + }); /// The widget below this widget in the tree. final Widget child; + /// * `primaryRouteAnimation` is a linear route animation from 0.0 to 1.0 + /// when this screen is being pushed. + final Animation primaryRouteAnimation; + + /// * `secondaryRouteAnimation` is a linear route animation from 0.0 to 1.0 + /// when another screen is being pushed on top of this one. + final Animation secondaryRouteAnimation; + + /// * `linearTransition` is whether to perform the transitions linearly. + /// Used to precisely track back gesture drags. + final bool linearTransition; + + @override + State createState() => _CupertinoPageTransitionState(); +} + +class _CupertinoPageTransitionState extends State { + + + // When this page is coming in to cover another page. + late Animation _primaryPositionAnimation; + // When this page is becoming covered by another page. + late Animation _secondaryPositionAnimation; + // Shadow of page which is coming in to cover another page. + late Animation _primaryShadowAnimation; + // Curve of primary page which is coming in to cover another page. + CurvedAnimation? _primaryPositionCurve; + // Curve of secondary page which is becoming covered by another page. + CurvedAnimation? _secondaryPositionCurve; + // Curve of primary page's shadow. + CurvedAnimation? _primaryShadowCurve; + + @override + void initState() { + super.initState(); + _setupAnimation(); + } + + @override + void didUpdateWidget(covariant CupertinoPageTransition oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.primaryRouteAnimation != widget.primaryRouteAnimation + || oldWidget.secondaryRouteAnimation != widget.secondaryRouteAnimation + || oldWidget.child != widget.child || oldWidget.linearTransition != widget.linearTransition) { + _disposeCurve(); + _setupAnimation(); + } + } + + @override + void dispose() { + super.dispose(); + _disposeCurve(); + } + + void _disposeCurve() { + _primaryPositionCurve?.dispose(); + _secondaryPositionCurve?.dispose(); + _primaryShadowCurve?.dispose(); + } + + void _setupAnimation() { + _primaryPositionAnimation = + (widget.linearTransition + ? widget.primaryRouteAnimation + : _primaryPositionCurve = CurvedAnimation( + parent: widget.primaryRouteAnimation, + curve: Curves.fastEaseInToSlowEaseOut, + reverseCurve: Curves.fastEaseInToSlowEaseOut.flipped, + ) + ).drive(_kRightMiddleTween); + _secondaryPositionAnimation = + (widget.linearTransition + ? widget.secondaryRouteAnimation + : _secondaryPositionCurve = CurvedAnimation( + parent: widget.secondaryRouteAnimation, + curve: Curves.linearToEaseOut, + reverseCurve: Curves.easeInToLinear, + ) + ).drive(_kMiddleLeftTween); + _primaryShadowAnimation = + (widget.linearTransition + ? widget.primaryRouteAnimation + : _secondaryPositionCurve = CurvedAnimation( + parent: widget.primaryRouteAnimation, + curve: Curves.linearToEaseOut, + ) + ).drive(_CupertinoEdgeShadowDecoration.kTween); + } + @override Widget build(BuildContext context) { assert(debugCheckHasDirectionality(context)); @@ -442,7 +495,7 @@ class CupertinoPageTransition extends StatelessWidget { textDirection: textDirection, child: DecoratedBoxTransition( decoration: _primaryShadowAnimation, - child: child, + child: widget.child, ), ), );