Unverified Commit a7e868dd authored by Alexandre Ardhuin's avatar Alexandre Ardhuin Committed by GitHub

migrate animation to nullsafety (#62485)

* migrate animation to nullsafety

* nullable tween

* fix generic type issue

* address review comment
parent d6a25ae6
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
/// The Flutter animation system. /// The Flutter animation system.
/// ///
/// To use, import `package:flutter/animation.dart`. /// To use, import `package:flutter/animation.dart`.
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:ui'; import 'dart:ui';
...@@ -526,7 +525,7 @@ abstract class Curve2D extends ParametricCurve<Offset> { ...@@ -526,7 +525,7 @@ abstract class Curve2D extends ParametricCurve<Offset> {
assert(x != null); assert(x != null);
double start = 0.0; double start = 0.0;
double end = 1.0; double end = 1.0;
double mid; late double mid;
double offsetToOrigin(double pos) => x - transform(pos).dx; double offsetToOrigin(double pos) => x - transform(pos).dx;
// Use a binary search to find the inverse point within 1e-6, or 100 // Use a binary search to find the inverse point within 1e-6, or 100
// subdivisions, whichever comes first. // subdivisions, whichever comes first.
...@@ -626,8 +625,8 @@ class CatmullRomSpline extends Curve2D { ...@@ -626,8 +625,8 @@ class CatmullRomSpline extends Curve2D {
CatmullRomSpline( CatmullRomSpline(
List<Offset> controlPoints, { List<Offset> controlPoints, {
double tension = 0.0, double tension = 0.0,
Offset startHandle, Offset? startHandle,
Offset endHandle, Offset? endHandle,
}) : assert(controlPoints != null), }) : assert(controlPoints != null),
assert(tension != null), assert(tension != null),
assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'), assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'),
...@@ -646,8 +645,8 @@ class CatmullRomSpline extends Curve2D { ...@@ -646,8 +645,8 @@ class CatmullRomSpline extends Curve2D {
CatmullRomSpline.precompute( CatmullRomSpline.precompute(
List<Offset> controlPoints, { List<Offset> controlPoints, {
double tension = 0.0, double tension = 0.0,
Offset startHandle, Offset? startHandle,
Offset endHandle, Offset? endHandle,
}) : assert(controlPoints != null), }) : assert(controlPoints != null),
assert(tension != null), assert(tension != null),
assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'), assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'),
...@@ -663,8 +662,8 @@ class CatmullRomSpline extends Curve2D { ...@@ -663,8 +662,8 @@ class CatmullRomSpline extends Curve2D {
static List<List<Offset>> _computeSegments( static List<List<Offset>> _computeSegments(
List<Offset> controlPoints, List<Offset> controlPoints,
double tension, { double tension, {
Offset startHandle, Offset? startHandle,
Offset endHandle, Offset? endHandle,
}) { }) {
// If not specified, select the first and last control points (which are // If not specified, select the first and last control points (which are
// handles: they are not intersected by the resulting curve) so that they // handles: they are not intersected by the resulting curve) so that they
...@@ -713,17 +712,17 @@ class CatmullRomSpline extends Curve2D { ...@@ -713,17 +712,17 @@ class CatmullRomSpline extends Curve2D {
final List<List<Offset>> _cubicSegments; final List<List<Offset>> _cubicSegments;
// This is non-empty only if the _cubicSegments are being computed lazily. // This is non-empty only if the _cubicSegments are being computed lazily.
final List<Offset> _controlPoints; final List<Offset>? _controlPoints;
final Offset _startHandle; final Offset? _startHandle;
final Offset _endHandle; final Offset? _endHandle;
final double _tension; final double? _tension;
void _initializeIfNeeded() { void _initializeIfNeeded() {
if (_cubicSegments.isNotEmpty) { if (_cubicSegments.isNotEmpty) {
return; return;
} }
_cubicSegments.addAll( _cubicSegments.addAll(
_computeSegments(_controlPoints, _tension, startHandle: _startHandle, endHandle: _endHandle), _computeSegments(_controlPoints!, _tension!, startHandle: _startHandle, endHandle: _endHandle),
); );
} }
...@@ -905,9 +904,9 @@ class CatmullRomCurve extends Curve { ...@@ -905,9 +904,9 @@ class CatmullRomCurve extends Curve {
/// In release mode, this function can be used to decide if a proposed /// In release mode, this function can be used to decide if a proposed
/// modification to the curve will result in a valid curve. /// modification to the curve will result in a valid curve.
static bool validateControlPoints( static bool validateControlPoints(
List<Offset> controlPoints, { List<Offset>? controlPoints, {
double tension = 0.0, double tension = 0.0,
List<String> reasons, List<String>? reasons,
}) { }) {
assert(tension != null); assert(tension != null);
if (controlPoints == null) { if (controlPoints == null) {
...@@ -937,7 +936,7 @@ class CatmullRomCurve extends Curve { ...@@ -937,7 +936,7 @@ class CatmullRomCurve extends Curve {
(controlPoints[i].dx <= 0.0 || controlPoints[i].dx >= 1.0)) { (controlPoints[i].dx <= 0.0 || controlPoints[i].dx >= 1.0)) {
assert(() { assert(() {
reasons?.add('Control points must have X values between 0.0 and 1.0, exclusive. ' reasons?.add('Control points must have X values between 0.0 and 1.0, exclusive. '
'Point $i has an x value (${controlPoints[i].dx}) which is outside the range.'); 'Point $i has an x value (${controlPoints![i].dx}) which is outside the range.');
return true; return true;
}()); }());
return false; return false;
...@@ -946,7 +945,7 @@ class CatmullRomCurve extends Curve { ...@@ -946,7 +945,7 @@ class CatmullRomCurve extends Curve {
assert(() { assert(() {
reasons?.add('Each X coordinate must be greater than the preceding X coordinate ' reasons?.add('Each X coordinate must be greater than the preceding X coordinate '
'(i.e. must be monotonically increasing in X). Point $i has an x value of ' '(i.e. must be monotonically increasing in X). Point $i has an x value of '
'${controlPoints[i].dx}, which is not greater than $lastX'); '${controlPoints![i].dx}, which is not greater than $lastX');
return true; return true;
}()); }());
return false; return false;
...@@ -1053,7 +1052,7 @@ class CatmullRomCurve extends Curve { ...@@ -1053,7 +1052,7 @@ class CatmullRomCurve extends Curve {
// Now interpolate between the found sample and the next one. // Now interpolate between the found sample and the next one.
final double t2 = (t - startValue.dx) / (endValue.dx - startValue.dx); final double t2 = (t - startValue.dx) / (endValue.dx - startValue.dx);
return lerpDouble(startValue.dy, endValue.dy, t2); return lerpDouble(startValue.dy, endValue.dy, t2)!;
} }
} }
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
...@@ -121,7 +120,7 @@ mixin AnimationLocalListenersMixin { ...@@ -121,7 +120,7 @@ mixin AnimationLocalListenersMixin {
void notifyListeners() { void notifyListeners() {
final List<VoidCallback> localListeners = List<VoidCallback>.from(_listeners); final List<VoidCallback> localListeners = List<VoidCallback>.from(_listeners);
for (final VoidCallback listener in localListeners) { for (final VoidCallback listener in localListeners) {
InformationCollector collector; InformationCollector? collector;
assert(() { assert(() {
collector = () sync* { collector = () sync* {
yield DiagnosticsProperty<AnimationLocalListenersMixin>( yield DiagnosticsProperty<AnimationLocalListenersMixin>(
...@@ -199,7 +198,7 @@ mixin AnimationLocalStatusListenersMixin { ...@@ -199,7 +198,7 @@ mixin AnimationLocalStatusListenersMixin {
if (_statusListeners.contains(listener)) if (_statusListeners.contains(listener))
listener(status); listener(status);
} catch (exception, stack) { } catch (exception, stack) {
InformationCollector collector; InformationCollector? collector;
assert(() { assert(() {
collector = () sync* { collector = () sync* {
yield DiagnosticsProperty<AnimationLocalStatusListenersMixin>( yield DiagnosticsProperty<AnimationLocalStatusListenersMixin>(
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:ui' show Color, Size, Rect; import 'dart:ui' show Color, Size, Rect;
...@@ -215,19 +214,22 @@ class Tween<T extends dynamic> extends Animatable<T> { ...@@ -215,19 +214,22 @@ class Tween<T extends dynamic> extends Animatable<T> {
/// The [begin] and [end] properties must be non-null before the tween is /// The [begin] and [end] properties must be non-null before the tween is
/// first used, but the arguments can be null if the values are going to be /// first used, but the arguments can be null if the values are going to be
/// filled in later. /// filled in later.
Tween({ this.begin, this.end }); Tween({
this.begin,
this.end,
});
/// The value this variable has at the beginning of the animation. /// The value this variable has at the beginning of the animation.
/// ///
/// See the constructor for details about whether this property may be null /// See the constructor for details about whether this property may be null
/// (it varies from subclass to subclass). /// (it varies from subclass to subclass).
T begin; T? begin;
/// The value this variable has at the end of the animation. /// The value this variable has at the end of the animation.
/// ///
/// See the constructor for details about whether this property may be null /// See the constructor for details about whether this property may be null
/// (it varies from subclass to subclass). /// (it varies from subclass to subclass).
T end; T? end;
/// Returns the value this variable has at the given animation clock value. /// Returns the value this variable has at the given animation clock value.
/// ///
...@@ -256,9 +258,9 @@ class Tween<T extends dynamic> extends Animatable<T> { ...@@ -256,9 +258,9 @@ class Tween<T extends dynamic> extends Animatable<T> {
@override @override
T transform(double t) { T transform(double t) {
if (t == 0.0) if (t == 0.0)
return begin; return begin as T;
if (t == 1.0) if (t == 1.0)
return end; return end as T;
return lerp(t); return lerp(t);
} }
...@@ -290,7 +292,7 @@ class ReverseTween<T> extends Tween<T> { ...@@ -290,7 +292,7 @@ class ReverseTween<T> extends Tween<T> {
/// [Color.lerp]. /// [Color.lerp].
/// ///
/// See [Tween] for a discussion on how to use interpolation objects. /// See [Tween] for a discussion on how to use interpolation objects.
class ColorTween extends Tween<Color> { class ColorTween extends Tween<Color?> {
/// Creates a [Color] tween. /// Creates a [Color] tween.
/// ///
/// The [begin] and [end] properties may be null; the null value /// The [begin] and [end] properties may be null; the null value
...@@ -300,11 +302,11 @@ class ColorTween extends Tween<Color> { ...@@ -300,11 +302,11 @@ class ColorTween extends Tween<Color> {
/// or [end] if you want the effect of fading in or out of transparent. /// or [end] if you want the effect of fading in or out of transparent.
/// Instead prefer null. [Colors.transparent] refers to black transparent and /// Instead prefer null. [Colors.transparent] refers to black transparent and
/// thus will fade out of or into black which is likely unwanted. /// thus will fade out of or into black which is likely unwanted.
ColorTween({ Color begin, Color end }) : super(begin: begin, end: end); ColorTween({ Color? begin, Color? end }) : super(begin: begin, end: end);
/// Returns the value this variable has at the given animation clock value. /// Returns the value this variable has at the given animation clock value.
@override @override
Color lerp(double t) => Color.lerp(begin, end, t); Color? lerp(double t) => Color.lerp(begin, end, t);
} }
/// An interpolation between two sizes. /// An interpolation between two sizes.
...@@ -313,16 +315,16 @@ class ColorTween extends Tween<Color> { ...@@ -313,16 +315,16 @@ class ColorTween extends Tween<Color> {
/// [Size.lerp]. /// [Size.lerp].
/// ///
/// See [Tween] for a discussion on how to use interpolation objects. /// See [Tween] for a discussion on how to use interpolation objects.
class SizeTween extends Tween<Size> { class SizeTween extends Tween<Size?> {
/// Creates a [Size] tween. /// Creates a [Size] tween.
/// ///
/// The [begin] and [end] properties may be null; the null value /// The [begin] and [end] properties may be null; the null value
/// is treated as an empty size. /// is treated as an empty size.
SizeTween({ Size begin, Size end }) : super(begin: begin, end: end); SizeTween({ Size? begin, Size? end }) : super(begin: begin, end: end);
/// Returns the value this variable has at the given animation clock value. /// Returns the value this variable has at the given animation clock value.
@override @override
Size lerp(double t) => Size.lerp(begin, end, t); Size? lerp(double t) => Size.lerp(begin, end, t);
} }
/// An interpolation between two rectangles. /// An interpolation between two rectangles.
...@@ -331,16 +333,16 @@ class SizeTween extends Tween<Size> { ...@@ -331,16 +333,16 @@ class SizeTween extends Tween<Size> {
/// [Rect.lerp]. /// [Rect.lerp].
/// ///
/// See [Tween] for a discussion on how to use interpolation objects. /// See [Tween] for a discussion on how to use interpolation objects.
class RectTween extends Tween<Rect> { class RectTween extends Tween<Rect?> {
/// Creates a [Rect] tween. /// Creates a [Rect] tween.
/// ///
/// The [begin] and [end] properties may be null; the null value /// The [begin] and [end] properties may be null; the null value
/// is treated as an empty rect at the top left corner. /// is treated as an empty rect at the top left corner.
RectTween({ Rect begin, Rect end }) : super(begin: begin, end: end); RectTween({ Rect? begin, Rect? end }) : super(begin: begin, end: end);
/// Returns the value this variable has at the given animation clock value. /// Returns the value this variable has at the given animation clock value.
@override @override
Rect lerp(double t) => Rect.lerp(begin, end, t); Rect? lerp(double t) => Rect.lerp(begin, end, t);
} }
/// An interpolation between two integers that rounds. /// An interpolation between two integers that rounds.
...@@ -360,12 +362,12 @@ class IntTween extends Tween<int> { ...@@ -360,12 +362,12 @@ class IntTween extends Tween<int> {
/// The [begin] and [end] properties must be non-null before the tween is /// The [begin] and [end] properties must be non-null before the tween is
/// first used, but the arguments can be null if the values are going to be /// first used, but the arguments can be null if the values are going to be
/// filled in later. /// filled in later.
IntTween({ int begin, int end }) : super(begin: begin, end: end); IntTween({ int? begin, int? end }) : super(begin: begin, end: end);
// The inherited lerp() function doesn't work with ints because it multiplies // The inherited lerp() function doesn't work with ints because it multiplies
// the begin and end types by a double, and int * double returns a double. // the begin and end types by a double, and int * double returns a double.
@override @override
int lerp(double t) => (begin + (end - begin) * t).round(); int lerp(double t) => (begin! + (end! - begin!) * t).round();
} }
/// An interpolation between two integers that floors. /// An interpolation between two integers that floors.
...@@ -385,12 +387,12 @@ class StepTween extends Tween<int> { ...@@ -385,12 +387,12 @@ class StepTween extends Tween<int> {
/// The [begin] and [end] properties must be non-null before the tween is /// The [begin] and [end] properties must be non-null before the tween is
/// first used, but the arguments can be null if the values are going to be /// first used, but the arguments can be null if the values are going to be
/// filled in later. /// filled in later.
StepTween({ int begin, int end }) : super(begin: begin, end: end); StepTween({ int? begin, int? end }) : super(begin: begin, end: end);
// The inherited lerp() function doesn't work with ints because it multiplies // The inherited lerp() function doesn't work with ints because it multiplies
// the begin and end types by a double, and int * double returns a double. // the begin and end types by a double, and int * double returns a double.
@override @override
int lerp(double t) => (begin + (end - begin) * t).floor(); int lerp(double t) => (begin! + (end! - begin!) * t).floor();
} }
/// A tween with a constant value. /// A tween with a constant value.
...@@ -400,7 +402,7 @@ class ConstantTween<T> extends Tween<T> { ...@@ -400,7 +402,7 @@ class ConstantTween<T> extends Tween<T> {
/// This tween doesn't interpolate, it always returns the same value. /// This tween doesn't interpolate, it always returns the same value.
@override @override
T lerp(double t) => begin; T lerp(double t) => begin as T;
@override @override
String toString() => '${objectRuntimeType(this, 'ReverseTween')}(value: $begin)'; String toString() => '${objectRuntimeType(this, 'ReverseTween')}(value: $begin)';
...@@ -436,7 +438,7 @@ class CurveTween extends Animatable<double> { ...@@ -436,7 +438,7 @@ class CurveTween extends Animatable<double> {
/// Creates a curve tween. /// Creates a curve tween.
/// ///
/// The [curve] argument must not be null. /// The [curve] argument must not be null.
CurveTween({ @required this.curve }) CurveTween({ required this.curve })
: assert(curve != null); : assert(curve != null);
/// The curve to use when transforming the value of the animation. /// The curve to use when transforming the value of the animation.
......
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:flutter/foundation.dart';
import 'animation.dart'; import 'animation.dart';
import 'tween.dart'; import 'tween.dart';
...@@ -90,8 +87,7 @@ class TweenSequence<T> extends Animatable<T> { ...@@ -90,8 +87,7 @@ class TweenSequence<T> extends Animatable<T> {
return _evaluateAt(t, index); return _evaluateAt(t, index);
} }
// Should be unreachable. // Should be unreachable.
assert(false, 'TweenSequence.evaluate() could not find an interval for $t'); throw StateError('TweenSequence.evaluate() could not find an interval for $t');
return null;
} }
@override @override
...@@ -128,8 +124,8 @@ class TweenSequenceItem<T> { ...@@ -128,8 +124,8 @@ class TweenSequenceItem<T> {
/// ///
/// The [tween] must not be null and [weight] must be greater than 0.0. /// The [tween] must not be null and [weight] must be greater than 0.0.
const TweenSequenceItem({ const TweenSequenceItem({
@required this.tween, required this.tween,
@required this.weight, required this.weight,
}) : assert(tween != null), }) : assert(tween != null),
assert(weight != null), assert(weight != null),
assert(weight > 0.0); assert(weight > 0.0);
......
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