Skip to content

Commit

Permalink
migrate rendering to nullsafety (flutter#64621)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hixie authored and mingwandroid committed Sep 6, 2020
1 parent 4ab6b4b commit 1a21f73
Show file tree
Hide file tree
Showing 67 changed files with 2,987 additions and 3,053 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Expand Up @@ -202,6 +202,7 @@ linter:
- use_full_hex_values_for_flutter_colors
# - use_function_type_syntax_for_parameters # not yet tested
# - use_key_in_widget_constructors # not yet tested
- use_late_for_private_fields_and_variables
- use_rethrow_when_possible
# - use_setters_to_change_properties # not yet tested
# - use_string_buffers # has false positives: https://github.com/dart-lang/sdk/issues/34182
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/analysis_options.yaml
Expand Up @@ -8,5 +8,6 @@ analyzer:
errors:
always_require_non_null_named_parameters: false # not needed with nnbd
type_init_formals: false # https://github.com/dart-lang/linter/issues/2192
unrelated_type_equality_checks: false # https://github.com/dart-lang/linter/issues/2196
void_checks: false # https://github.com/dart-lang/linter/issues/2185
unnecessary_null_comparison: false # https://github.com/dart-lang/language/issues/1018 , turned off until https://github.com/flutter/flutter/issues/61042
2 changes: 0 additions & 2 deletions packages/flutter/lib/rendering.dart
Expand Up @@ -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 rendering tree.
///
/// To use, import `package:flutter/rendering.dart`.
Expand Down
31 changes: 31 additions & 0 deletions packages/flutter/lib/src/animation/tween.dart
Expand Up @@ -208,6 +208,20 @@ class _ChainedEvaluation<T> extends Animatable<T> {
/// [Tween]s that use dedicated `lerp` methods instead of merely relying on the
/// operators (in particular, this allows them to handle null values in a more
/// useful manner).
///
/// ## Nullability
///
/// The [begin] and [end] fields are nullable; a [Tween] does not have to
/// have non-null values specified when it is created.
///
/// If `T` is nullable, then [lerp] and [transform] may return null.
/// This is typically seen in the case where [begin] is null and `t`
/// is 0.0, or [end] is null and `t` is 1.0, or both are null (at any
/// `t` value).
///
/// If `T` is not nullable, then [begin] and [end] must both be set to
/// non-null values before using [lerp] or [transform], otherwise they
/// will throw.
class Tween<T extends dynamic> extends Animatable<T> {
/// Creates a tween.
///
Expand Down Expand Up @@ -236,6 +250,9 @@ class Tween<T extends dynamic> extends Animatable<T> {
/// The default implementation of this method uses the [+], [-], and [*]
/// operators on `T`. The [begin] and [end] properties must therefore be
/// non-null by the time this method is called.
///
/// In general, however, it is possible for this to return null, especially
/// when `t`=0.0 and [begin] is null, or `t`=1.0 and [end] is null.
@protected
T lerp(double t) {
assert(begin != null);
Expand Down Expand Up @@ -291,6 +308,9 @@ class ReverseTween<T> extends Tween<T> {
/// This class specializes the interpolation of [Tween<Color>] to use
/// [Color.lerp].
///
/// The values can be null, representing no color (which is distinct to
/// transparent black, as represented by [Colors.transparent]).
///
/// See [Tween] for a discussion on how to use interpolation objects.
class ColorTween extends Tween<Color?> {
/// Creates a [Color] tween.
Expand All @@ -314,6 +334,8 @@ class ColorTween extends Tween<Color?> {
/// This class specializes the interpolation of [Tween<Size>] to use
/// [Size.lerp].
///
/// The values can be null, representing [Size.zero].
///
/// See [Tween] for a discussion on how to use interpolation objects.
class SizeTween extends Tween<Size?> {
/// Creates a [Size] tween.
Expand All @@ -332,6 +354,9 @@ class SizeTween extends Tween<Size?> {
/// This class specializes the interpolation of [Tween<Rect>] to use
/// [Rect.lerp].
///
/// The values can be null, representing a zero-sized rectangle at the
/// origin ([Rect.zero]).
///
/// See [Tween] for a discussion on how to use interpolation objects.
class RectTween extends Tween<Rect?> {
/// Creates a [Rect] tween.
Expand All @@ -355,6 +380,9 @@ class RectTween extends Tween<Rect?> {
/// This is the closest approximation to a linear tween that is possible with an
/// integer. Compare to [StepTween] and [Tween<double>].
///
/// The [begin] and [end] values must be set to non-null values before
/// calling [lerp] or [transform].
///
/// See [Tween] for a discussion on how to use interpolation objects.
class IntTween extends Tween<int> {
/// Creates an int tween.
Expand All @@ -380,6 +408,9 @@ class IntTween extends Tween<int> {
/// This results in a value that is never greater than the equivalent
/// value from a linear double interpolation. Compare to [IntTween].
///
/// The [begin] and [end] values must be set to non-null values before
/// calling [lerp] or [transform].
///
/// See [Tween] for a discussion on how to use interpolation objects.
class StepTween extends Tween<int> {
/// Creates an [int] tween that floors.
Expand Down
14 changes: 4 additions & 10 deletions packages/flutter/lib/src/foundation/diagnostics.dart
Expand Up @@ -3377,13 +3377,10 @@ abstract class DiagnosticableTree with Diagnosticable {
///
/// * [toString], for a brief description of the object.
/// * [toStringDeep], for a description of the subtree rooted at this object.
String? toStringShallow({
String toStringShallow({
String joiner = ', ',
DiagnosticLevel minLevel = DiagnosticLevel.debug,
}) {
if (kReleaseMode) {
return toString();
}
String? shallowString;
assert(() {
final StringBuffer result = StringBuffer();
Expand All @@ -3398,7 +3395,7 @@ abstract class DiagnosticableTree with Diagnosticable {
shallowString = result.toString();
return true;
}());
return shallowString;
return shallowString ?? toString();
}

/// Returns a string representation of this node and its descendants.
Expand Down Expand Up @@ -3470,13 +3467,10 @@ mixin DiagnosticableTreeMixin implements DiagnosticableTree {
}

@override
String? toStringShallow({
String toStringShallow({
String joiner = ', ',
DiagnosticLevel minLevel = DiagnosticLevel.debug,
}) {
if (kReleaseMode) {
return toString();
}
String? shallowString;
assert(() {
final StringBuffer result = StringBuffer();
Expand All @@ -3491,7 +3485,7 @@ mixin DiagnosticableTreeMixin implements DiagnosticableTree {
shallowString = result.toString();
return true;
}());
return shallowString;
return shallowString ?? toString();
}

@override
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/lib/src/gestures/binding.dart
Expand Up @@ -291,6 +291,7 @@ mixin GestureBinding on BindingBase implements HitTestable, HitTestDispatcher, H
event is PointerHoverEvent ||
event is PointerAddedEvent ||
event is PointerRemovedEvent) {
assert(event.position != null);
dispatchEvent(event, hitTestResult);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/lib/src/gestures/converter.dart
Expand Up @@ -48,6 +48,7 @@ class PointerEventConverter {
static Iterable<PointerEvent> expand(Iterable<ui.PointerData> data, double devicePixelRatio) sync* {
for (final ui.PointerData datum in data) {
final Offset position = Offset(datum.physicalX, datum.physicalY) / devicePixelRatio;
assert(position != null);
final Offset delta = Offset(datum.physicalDeltaX, datum.physicalDeltaY) / devicePixelRatio;
final double radiusMinor = _toLogicalPixels(datum.radiusMinor, devicePixelRatio);
final double radiusMajor = _toLogicalPixels(datum.radiusMajor, devicePixelRatio);
Expand Down
3 changes: 3 additions & 0 deletions packages/flutter/lib/src/painting/text_painter.dart
Expand Up @@ -44,6 +44,9 @@ class PlaceholderDimensions {
}) : assert(size != null),
assert(alignment != null);

/// A constant representing an empty placeholder.
static const PlaceholderDimensions empty = PlaceholderDimensions(size: Size.zero, alignment: ui.PlaceholderAlignment.bottom);

/// Width and height dimensions of the placeholder.
final Size size;

Expand Down
52 changes: 25 additions & 27 deletions packages/flutter/lib/src/rendering/animated_size.dart
Expand Up @@ -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/animation.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
Expand Down Expand Up @@ -76,13 +74,13 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
/// The arguments [duration], [curve], [alignment], and [vsync] must
/// not be null.
RenderAnimatedSize({
@required TickerProvider vsync,
@required Duration duration,
Duration reverseDuration,
required TickerProvider vsync,
required Duration duration,
Duration? reverseDuration,
Curve curve = Curves.linear,
AlignmentGeometry alignment = Alignment.center,
TextDirection textDirection,
RenderBox child,
TextDirection? textDirection,
RenderBox? child,
}) : assert(vsync != null),
assert(duration != null),
assert(curve != null),
Expand All @@ -102,11 +100,11 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
);
}

AnimationController _controller;
CurvedAnimation _animation;
late final AnimationController _controller;
late final CurvedAnimation _animation;
final SizeTween _sizeTween = SizeTween();
bool _hasVisualOverflow;
double _lastValue;
late bool _hasVisualOverflow;
double? _lastValue;

/// The state this size animation is in.
///
Expand All @@ -116,7 +114,7 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
RenderAnimatedSizeState _state = RenderAnimatedSizeState.start;

/// The duration of the animation.
Duration get duration => _controller.duration;
Duration get duration => _controller.duration!;
set duration(Duration value) {
assert(value != null);
if (value == _controller.duration)
Expand All @@ -125,8 +123,8 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
}

/// The duration of the animation when running in reverse.
Duration get reverseDuration => _controller.reverseDuration;
set reverseDuration(Duration value) {
Duration? get reverseDuration => _controller.reverseDuration;
set reverseDuration(Duration? value) {
if (value == _controller.reverseDuration)
return;
_controller.reverseDuration = value;
Expand Down Expand Up @@ -164,7 +162,7 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
super.detach();
}

Size get _animatedSize {
Size? get _animatedSize {
return _sizeTween.evaluate(_animation);
}

Expand All @@ -181,7 +179,7 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
return;
}

child.layout(constraints, parentUsesSize: true);
child!.layout(constraints, parentUsesSize: true);

assert(_state != null);
switch (_state) {
Expand All @@ -199,11 +197,11 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
break;
}

size = constraints.constrain(_animatedSize);
size = constraints.constrain(_animatedSize!);
alignChild();

if (size.width < _sizeTween.end.width ||
size.height < _sizeTween.end.height)
if (size.width < _sizeTween.end!.width ||
size.height < _sizeTween.end!.height)
_hasVisualOverflow = true;
}

Expand All @@ -217,7 +215,7 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
/// We have the initial size to animate from, but we do not have the target
/// size to animate to, so we set both ends to child's size.
void _layoutStart() {
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child.size);
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size);
_state = RenderAnimatedSizeState.stable;
}

Expand All @@ -227,14 +225,14 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
/// If during animation the size of the child changes we restart the
/// animation.
void _layoutStable() {
if (_sizeTween.end != child.size) {
if (_sizeTween.end != child!.size) {
_sizeTween.begin = size;
_sizeTween.end = debugAdoptSize(child.size);
_sizeTween.end = debugAdoptSize(child!.size);
_restartAnimation();
_state = RenderAnimatedSizeState.changed;
} else if (_controller.value == _controller.upperBound) {
// Animation finished. Reset target sizes.
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child.size);
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size);
} else if (!_controller.isAnimating) {
_controller.forward(); // resume the animation after being detached
}
Expand All @@ -247,9 +245,9 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
/// changes again, we match the child's size, restart animation and go to
/// unstable state.
void _layoutChanged() {
if (_sizeTween.end != child.size) {
if (_sizeTween.end != child!.size) {
// Child size changed again. Match the child's size and restart animation.
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child.size);
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size);
_restartAnimation();
_state = RenderAnimatedSizeState.unstable;
} else {
Expand All @@ -264,9 +262,9 @@ class RenderAnimatedSize extends RenderAligningShiftedBox {
///
/// Continue tracking the child's size until is stabilizes.
void _layoutUnstable() {
if (_sizeTween.end != child.size) {
if (_sizeTween.end != child!.size) {
// Still unstable. Continue tracking the child.
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child.size);
_sizeTween.begin = _sizeTween.end = debugAdoptSize(child!.size);
_restartAnimation();
} else {
// Child size stabilized.
Expand Down

0 comments on commit 1a21f73

Please sign in to comment.