Making AnimatedCrossFade more null safe (#91187)
This commit is contained in:
parent
bfa4bdbf3b
commit
2ca823d40c
@ -248,7 +248,7 @@ class AnimatedCrossFade extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProviderStateMixin {
|
||||
AnimationController? _controller;
|
||||
late AnimationController _controller;
|
||||
late Animation<double> _firstAnimation;
|
||||
late Animation<double> _secondAnimation;
|
||||
|
||||
@ -261,10 +261,10 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
|
||||
vsync: this,
|
||||
);
|
||||
if (widget.crossFadeState == CrossFadeState.showSecond)
|
||||
_controller!.value = 1.0;
|
||||
_controller.value = 1.0;
|
||||
_firstAnimation = _initAnimation(widget.firstCurve, true);
|
||||
_secondAnimation = _initAnimation(widget.secondCurve, false);
|
||||
_controller!.addStatusListener((AnimationStatus status) {
|
||||
_controller.addStatusListener((AnimationStatus status) {
|
||||
setState(() {
|
||||
// Trigger a rebuild because it depends on _isTransitioning, which
|
||||
// changes its value together with animation status.
|
||||
@ -273,7 +273,7 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
|
||||
}
|
||||
|
||||
Animation<double> _initAnimation(Curve curve, bool inverted) {
|
||||
Animation<double> result = _controller!.drive(CurveTween(curve: curve));
|
||||
Animation<double> result = _controller.drive(CurveTween(curve: curve));
|
||||
if (inverted)
|
||||
result = result.drive(Tween<double>(begin: 1.0, end: 0.0));
|
||||
return result;
|
||||
@ -281,7 +281,7 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller!.dispose();
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@ -289,9 +289,9 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
|
||||
void didUpdateWidget(AnimatedCrossFade oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.duration != oldWidget.duration)
|
||||
_controller!.duration = widget.duration;
|
||||
_controller.duration = widget.duration;
|
||||
if (widget.reverseDuration != oldWidget.reverseDuration)
|
||||
_controller!.reverseDuration = widget.reverseDuration;
|
||||
_controller.reverseDuration = widget.reverseDuration;
|
||||
if (widget.firstCurve != oldWidget.firstCurve)
|
||||
_firstAnimation = _initAnimation(widget.firstCurve, true);
|
||||
if (widget.secondCurve != oldWidget.secondCurve)
|
||||
@ -299,24 +299,24 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
|
||||
if (widget.crossFadeState != oldWidget.crossFadeState) {
|
||||
switch (widget.crossFadeState) {
|
||||
case CrossFadeState.showFirst:
|
||||
_controller!.reverse();
|
||||
_controller.reverse();
|
||||
break;
|
||||
case CrossFadeState.showSecond:
|
||||
_controller!.forward();
|
||||
_controller.forward();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether we're in the middle of cross-fading this frame.
|
||||
bool get _isTransitioning => _controller!.status == AnimationStatus.forward || _controller!.status == AnimationStatus.reverse;
|
||||
bool get _isTransitioning => _controller.status == AnimationStatus.forward || _controller.status == AnimationStatus.reverse;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const Key kFirstChildKey = ValueKey<CrossFadeState>(CrossFadeState.showFirst);
|
||||
const Key kSecondChildKey = ValueKey<CrossFadeState>(CrossFadeState.showSecond);
|
||||
final bool transitioningForwards = _controller!.status == AnimationStatus.completed ||
|
||||
_controller!.status == AnimationStatus.forward;
|
||||
final bool transitioningForwards = _controller.status == AnimationStatus.completed ||
|
||||
_controller.status == AnimationStatus.forward;
|
||||
final Key topKey;
|
||||
Widget topChild;
|
||||
final Animation<double> topAnimation;
|
||||
|
Loading…
x
Reference in New Issue
Block a user