|
| 1 | +/// |
| 2 | +/// [Author] Alex (https://github.com/AlexVincent525) |
| 3 | +/// [Date] 2020-05-31 11:23 |
| 4 | +/// |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +// Zooms and fades a new page in, zooming out the previous page. This transition |
| 8 | +// is designed to match the Android 10 activity transition. |
| 9 | +class ZoomPageTransition extends StatefulWidget { |
| 10 | + const ZoomPageTransition({ |
| 11 | + Key key, |
| 12 | + this.animation, |
| 13 | + this.secondaryAnimation, |
| 14 | + this.child, |
| 15 | + }) : super(key: key); |
| 16 | + |
| 17 | + // The scrim obscures the old page by becoming increasingly opaque. |
| 18 | + static final Tween<double> _scrimOpacityTween = Tween<double>( |
| 19 | + begin: 0.0, |
| 20 | + end: 0.60, |
| 21 | + ); |
| 22 | + |
| 23 | + // A curve sequence that is similar to the 'fastOutExtraSlowIn' curve used in |
| 24 | + // the native transition. |
| 25 | + static final List<TweenSequenceItem<double>> |
| 26 | + fastOutExtraSlowInTweenSequenceItems = <TweenSequenceItem<double>>[ |
| 27 | + TweenSequenceItem<double>( |
| 28 | + tween: Tween<double>(begin: 0.0, end: 0.4) |
| 29 | + .chain(CurveTween(curve: const Cubic(0.05, 0.0, 0.133333, 0.06))), |
| 30 | + weight: 0.166666, |
| 31 | + ), |
| 32 | + TweenSequenceItem<double>( |
| 33 | + tween: Tween<double>(begin: 0.4, end: 1.0) |
| 34 | + .chain(CurveTween(curve: const Cubic(0.208333, 0.82, 0.25, 1.0))), |
| 35 | + weight: 1.0 - 0.166666, |
| 36 | + ), |
| 37 | + ]; |
| 38 | + static final TweenSequence<double> _scaleCurveSequence = |
| 39 | + TweenSequence<double>(fastOutExtraSlowInTweenSequenceItems); |
| 40 | + static final FlippedTweenSequence flippedScaleCurveSequence = |
| 41 | + FlippedTweenSequence(fastOutExtraSlowInTweenSequenceItems); |
| 42 | + |
| 43 | + final Animation<double> animation; |
| 44 | + final Animation<double> secondaryAnimation; |
| 45 | + final Widget child; |
| 46 | + |
| 47 | + @override |
| 48 | + _ZoomPageTransitionState createState() => _ZoomPageTransitionState(); |
| 49 | +} |
| 50 | + |
| 51 | +class _ZoomPageTransitionState extends State<ZoomPageTransition> { |
| 52 | + AnimationStatus _currentAnimationStatus; |
| 53 | + AnimationStatus _lastAnimationStatus; |
| 54 | + |
| 55 | + @override |
| 56 | + void initState() { |
| 57 | + super.initState(); |
| 58 | + widget.animation.addStatusListener((AnimationStatus animationStatus) { |
| 59 | + _lastAnimationStatus = _currentAnimationStatus; |
| 60 | + _currentAnimationStatus = animationStatus; |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // This check ensures that the animation reverses the original animation if |
| 65 | + // the transition were interruped midway. This prevents a disjointed |
| 66 | + // experience since the reverse animation uses different fade and scaling |
| 67 | + // curves. |
| 68 | + bool get _transitionWasInterrupted { |
| 69 | + bool wasInProgress = false; |
| 70 | + bool isInProgress = false; |
| 71 | + |
| 72 | + switch (_currentAnimationStatus) { |
| 73 | + case AnimationStatus.completed: |
| 74 | + case AnimationStatus.dismissed: |
| 75 | + isInProgress = false; |
| 76 | + break; |
| 77 | + case AnimationStatus.forward: |
| 78 | + case AnimationStatus.reverse: |
| 79 | + isInProgress = true; |
| 80 | + break; |
| 81 | + } |
| 82 | + switch (_lastAnimationStatus) { |
| 83 | + case AnimationStatus.completed: |
| 84 | + case AnimationStatus.dismissed: |
| 85 | + wasInProgress = false; |
| 86 | + break; |
| 87 | + case AnimationStatus.forward: |
| 88 | + case AnimationStatus.reverse: |
| 89 | + wasInProgress = true; |
| 90 | + break; |
| 91 | + } |
| 92 | + return wasInProgress && isInProgress; |
| 93 | + } |
| 94 | + |
| 95 | + @override |
| 96 | + Widget build(BuildContext context) { |
| 97 | + final Animation<double> _forwardScrimOpacityAnimation = widget.animation |
| 98 | + .drive(ZoomPageTransition._scrimOpacityTween |
| 99 | + .chain(CurveTween(curve: const Interval(0.2075, 0.4175)))); |
| 100 | + |
| 101 | + final Animation<double> _forwardEndScreenScaleTransition = widget.animation |
| 102 | + .drive(Tween<double>(begin: 0.85, end: 1.00) |
| 103 | + .chain(ZoomPageTransition._scaleCurveSequence)); |
| 104 | + |
| 105 | + final Animation<double> _forwardStartScreenScaleTransition = |
| 106 | + widget.secondaryAnimation.drive(Tween<double>(begin: 1.00, end: 1.05) |
| 107 | + .chain(ZoomPageTransition._scaleCurveSequence)); |
| 108 | + |
| 109 | + final Animation<double> _forwardEndScreenFadeTransition = widget.animation |
| 110 | + .drive(Tween<double>(begin: 0.0, end: 1.00) |
| 111 | + .chain(CurveTween(curve: const Interval(0.125, 0.250)))); |
| 112 | + |
| 113 | + final Animation<double> _reverseEndScreenScaleTransition = |
| 114 | + widget.secondaryAnimation.drive(Tween<double>(begin: 1.00, end: 1.10) |
| 115 | + .chain(ZoomPageTransition.flippedScaleCurveSequence)); |
| 116 | + |
| 117 | + final Animation<double> _reverseStartScreenScaleTransition = |
| 118 | + widget.animation.drive(Tween<double>(begin: 0.9, end: 1.0) |
| 119 | + .chain(ZoomPageTransition.flippedScaleCurveSequence)); |
| 120 | + |
| 121 | + final Animation<double> _reverseStartScreenFadeTransition = widget.animation |
| 122 | + .drive(Tween<double>(begin: 0.0, end: 1.00) |
| 123 | + .chain(CurveTween(curve: const Interval(1 - 0.2075, 1 - 0.0825)))); |
| 124 | + |
| 125 | + return AnimatedBuilder( |
| 126 | + animation: widget.animation, |
| 127 | + builder: (BuildContext context, Widget child) { |
| 128 | + if (widget.animation.status == AnimationStatus.forward || |
| 129 | + _transitionWasInterrupted) { |
| 130 | + return Container( |
| 131 | + color: |
| 132 | + Colors.black.withOpacity(_forwardScrimOpacityAnimation.value), |
| 133 | + child: FadeTransition( |
| 134 | + opacity: _forwardEndScreenFadeTransition, |
| 135 | + child: ScaleTransition( |
| 136 | + scale: _forwardEndScreenScaleTransition, |
| 137 | + child: child, |
| 138 | + ), |
| 139 | + ), |
| 140 | + ); |
| 141 | + } else if (widget.animation.status == AnimationStatus.reverse) { |
| 142 | + return ScaleTransition( |
| 143 | + scale: _reverseStartScreenScaleTransition, |
| 144 | + child: FadeTransition( |
| 145 | + opacity: _reverseStartScreenFadeTransition, |
| 146 | + child: child, |
| 147 | + ), |
| 148 | + ); |
| 149 | + } |
| 150 | + return child; |
| 151 | + }, |
| 152 | + child: AnimatedBuilder( |
| 153 | + animation: widget.secondaryAnimation, |
| 154 | + builder: (BuildContext context, Widget child) { |
| 155 | + if (widget.secondaryAnimation.status == AnimationStatus.forward || |
| 156 | + _transitionWasInterrupted) { |
| 157 | + return ScaleTransition( |
| 158 | + scale: _forwardStartScreenScaleTransition, |
| 159 | + child: child, |
| 160 | + ); |
| 161 | + } else if (widget.secondaryAnimation.status == |
| 162 | + AnimationStatus.reverse) { |
| 163 | + return ScaleTransition( |
| 164 | + scale: _reverseEndScreenScaleTransition, |
| 165 | + child: child, |
| 166 | + ); |
| 167 | + } |
| 168 | + return child; |
| 169 | + }, |
| 170 | + child: widget.child, |
| 171 | + ), |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments