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