fixes cupertino page transition leak (#147133)

This commit is contained in:
Dimil Kalathiya
2024-04-23 09:10:43 +05:30
committed by GitHub
parent 4938403a7a
commit 65fbd52b1a

View File

@@ -378,57 +378,110 @@ class CupertinoPage<T> extends Page<T> {
///
/// 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<double> primaryRouteAnimation,
required Animation<double> 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<Offset> _primaryPositionAnimation;
// When this page is becoming covered by another page.
final Animation<Offset> _secondaryPositionAnimation;
final Animation<Decoration> _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<double> 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<double> secondaryRouteAnimation;
/// * `linearTransition` is whether to perform the transitions linearly.
/// Used to precisely track back gesture drags.
final bool linearTransition;
@override
State<CupertinoPageTransition> createState() => _CupertinoPageTransitionState();
}
class _CupertinoPageTransitionState extends State<CupertinoPageTransition> {
// When this page is coming in to cover another page.
late Animation<Offset> _primaryPositionAnimation;
// When this page is becoming covered by another page.
late Animation<Offset> _secondaryPositionAnimation;
// Shadow of page which is coming in to cover another page.
late Animation<Decoration> _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,
),
),
);