Unverified Commit 2ca823d4 authored by Tomek Polański's avatar Tomek Polański Committed by GitHub

Making AnimatedCrossFade more null safe (#91187)

parent bfa4bdbf
...@@ -248,7 +248,7 @@ class AnimatedCrossFade extends StatefulWidget { ...@@ -248,7 +248,7 @@ class AnimatedCrossFade extends StatefulWidget {
} }
class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProviderStateMixin { class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProviderStateMixin {
AnimationController? _controller; late AnimationController _controller;
late Animation<double> _firstAnimation; late Animation<double> _firstAnimation;
late Animation<double> _secondAnimation; late Animation<double> _secondAnimation;
...@@ -261,10 +261,10 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid ...@@ -261,10 +261,10 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
vsync: this, vsync: this,
); );
if (widget.crossFadeState == CrossFadeState.showSecond) if (widget.crossFadeState == CrossFadeState.showSecond)
_controller!.value = 1.0; _controller.value = 1.0;
_firstAnimation = _initAnimation(widget.firstCurve, true); _firstAnimation = _initAnimation(widget.firstCurve, true);
_secondAnimation = _initAnimation(widget.secondCurve, false); _secondAnimation = _initAnimation(widget.secondCurve, false);
_controller!.addStatusListener((AnimationStatus status) { _controller.addStatusListener((AnimationStatus status) {
setState(() { setState(() {
// Trigger a rebuild because it depends on _isTransitioning, which // Trigger a rebuild because it depends on _isTransitioning, which
// changes its value together with animation status. // changes its value together with animation status.
...@@ -273,7 +273,7 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid ...@@ -273,7 +273,7 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
} }
Animation<double> _initAnimation(Curve curve, bool inverted) { 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) if (inverted)
result = result.drive(Tween<double>(begin: 1.0, end: 0.0)); result = result.drive(Tween<double>(begin: 1.0, end: 0.0));
return result; return result;
...@@ -281,7 +281,7 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid ...@@ -281,7 +281,7 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
@override @override
void dispose() { void dispose() {
_controller!.dispose(); _controller.dispose();
super.dispose(); super.dispose();
} }
...@@ -289,9 +289,9 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid ...@@ -289,9 +289,9 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
void didUpdateWidget(AnimatedCrossFade oldWidget) { void didUpdateWidget(AnimatedCrossFade oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (widget.duration != oldWidget.duration) if (widget.duration != oldWidget.duration)
_controller!.duration = widget.duration; _controller.duration = widget.duration;
if (widget.reverseDuration != oldWidget.reverseDuration) if (widget.reverseDuration != oldWidget.reverseDuration)
_controller!.reverseDuration = widget.reverseDuration; _controller.reverseDuration = widget.reverseDuration;
if (widget.firstCurve != oldWidget.firstCurve) if (widget.firstCurve != oldWidget.firstCurve)
_firstAnimation = _initAnimation(widget.firstCurve, true); _firstAnimation = _initAnimation(widget.firstCurve, true);
if (widget.secondCurve != oldWidget.secondCurve) if (widget.secondCurve != oldWidget.secondCurve)
...@@ -299,24 +299,24 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid ...@@ -299,24 +299,24 @@ class _AnimatedCrossFadeState extends State<AnimatedCrossFade> with TickerProvid
if (widget.crossFadeState != oldWidget.crossFadeState) { if (widget.crossFadeState != oldWidget.crossFadeState) {
switch (widget.crossFadeState) { switch (widget.crossFadeState) {
case CrossFadeState.showFirst: case CrossFadeState.showFirst:
_controller!.reverse(); _controller.reverse();
break; break;
case CrossFadeState.showSecond: case CrossFadeState.showSecond:
_controller!.forward(); _controller.forward();
break; break;
} }
} }
} }
/// Whether we're in the middle of cross-fading this frame. /// 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
const Key kFirstChildKey = ValueKey<CrossFadeState>(CrossFadeState.showFirst); const Key kFirstChildKey = ValueKey<CrossFadeState>(CrossFadeState.showFirst);
const Key kSecondChildKey = ValueKey<CrossFadeState>(CrossFadeState.showSecond); const Key kSecondChildKey = ValueKey<CrossFadeState>(CrossFadeState.showSecond);
final bool transitioningForwards = _controller!.status == AnimationStatus.completed || final bool transitioningForwards = _controller.status == AnimationStatus.completed ||
_controller!.status == AnimationStatus.forward; _controller.status == AnimationStatus.forward;
final Key topKey; final Key topKey;
Widget topChild; Widget topChild;
final Animation<double> topAnimation; final Animation<double> topAnimation;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment