From 00dcd5f49ba76b0e6502f0949e2c80d5f9f97738 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 29 Jul 2020 00:11:05 +0200 Subject: [PATCH] migrate gestures to nullsafety (#62157) --- dev/tools/dartdoc.dart | 1 + packages/flutter/lib/gestures.dart | 2 - packages/flutter/lib/src/gestures/arena.dart | 19 +- .../flutter/lib/src/gestures/binding.dart | 21 +- .../flutter/lib/src/gestures/constants.dart | 1 - .../flutter/lib/src/gestures/converter.dart | 6 +- packages/flutter/lib/src/gestures/debug.dart | 1 - packages/flutter/lib/src/gestures/drag.dart | 1 - .../lib/src/gestures/drag_details.dart | 17 +- packages/flutter/lib/src/gestures/eager.dart | 3 +- packages/flutter/lib/src/gestures/events.dart | 227 +++++++++--------- .../flutter/lib/src/gestures/force_press.dart | 33 ++- .../flutter/lib/src/gestures/hit_test.dart | 14 +- .../flutter/lib/src/gestures/long_press.dart | 85 ++++--- .../flutter/lib/src/gestures/lsq_solver.dart | 7 +- .../flutter/lib/src/gestures/monodrag.dart | 81 +++---- .../flutter/lib/src/gestures/multidrag.dart | 103 ++++---- .../flutter/lib/src/gestures/multitap.dart | 99 ++++---- .../lib/src/gestures/pointer_router.dart | 31 ++- .../src/gestures/pointer_signal_resolver.dart | 13 +- .../flutter/lib/src/gestures/recognizer.dart | 73 +++--- packages/flutter/lib/src/gestures/scale.dart | 91 ++++--- packages/flutter/lib/src/gestures/tap.dart | 73 +++--- packages/flutter/lib/src/gestures/team.dart | 17 +- .../lib/src/gestures/velocity_tracker.dart | 25 +- 25 files changed, 507 insertions(+), 537 deletions(-) diff --git a/dev/tools/dartdoc.dart b/dev/tools/dartdoc.dart index 7c1099c429d..c13f92503a8 100644 --- a/dev/tools/dartdoc.dart +++ b/dev/tools/dartdoc.dart @@ -126,6 +126,7 @@ Future main(List arguments) async { final List dartdocArgs = [ ...dartdocBaseArgs, '--allow-tools', + '--enable-experiment=non-nullable', if (args['json'] as bool) '--json', if (args['validate-links'] as bool) '--validate-links' else '--no-validate-links', '--link-to-source-excludes', '../../bin/cache', diff --git a/packages/flutter/lib/gestures.dart b/packages/flutter/lib/gestures.dart index dffd139d72e..3d1cdc68edf 100644 --- a/packages/flutter/lib/gestures.dart +++ b/packages/flutter/lib/gestures.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 - /// The Flutter gesture recognizers. /// /// To use, import `package:flutter/gestures.dart`. diff --git a/packages/flutter/lib/src/gestures/arena.dart b/packages/flutter/lib/src/gestures/arena.dart index b2bf9fc3e95..cfed21fb04d 100644 --- a/packages/flutter/lib/src/gestures/arena.dart +++ b/packages/flutter/lib/src/gestures/arena.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:async'; @@ -65,7 +64,7 @@ class _GestureArena { /// "eager winner". We look for an eager winner when closing the arena to new /// participants, and if there is one, we resolve the arena in its favor at /// that time. - GestureArenaMember eagerWinner; + GestureArenaMember? eagerWinner; void add(GestureArenaMember member) { assert(isOpen); @@ -119,7 +118,7 @@ class GestureArenaManager { /// /// Called after the framework has finished dispatching the pointer down event. void close(int pointer) { - final _GestureArena state = _arenas[pointer]; + final _GestureArena? state = _arenas[pointer]; if (state == null) return; // This arena either never existed or has been resolved. state.isOpen = false; @@ -141,7 +140,7 @@ class GestureArenaManager { /// * [hold] /// * [release] void sweep(int pointer) { - final _GestureArena state = _arenas[pointer]; + final _GestureArena? state = _arenas[pointer]; if (state == null) return; // This arena either never existed or has been resolved. assert(!state.isOpen); @@ -175,7 +174,7 @@ class GestureArenaManager { /// * [sweep] /// * [release] void hold(int pointer) { - final _GestureArena state = _arenas[pointer]; + final _GestureArena? state = _arenas[pointer]; if (state == null) return; // This arena either never existed or has been resolved. state.isHeld = true; @@ -192,7 +191,7 @@ class GestureArenaManager { /// * [sweep] /// * [hold] void release(int pointer) { - final _GestureArena state = _arenas[pointer]; + final _GestureArena? state = _arenas[pointer]; if (state == null) return; // This arena either never existed or has been resolved. state.isHeld = false; @@ -205,7 +204,7 @@ class GestureArenaManager { /// /// This is called by calling [GestureArenaEntry.resolve] on the object returned from [add]. void _resolve(int pointer, GestureArenaMember member, GestureDisposition disposition) { - final _GestureArena state = _arenas[pointer]; + final _GestureArena? state = _arenas[pointer]; if (state == null) return; // This arena has already resolved. assert(_debugLogDiagnostic(pointer, '${ disposition == GestureDisposition.accepted ? "Accepting" : "Rejecting" }: $member')); @@ -236,7 +235,7 @@ class GestureArenaManager { assert(_debugLogDiagnostic(pointer, 'Arena empty.')); } else if (state.eagerWinner != null) { assert(_debugLogDiagnostic(pointer, 'Eager winner: ${state.eagerWinner}')); - _resolveInFavorOf(pointer, state, state.eagerWinner); + _resolveInFavorOf(pointer, state, state.eagerWinner!); } } @@ -265,10 +264,10 @@ class GestureArenaManager { member.acceptGesture(pointer); } - bool _debugLogDiagnostic(int pointer, String message, [ _GestureArena state ]) { + bool _debugLogDiagnostic(int pointer, String message, [ _GestureArena? state ]) { assert(() { if (debugPrintGestureArenaDiagnostics) { - final int count = state != null ? state.members.length : null; + final int? count = state != null ? state.members.length : null; final String s = count != 1 ? 's' : ''; debugPrint('Gesture arena ${pointer.toString().padRight(4)} ❙ $message${ count != null ? " with $count member$s." : ""}'); } diff --git a/packages/flutter/lib/src/gestures/binding.dart b/packages/flutter/lib/src/gestures/binding.dart index ae1d512bb08..82dd42072ef 100644 --- a/packages/flutter/lib/src/gestures/binding.dart +++ b/packages/flutter/lib/src/gestures/binding.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:async'; import 'dart:collection'; @@ -75,8 +74,8 @@ mixin GestureBinding on BindingBase implements HitTestable, HitTestDispatcher, H } /// The singleton instance of this object. - static GestureBinding get instance => _instance; - static GestureBinding _instance; + static GestureBinding? get instance => _instance; + static GestureBinding? _instance; final Queue _pendingPointerEvents = Queue(); @@ -123,7 +122,7 @@ mixin GestureBinding on BindingBase implements HitTestable, HitTestDispatcher, H void _handlePointerEvent(PointerEvent event) { assert(!locked); - HitTestResult hitTestResult; + HitTestResult? hitTestResult; if (event is PointerDownEvent || event is PointerSignalEvent) { assert(!_hitTests.containsKey(event.pointer)); hitTestResult = HitTestResult(); @@ -172,7 +171,7 @@ mixin GestureBinding on BindingBase implements HitTestable, HitTestDispatcher, H /// might throw. The [hitTestResult] argument may only be null for /// [PointerHoverEvent], [PointerAddedEvent], or [PointerRemovedEvent] events. @override // from HitTestDispatcher - void dispatchEvent(PointerEvent event, HitTestResult hitTestResult) { + void dispatchEvent(PointerEvent event, HitTestResult? hitTestResult) { assert(!locked); // No hit test information implies that this is a hover or pointer // add/remove event. @@ -243,12 +242,12 @@ class FlutterErrorDetailsForPointerEventDispatcher extends FlutterErrorDetails { /// that will subsequently be reported using [FlutterError.onError]. const FlutterErrorDetailsForPointerEventDispatcher({ dynamic exception, - StackTrace stack, - String library, - DiagnosticsNode context, + StackTrace? stack, + String? library, + DiagnosticsNode? context, this.event, this.hitTestEntry, - InformationCollector informationCollector, + InformationCollector? informationCollector, bool silent = false, }) : super( exception: exception, @@ -260,7 +259,7 @@ class FlutterErrorDetailsForPointerEventDispatcher extends FlutterErrorDetails { ); /// The pointer event that was being routed when the exception was raised. - final PointerEvent event; + final PointerEvent? event; /// The hit test result entry for the object whose handleEvent method threw /// the exception. May be null if no hit test entry is associated with the @@ -268,5 +267,5 @@ class FlutterErrorDetailsForPointerEventDispatcher extends FlutterErrorDetails { /// /// The target object itself is given by the [HitTestEntry.target] property of /// the hitTestEntry object. - final HitTestEntry hitTestEntry; + final HitTestEntry? hitTestEntry; } diff --git a/packages/flutter/lib/src/gestures/constants.dart b/packages/flutter/lib/src/gestures/constants.dart index fb4efd0cbac..cd7d106a6f3 100644 --- a/packages/flutter/lib/src/gestures/constants.dart +++ b/packages/flutter/lib/src/gestures/constants.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 // Modeled after Android's ViewConfiguration: // https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/ViewConfiguration.java diff --git a/packages/flutter/lib/src/gestures/converter.dart b/packages/flutter/lib/src/gestures/converter.dart index 85eec8c0925..63c1d0d2795 100644 --- a/packages/flutter/lib/src/gestures/converter.dart +++ b/packages/flutter/lib/src/gestures/converter.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:ui' as ui show PointerData, PointerChange, PointerSignalKind; @@ -214,7 +213,7 @@ class PointerEventConverter { break; } } else { - switch (datum.signalKind) { + switch (datum.signalKind!) { case ui.PointerSignalKind.scroll: final Offset scrollDelta = Offset(datum.scrollDeltaX, datum.scrollDeltaY) / devicePixelRatio; @@ -238,6 +237,5 @@ class PointerEventConverter { } } - static double _toLogicalPixels(double physicalPixels, double devicePixelRatio) => - physicalPixels == null ? null : physicalPixels / devicePixelRatio; + static double _toLogicalPixels(double physicalPixels, double devicePixelRatio) => physicalPixels / devicePixelRatio; } diff --git a/packages/flutter/lib/src/gestures/debug.dart b/packages/flutter/lib/src/gestures/debug.dart index 14961cdbd08..83eb1310da4 100644 --- a/packages/flutter/lib/src/gestures/debug.dart +++ b/packages/flutter/lib/src/gestures/debug.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 'package:flutter/foundation.dart'; diff --git a/packages/flutter/lib/src/gestures/drag.dart b/packages/flutter/lib/src/gestures/drag.dart index 401be2f8ac6..757b9c14779 100644 --- a/packages/flutter/lib/src/gestures/drag.dart +++ b/packages/flutter/lib/src/gestures/drag.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 'drag_details.dart'; diff --git a/packages/flutter/lib/src/gestures/drag_details.dart b/packages/flutter/lib/src/gestures/drag_details.dart index d35190ff300..ce8282313bc 100644 --- a/packages/flutter/lib/src/gestures/drag_details.dart +++ b/packages/flutter/lib/src/gestures/drag_details.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:ui' show Offset; @@ -24,7 +23,7 @@ class DragDownDetails { /// The [globalPosition] argument must not be null. DragDownDetails({ this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -71,7 +70,7 @@ class DragStartDetails { DragStartDetails({ this.sourceTimeStamp, this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -79,7 +78,7 @@ class DragStartDetails { /// event. /// /// Could be null if triggered from proxied events such as accessibility. - final Duration sourceTimeStamp; + final Duration? sourceTimeStamp; /// The global position at which the pointer contacted the screen. /// @@ -134,8 +133,8 @@ class DragUpdateDetails { this.sourceTimeStamp, this.delta = Offset.zero, this.primaryDelta, - @required this.globalPosition, - Offset localPosition, + required this.globalPosition, + Offset? localPosition, }) : assert(delta != null), assert(primaryDelta == null || (primaryDelta == delta.dx && delta.dy == 0.0) @@ -146,7 +145,7 @@ class DragUpdateDetails { /// event. /// /// Could be null if triggered from proxied events such as accessibility. - final Duration sourceTimeStamp; + final Duration? sourceTimeStamp; /// The amount the pointer has moved in the coordinate space of the event /// receiver since the previous update. @@ -169,7 +168,7 @@ class DragUpdateDetails { /// two-dimensional drag (e.g., a pan), then this value is null. /// /// Defaults to null if not specified in the constructor. - final double primaryDelta; + final double? primaryDelta; /// The pointer's global position when it triggered this update. /// @@ -233,7 +232,7 @@ class DragEndDetails { /// two-dimensional drag (e.g., a pan), then this value is null. /// /// Defaults to null if not specified in the constructor. - final double primaryVelocity; + final double? primaryVelocity; @override String toString() => '${objectRuntimeType(this, 'DragEndDetails')}($velocity)'; diff --git a/packages/flutter/lib/src/gestures/eager.dart b/packages/flutter/lib/src/gestures/eager.dart index 645d8eafab2..c475b025dee 100644 --- a/packages/flutter/lib/src/gestures/eager.dart +++ b/packages/flutter/lib/src/gestures/eager.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 'arena.dart'; import 'events.dart'; @@ -17,7 +16,7 @@ class EagerGestureRecognizer extends OneSequenceGestureRecognizer { /// Create an eager gesture recognizer. /// /// {@macro flutter.gestures.gestureRecognizer.kind} - EagerGestureRecognizer({ PointerDeviceKind kind }) : super(kind: kind); + EagerGestureRecognizer({ PointerDeviceKind? kind }) : super(kind: kind); @override void addAllowedPointer(PointerDownEvent event) { diff --git a/packages/flutter/lib/src/gestures/events.dart b/packages/flutter/lib/src/gestures/events.dart index 22a91a18661..906a02a5072 100644 --- a/packages/flutter/lib/src/gestures/events.dart +++ b/packages/flutter/lib/src/gestures/events.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:ui' show Offset, PointerDeviceKind; @@ -210,9 +209,9 @@ abstract class PointerEvent with Diagnosticable { this.kind = PointerDeviceKind.touch, this.device = 0, this.position = Offset.zero, - Offset localPosition, + Offset? localPosition, this.delta = Offset.zero, - Offset localDelta, + Offset? localDelta, this.buttons = 0, this.down = false, this.obscured = false, @@ -459,7 +458,7 @@ abstract class PointerEvent with Diagnosticable { /// /// * [transformed], which transforms this event into a different coordinate /// space. - final Matrix4 transform; + final Matrix4? transform; /// The original un-transformed [PointerEvent] before any [transform]s were /// applied. @@ -470,7 +469,7 @@ abstract class PointerEvent with Diagnosticable { /// event, they all receive the event transformed to their local coordinate /// space. The [original] property can be used to determine if all those /// transformed events actually originated from the same pointer interaction. - final PointerEvent original; + final PointerEvent? original; /// Transforms the event from the global coordinate space into the coordinate /// space of an event receiver. @@ -484,7 +483,7 @@ abstract class PointerEvent with Diagnosticable { /// Transforms are not commutative. If this method is called on a /// [PointerEvent] that has a non-null [transform] value, that value will be /// overridden by the provided `transform`. - PointerEvent transformed(Matrix4 transform); + PointerEvent transformed(Matrix4? transform); @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { @@ -528,7 +527,7 @@ abstract class PointerEvent with Diagnosticable { /// /// The z-value of `position` is assumed to be 0.0. If `transform` is null, /// `position` is returned as-is. - static Offset transformPosition(Matrix4 transform, Offset position) { + static Offset transformPosition(Matrix4? transform, Offset position) { if (transform == null) { return position; } @@ -545,10 +544,10 @@ abstract class PointerEvent with Diagnosticable { /// /// If `transform` is null, `untransformedDelta` is returned. static Offset transformDeltaViaPositions({ - @required Offset untransformedEndPosition, - Offset transformedEndPosition, - @required Offset untransformedDelta, - @required Matrix4 transform, + required Offset untransformedEndPosition, + Offset? transformedEndPosition, + required Offset untransformedDelta, + required Matrix4? transform, }) { if (transform == null) { return untransformedDelta; @@ -592,7 +591,7 @@ class PointerAddedEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, bool obscured = false, double pressureMin = 1.0, double pressureMax = 1.0, @@ -602,8 +601,8 @@ class PointerAddedEvent extends PointerEvent { double radiusMax = 0.0, double orientation = 0.0, double tilt = 0.0, - Matrix4 transform, - PointerAddedEvent original, + Matrix4? transform, + PointerAddedEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -627,7 +626,7 @@ class PointerAddedEvent extends PointerEvent { ); @override - PointerAddedEvent transformed(Matrix4 transform) { + PointerAddedEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -647,7 +646,7 @@ class PointerAddedEvent extends PointerEvent { orientation: orientation, tilt: tilt, transform: transform, - original: original as PointerAddedEvent ?? this, + original: original as PointerAddedEvent? ?? this, embedderId: embedderId, ); } @@ -666,15 +665,15 @@ class PointerRemovedEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, bool obscured = false, double pressureMin = 1.0, double pressureMax = 1.0, double distanceMax = 0.0, double radiusMin = 0.0, double radiusMax = 0.0, - Matrix4 transform, - PointerRemovedEvent original, + Matrix4? transform, + PointerRemovedEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -695,7 +694,7 @@ class PointerRemovedEvent extends PointerEvent { ); @override - PointerRemovedEvent transformed(Matrix4 transform) { + PointerRemovedEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -712,7 +711,7 @@ class PointerRemovedEvent extends PointerEvent { radiusMin: radiusMin, radiusMax: radiusMax, transform: transform, - original: original as PointerRemovedEvent ?? this, + original: original as PointerRemovedEvent? ?? this, embedderId: embedderId, ); } @@ -739,9 +738,9 @@ class PointerHoverEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, Offset delta = Offset.zero, - Offset localDelta, + Offset? localDelta, int buttons = 0, bool obscured = false, double pressureMin = 1.0, @@ -756,8 +755,8 @@ class PointerHoverEvent extends PointerEvent { double orientation = 0.0, double tilt = 0.0, bool synthesized = false, - Matrix4 transform, - PointerHoverEvent original, + Matrix4? transform, + PointerHoverEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -789,7 +788,7 @@ class PointerHoverEvent extends PointerEvent { ); @override - PointerHoverEvent transformed(Matrix4 transform) { + PointerHoverEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -822,7 +821,7 @@ class PointerHoverEvent extends PointerEvent { tilt: tilt, synthesized: synthesized, transform: transform, - original: original as PointerHoverEvent ?? this, + original: original as PointerHoverEvent? ?? this, embedderId: embedderId, ); } @@ -849,9 +848,9 @@ class PointerEnterEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, Offset delta = Offset.zero, - Offset localDelta, + Offset? localDelta, int buttons = 0, bool obscured = false, double pressureMin = 1.0, @@ -867,8 +866,8 @@ class PointerEnterEvent extends PointerEvent { double tilt = 0.0, bool down = false, bool synthesized = false, - Matrix4 transform, - PointerEnterEvent original, + Matrix4? transform, + PointerEnterEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -912,34 +911,34 @@ class PointerEnterEvent extends PointerEvent { /// /// This is used by the [MouseTracker] to synthesize enter events. PointerEnterEvent.fromMouseEvent(PointerEvent event) : this( - timeStamp: event?.timeStamp, - kind: event?.kind, - device: event?.device, - position: event?.position, - localPosition: event?.localPosition, - delta: event?.delta, - localDelta: event?.localDelta, - buttons: event?.buttons, - obscured: event?.obscured, - pressureMin: event?.pressureMin, - pressureMax: event?.pressureMax, - distance: event?.distance, - distanceMax: event?.distanceMax, - size: event?.size, - radiusMajor: event?.radiusMajor, - radiusMinor: event?.radiusMinor, - radiusMin: event?.radiusMin, - radiusMax: event?.radiusMax, - orientation: event?.orientation, - tilt: event?.tilt, - down: event?.down, - synthesized: event?.synthesized, - transform: event?.transform, + timeStamp: event.timeStamp, + kind: event.kind, + device: event.device, + position: event.position, + localPosition: event.localPosition, + delta: event.delta, + localDelta: event.localDelta, + buttons: event.buttons, + obscured: event.obscured, + pressureMin: event.pressureMin, + pressureMax: event.pressureMax, + distance: event.distance, + distanceMax: event.distanceMax, + size: event.size, + radiusMajor: event.radiusMajor, + radiusMinor: event.radiusMinor, + radiusMin: event.radiusMin, + radiusMax: event.radiusMax, + orientation: event.orientation, + tilt: event.tilt, + down: event.down, + synthesized: event.synthesized, + transform: event.transform, original: null, ); @override - PointerEnterEvent transformed(Matrix4 transform) { + PointerEnterEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -973,7 +972,7 @@ class PointerEnterEvent extends PointerEvent { down: down, synthesized: synthesized, transform: transform, - original: original as PointerEnterEvent ?? this, + original: original as PointerEnterEvent? ?? this, embedderId: embedderId, ); } @@ -1000,9 +999,9 @@ class PointerExitEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, Offset delta = Offset.zero, - Offset localDelta, + Offset? localDelta, int buttons = 0, bool obscured = false, double pressureMin = 1.0, @@ -1018,8 +1017,8 @@ class PointerExitEvent extends PointerEvent { double tilt = 0.0, bool down = false, bool synthesized = false, - Matrix4 transform, - PointerExitEvent original, + Matrix4? transform, + PointerExitEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -1063,34 +1062,34 @@ class PointerExitEvent extends PointerEvent { /// /// This is used by the [MouseTracker] to synthesize exit events. PointerExitEvent.fromMouseEvent(PointerEvent event) : this( - timeStamp: event?.timeStamp, - kind: event?.kind, - device: event?.device, - position: event?.position, - localPosition: event?.localPosition, - delta: event?.delta, - localDelta: event?.localDelta, - buttons: event?.buttons, - obscured: event?.obscured, - pressureMin: event?.pressureMin, - pressureMax: event?.pressureMax, - distance: event?.distance, - distanceMax: event?.distanceMax, - size: event?.size, - radiusMajor: event?.radiusMajor, - radiusMinor: event?.radiusMinor, - radiusMin: event?.radiusMin, - radiusMax: event?.radiusMax, - orientation: event?.orientation, - tilt: event?.tilt, - down: event?.down, - synthesized: event?.synthesized, - transform: event?.transform, + timeStamp: event.timeStamp, + kind: event.kind, + device: event.device, + position: event.position, + localPosition: event.localPosition, + delta: event.delta, + localDelta: event.localDelta, + buttons: event.buttons, + obscured: event.obscured, + pressureMin: event.pressureMin, + pressureMax: event.pressureMax, + distance: event.distance, + distanceMax: event.distanceMax, + size: event.size, + radiusMajor: event.radiusMajor, + radiusMinor: event.radiusMinor, + radiusMin: event.radiusMin, + radiusMax: event.radiusMax, + orientation: event.orientation, + tilt: event.tilt, + down: event.down, + synthesized: event.synthesized, + transform: event.transform, original: null, ); @override - PointerExitEvent transformed(Matrix4 transform) { + PointerExitEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -1124,7 +1123,7 @@ class PointerExitEvent extends PointerEvent { down: down, synthesized: synthesized, transform: transform, - original: original as PointerExitEvent ?? this, + original: original as PointerExitEvent? ?? this, embedderId: embedderId, ); } @@ -1146,7 +1145,7 @@ class PointerDownEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, int buttons = kPrimaryButton, bool obscured = false, double pressure = 1.0, @@ -1160,8 +1159,8 @@ class PointerDownEvent extends PointerEvent { double radiusMax = 0.0, double orientation = 0.0, double tilt = 0.0, - Matrix4 transform, - PointerDownEvent original, + Matrix4? transform, + PointerDownEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -1191,7 +1190,7 @@ class PointerDownEvent extends PointerEvent { ); @override - PointerDownEvent transformed(Matrix4 transform) { + PointerDownEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -1216,7 +1215,7 @@ class PointerDownEvent extends PointerEvent { orientation: orientation, tilt: tilt, transform: transform, - original: original as PointerDownEvent ?? this, + original: original as PointerDownEvent? ?? this, embedderId: embedderId, ); } @@ -1241,9 +1240,9 @@ class PointerMoveEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, Offset delta = Offset.zero, - Offset localDelta, + Offset? localDelta, int buttons = kPrimaryButton, bool obscured = false, double pressure = 1.0, @@ -1259,8 +1258,8 @@ class PointerMoveEvent extends PointerEvent { double tilt = 0.0, int platformData = 0, bool synthesized = false, - Matrix4 transform, - PointerMoveEvent original, + Matrix4? transform, + PointerMoveEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -1294,7 +1293,7 @@ class PointerMoveEvent extends PointerEvent { ); @override - PointerMoveEvent transformed(Matrix4 transform) { + PointerMoveEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -1330,7 +1329,7 @@ class PointerMoveEvent extends PointerEvent { platformData: platformData, synthesized: synthesized, transform: transform, - original: original as PointerMoveEvent ?? this, + original: original as PointerMoveEvent? ?? this, embedderId: embedderId, ); } @@ -1352,7 +1351,7 @@ class PointerUpEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, int buttons = 0, bool obscured = false, // Allow pressure customization here because PointerUpEvent can contain @@ -1369,8 +1368,8 @@ class PointerUpEvent extends PointerEvent { double radiusMax = 0.0, double orientation = 0.0, double tilt = 0.0, - Matrix4 transform, - PointerUpEvent original, + Matrix4? transform, + PointerUpEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -1400,7 +1399,7 @@ class PointerUpEvent extends PointerEvent { ); @override - PointerUpEvent transformed(Matrix4 transform) { + PointerUpEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -1426,7 +1425,7 @@ class PointerUpEvent extends PointerEvent { orientation: orientation, tilt: tilt, transform: transform, - original: original as PointerUpEvent ?? this, + original: original as PointerUpEvent? ?? this, embedderId: embedderId, ); } @@ -1451,9 +1450,9 @@ abstract class PointerSignalEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.mouse, int device = 0, Offset position = Offset.zero, - Offset localPosition, - Matrix4 transform, - PointerSignalEvent original, + Offset? localPosition, + Matrix4? transform, + PointerSignalEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -1486,10 +1485,10 @@ class PointerScrollEvent extends PointerSignalEvent { PointerDeviceKind kind = PointerDeviceKind.mouse, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, this.scrollDelta = Offset.zero, - Matrix4 transform, - PointerScrollEvent original, + Matrix4? transform, + PointerScrollEvent? original, int embedderId = 0, }) : assert(timeStamp != null), assert(kind != null), @@ -1511,7 +1510,7 @@ class PointerScrollEvent extends PointerSignalEvent { final Offset scrollDelta; @override - PointerScrollEvent transformed(Matrix4 transform) { + PointerScrollEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -1523,7 +1522,7 @@ class PointerScrollEvent extends PointerSignalEvent { localPosition: PointerEvent.transformPosition(transform, position), scrollDelta: scrollDelta, transform: transform, - original: original as PointerScrollEvent ?? this, + original: original as PointerScrollEvent? ?? this, embedderId: embedderId, ); } @@ -1551,7 +1550,7 @@ class PointerCancelEvent extends PointerEvent { PointerDeviceKind kind = PointerDeviceKind.touch, int device = 0, Offset position = Offset.zero, - Offset localPosition, + Offset? localPosition, int buttons = 0, bool obscured = false, double pressureMin = 1.0, @@ -1565,8 +1564,8 @@ class PointerCancelEvent extends PointerEvent { double radiusMax = 0.0, double orientation = 0.0, double tilt = 0.0, - Matrix4 transform, - PointerCancelEvent original, + Matrix4? transform, + PointerCancelEvent? original, int embedderId = 0, }) : super( timeStamp: timeStamp, @@ -1596,7 +1595,7 @@ class PointerCancelEvent extends PointerEvent { ); @override - PointerCancelEvent transformed(Matrix4 transform) { + PointerCancelEvent transformed(Matrix4? transform) { if (transform == null || transform == this.transform) { return this; } @@ -1621,7 +1620,7 @@ class PointerCancelEvent extends PointerEvent { orientation: orientation, tilt: tilt, transform: transform, - original: original as PointerCancelEvent ?? this, + original: original as PointerCancelEvent? ?? this, embedderId: embedderId, ); } diff --git a/packages/flutter/lib/src/gestures/force_press.dart b/packages/flutter/lib/src/gestures/force_press.dart index 486b292cca8..4f4702031ff 100644 --- a/packages/flutter/lib/src/gestures/force_press.dart +++ b/packages/flutter/lib/src/gestures/force_press.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:ui' show Offset; @@ -51,9 +50,9 @@ class ForcePressDetails { /// /// The [globalPosition] argument must not be null. ForcePressDetails({ - @required this.globalPosition, - Offset localPosition, - @required this.pressure, + required this.globalPosition, + Offset? localPosition, + required this.pressure, }) : assert(globalPosition != null), assert(pressure != null), localPosition = localPosition ?? globalPosition; @@ -128,8 +127,8 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { this.startPressure = 0.4, this.peakPressure = 0.85, this.interpolation = _inverseLerp, - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : assert(startPressure != null), assert(peakPressure != null), assert(interpolation != null), @@ -143,7 +142,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { /// /// The position of the pointer is provided in the callback's `details` /// argument, which is a [ForcePressDetails] object. - GestureForcePressStartCallback onStart; + GestureForcePressStartCallback? onStart; /// A pointer is in contact with the screen and is either moving on the plane /// of the screen, pressing the screen with varying forces or both @@ -154,7 +153,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { /// matter what the pressure is during this time period. The position and /// pressure of the pointer is provided in the callback's `details` argument, /// which is a [ForcePressDetails] object. - GestureForcePressUpdateCallback onUpdate; + GestureForcePressUpdateCallback? onUpdate; /// A pointer is in contact with the screen and has just pressed with a force /// exceeding the [peakPressure]. This is an arbitrary second level action @@ -163,13 +162,13 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { /// /// The position of the pointer is provided in the callback's `details` /// argument, which is a [ForcePressDetails] object. - GestureForcePressPeakCallback onPeak; + GestureForcePressPeakCallback? onPeak; /// A pointer is no longer in contact with the screen. /// /// The position of the pointer is provided in the callback's `details` /// argument, which is a [ForcePressDetails] object. - GestureForcePressEndCallback onEnd; + GestureForcePressEndCallback? onEnd; /// The pressure of the press required to initiate a force press. /// @@ -209,8 +208,8 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { /// ``` final GestureForceInterpolation interpolation; - OffsetPair _lastPosition; - double _lastPressure; + late OffsetPair _lastPosition; + late double _lastPressure; _ForceState _state = _ForceState.ready; @override @@ -264,7 +263,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { if (pressure > startPressure && _state == _ForceState.accepted) { _state = _ForceState.started; if (onStart != null) { - invokeCallback('onStart', () => onStart(ForcePressDetails( + invokeCallback('onStart', () => onStart!(ForcePressDetails( pressure: pressure, globalPosition: _lastPosition.global, localPosition: _lastPosition.local, @@ -275,7 +274,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { (_state == _ForceState.started)) { _state = _ForceState.peaked; if (onPeak != null) { - invokeCallback('onPeak', () => onPeak(ForcePressDetails( + invokeCallback('onPeak', () => onPeak!(ForcePressDetails( pressure: pressure, globalPosition: event.position, localPosition: event.localPosition, @@ -285,7 +284,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { if (onUpdate != null && !pressure.isNaN && (_state == _ForceState.started || _state == _ForceState.peaked)) { if (onUpdate != null) { - invokeCallback('onUpdate', () => onUpdate(ForcePressDetails( + invokeCallback('onUpdate', () => onUpdate!(ForcePressDetails( pressure: pressure, globalPosition: event.position, localPosition: event.localPosition, @@ -302,7 +301,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { _state = _ForceState.accepted; if (onStart != null && _state == _ForceState.started) { - invokeCallback('onStart', () => onStart(ForcePressDetails( + invokeCallback('onStart', () => onStart!(ForcePressDetails( pressure: _lastPressure, globalPosition: _lastPosition.global, localPosition: _lastPosition.local, @@ -319,7 +318,7 @@ class ForcePressGestureRecognizer extends OneSequenceGestureRecognizer { } if (wasAccepted && onEnd != null) { if (onEnd != null) { - invokeCallback('onEnd', () => onEnd(ForcePressDetails( + invokeCallback('onEnd', () => onEnd!(ForcePressDetails( pressure: 0.0, globalPosition: _lastPosition.global, localPosition: _lastPosition.local, diff --git a/packages/flutter/lib/src/gestures/hit_test.dart b/packages/flutter/lib/src/gestures/hit_test.dart index fe06d2285f9..4a471201f0d 100644 --- a/packages/flutter/lib/src/gestures/hit_test.dart +++ b/packages/flutter/lib/src/gestures/hit_test.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 'package:flutter/foundation.dart'; import 'package:vector_math/vector_math_64.dart'; @@ -13,8 +12,7 @@ import 'events.dart'; abstract class HitTestable { // This class is intended to be used as an interface, and should not be // extended directly; this constructor prevents instantiation and extension. - // ignore: unused_element - factory HitTestable._() => null; + HitTestable._(); /// Check whether the given position hits this object. /// @@ -27,8 +25,7 @@ abstract class HitTestable { abstract class HitTestDispatcher { // This class is intended to be used as an interface, and should not be // extended directly; this constructor prevents instantiation and extension. - // ignore: unused_element - factory HitTestDispatcher._() => null; + HitTestDispatcher._(); /// Override this method to dispatch events. void dispatchEvent(PointerEvent event, HitTestResult result); @@ -38,8 +35,7 @@ abstract class HitTestDispatcher { abstract class HitTestTarget { // This class is intended to be used as an interface, and should not be // extended directly; this constructor prevents instantiation and extension. - // ignore: unused_element - factory HitTestTarget._() => null; + HitTestTarget._(); /// Override this method to receive events. void handleEvent(PointerEvent event, HitTestEntry entry); @@ -67,8 +63,8 @@ class HitTestEntry { /// /// * [BoxHitTestResult.addWithPaintTransform], which is used during hit testing /// to build up this transform. - Matrix4 get transform => _transform; - Matrix4 _transform; + Matrix4? get transform => _transform; + Matrix4? _transform; } // A type of data that can be applied to a matrix by left-multiplication. diff --git a/packages/flutter/lib/src/gestures/long_press.dart b/packages/flutter/lib/src/gestures/long_press.dart index 23e7f724b0a..9603d8d2b14 100644 --- a/packages/flutter/lib/src/gestures/long_press.dart +++ b/packages/flutter/lib/src/gestures/long_press.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 'arena.dart'; import 'constants.dart'; @@ -56,7 +55,7 @@ class LongPressStartDetails { /// The [globalPosition] argument must not be null. const LongPressStartDetails({ this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -80,9 +79,9 @@ class LongPressMoveUpdateDetails { /// The [globalPosition] and [offsetFromOrigin] arguments must not be null. const LongPressMoveUpdateDetails({ this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, this.offsetFromOrigin = Offset.zero, - Offset localOffsetFromOrigin, + Offset? localOffsetFromOrigin, }) : assert(globalPosition != null), assert(offsetFromOrigin != null), localPosition = localPosition ?? globalPosition, @@ -118,7 +117,7 @@ class LongPressEndDetails { /// The [globalPosition] argument must not be null. const LongPressEndDetails({ this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, this.velocity = Velocity.zero, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -162,10 +161,10 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// The [duration] argument can be used to overwrite the default duration /// after which the long press will be recognized. LongPressGestureRecognizer({ - Duration duration, - double postAcceptSlopTolerance, - PointerDeviceKind kind, - Object debugOwner, + Duration? duration, + double? postAcceptSlopTolerance, + PointerDeviceKind? kind, + Object? debugOwner, }) : super( deadline: duration ?? kLongPressTimeout, postAcceptSlopTolerance: postAcceptSlopTolerance, @@ -174,10 +173,10 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { ); bool _longPressAccepted = false; - OffsetPair _longPressOrigin; + OffsetPair? _longPressOrigin; // The buttons sent by `PointerDownEvent`. If a `PointerMoveEvent` comes with a // different set of buttons, the gesture is canceled. - int _initialButtons; + int? _initialButtons; /// Called when a long press gesture by a primary button has been recognized. /// @@ -186,7 +185,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kPrimaryButton], the button this callback responds to. /// * [onLongPressStart], which has the same timing but has data for the /// press location. - GestureLongPressCallback onLongPress; + GestureLongPressCallback? onLongPress; /// Called when a long press gesture by a primary button has been recognized. /// @@ -195,7 +194,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kPrimaryButton], the button this callback responds to. /// * [onLongPress], which has the same timing but without details. /// * [LongPressStartDetails], which is passed as an argument to this callback. - GestureLongPressStartCallback onLongPressStart; + GestureLongPressStartCallback? onLongPressStart; /// Called when moving after the long press by a primary button is recognized. /// @@ -204,7 +203,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kPrimaryButton], the button this callback responds to. /// * [LongPressMoveUpdateDetails], which is passed as an argument to this /// callback. - GestureLongPressMoveUpdateCallback onLongPressMoveUpdate; + GestureLongPressMoveUpdateCallback? onLongPressMoveUpdate; /// Called when the pointer stops contacting the screen after a long-press /// by a primary button. @@ -214,7 +213,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kPrimaryButton], the button this callback responds to. /// * [onLongPressEnd], which has the same timing but has data for the up /// gesture location. - GestureLongPressUpCallback onLongPressUp; + GestureLongPressUpCallback? onLongPressUp; /// Called when the pointer stops contacting the screen after a long-press /// by a primary button. @@ -225,7 +224,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [onLongPressUp], which has the same timing, but without details. /// * [LongPressEndDetails], which is passed as an argument to this /// callback. - GestureLongPressEndCallback onLongPressEnd; + GestureLongPressEndCallback? onLongPressEnd; /// Called when a long press gesture by a secondary button has been /// recognized. @@ -235,7 +234,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kSecondaryButton], the button this callback responds to. /// * [onSecondaryLongPressStart], which has the same timing but has data for /// the press location. - GestureLongPressCallback onSecondaryLongPress; + GestureLongPressCallback? onSecondaryLongPress; /// Called when a long press gesture by a secondary button has been recognized. /// @@ -245,7 +244,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [onSecondaryLongPress], which has the same timing but without details. /// * [LongPressStartDetails], which is passed as an argument to this /// callback. - GestureLongPressStartCallback onSecondaryLongPressStart; + GestureLongPressStartCallback? onSecondaryLongPressStart; /// Called when moving after the long press by a secondary button is /// recognized. @@ -255,7 +254,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kSecondaryButton], the button this callback responds to. /// * [LongPressMoveUpdateDetails], which is passed as an argument to this /// callback. - GestureLongPressMoveUpdateCallback onSecondaryLongPressMoveUpdate; + GestureLongPressMoveUpdateCallback? onSecondaryLongPressMoveUpdate; /// Called when the pointer stops contacting the screen after a long-press by /// a secondary button. @@ -265,7 +264,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [kSecondaryButton], the button this callback responds to. /// * [onSecondaryLongPressEnd], which has the same timing but has data for /// the up gesture location. - GestureLongPressUpCallback onSecondaryLongPressUp; + GestureLongPressUpCallback? onSecondaryLongPressUp; /// Called when the pointer stops contacting the screen after a long-press by /// a secondary button. @@ -276,9 +275,9 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { /// * [onSecondaryLongPressUp], which has the same timing, but without /// details. /// * [LongPressEndDetails], which is passed as an argument to this callback. - GestureLongPressEndCallback onSecondaryLongPressEnd; + GestureLongPressEndCallback? onSecondaryLongPressEnd; - VelocityTracker _velocityTracker; + VelocityTracker? _velocityTracker; @override bool isPointerAllowed(PointerDownEvent event) { @@ -310,7 +309,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { // Exceeding the deadline puts the gesture in the accepted state. resolve(GestureDisposition.accepted); _longPressAccepted = true; - super.acceptGesture(primaryPointer); + super.acceptGesture(primaryPointer!); _checkLongPressStart(); } @@ -319,11 +318,11 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { if (!event.synthesized) { if (event is PointerDownEvent) { _velocityTracker = VelocityTracker(); - _velocityTracker.addPosition(event.timeStamp, event.localPosition); + _velocityTracker!.addPosition(event.timeStamp, event.localPosition); } if (event is PointerMoveEvent) { assert(_velocityTracker != null); - _velocityTracker.addPosition(event.timeStamp, event.localPosition); + _velocityTracker!.addPosition(event.timeStamp, event.localPosition); } } @@ -344,7 +343,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { } else if (event is PointerMoveEvent) { if (event.buttons != _initialButtons) { resolve(GestureDisposition.rejected); - stopTrackingPointer(primaryPointer); + stopTrackingPointer(primaryPointer!); } else if (_longPressAccepted) { _checkLongPressMoveUpdate(event); } @@ -356,26 +355,26 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { case kPrimaryButton: if (onLongPressStart != null) { final LongPressStartDetails details = LongPressStartDetails( - globalPosition: _longPressOrigin.global, - localPosition: _longPressOrigin.local, + globalPosition: _longPressOrigin!.global, + localPosition: _longPressOrigin!.local, ); - invokeCallback('onLongPressStart', () => onLongPressStart(details)); + invokeCallback('onLongPressStart', () => onLongPressStart!(details)); } if (onLongPress != null) { - invokeCallback('onLongPress', onLongPress); + invokeCallback('onLongPress', onLongPress!); } break; case kSecondaryButton: if (onSecondaryLongPressStart != null) { final LongPressStartDetails details = LongPressStartDetails( - globalPosition: _longPressOrigin.global, - localPosition: _longPressOrigin.local, + globalPosition: _longPressOrigin!.global, + localPosition: _longPressOrigin!.local, ); invokeCallback( - 'onSecondaryLongPressStart', () => onSecondaryLongPressStart(details)); + 'onSecondaryLongPressStart', () => onSecondaryLongPressStart!(details)); } if (onSecondaryLongPress != null) { - invokeCallback('onSecondaryLongPress', onSecondaryLongPress); + invokeCallback('onSecondaryLongPress', onSecondaryLongPress!); } break; default: @@ -387,20 +386,20 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { final LongPressMoveUpdateDetails details = LongPressMoveUpdateDetails( globalPosition: event.position, localPosition: event.localPosition, - offsetFromOrigin: event.position - _longPressOrigin.global, - localOffsetFromOrigin: event.localPosition - _longPressOrigin.local, + offsetFromOrigin: event.position - _longPressOrigin!.global, + localOffsetFromOrigin: event.localPosition - _longPressOrigin!.local, ); switch (_initialButtons) { case kPrimaryButton: if (onLongPressMoveUpdate != null) { invokeCallback('onLongPressMoveUpdate', - () => onLongPressMoveUpdate(details)); + () => onLongPressMoveUpdate!(details)); } break; case kSecondaryButton: if (onSecondaryLongPressMoveUpdate != null) { invokeCallback('onSecondaryLongPressMoveUpdate', - () => onSecondaryLongPressMoveUpdate(details)); + () => onSecondaryLongPressMoveUpdate!(details)); } break; default: @@ -409,7 +408,7 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { } void _checkLongPressEnd(PointerEvent event) { - final VelocityEstimate estimate = _velocityTracker.getVelocityEstimate(); + final VelocityEstimate? estimate = _velocityTracker!.getVelocityEstimate(); final Velocity velocity = estimate == null ? Velocity.zero : Velocity(pixelsPerSecond: estimate.pixelsPerSecond); @@ -423,18 +422,18 @@ class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer { switch (_initialButtons) { case kPrimaryButton: if (onLongPressEnd != null) { - invokeCallback('onLongPressEnd', () => onLongPressEnd(details)); + invokeCallback('onLongPressEnd', () => onLongPressEnd!(details)); } if (onLongPressUp != null) { - invokeCallback('onLongPressUp', onLongPressUp); + invokeCallback('onLongPressUp', onLongPressUp!); } break; case kSecondaryButton: if (onSecondaryLongPressEnd != null) { - invokeCallback('onSecondaryLongPressEnd', () => onSecondaryLongPressEnd(details)); + invokeCallback('onSecondaryLongPressEnd', () => onSecondaryLongPressEnd!(details)); } if (onSecondaryLongPressUp != null) { - invokeCallback('onSecondaryLongPressUp', onSecondaryLongPressUp); + invokeCallback('onSecondaryLongPressUp', onSecondaryLongPressUp!); } break; default: diff --git a/packages/flutter/lib/src/gestures/lsq_solver.dart b/packages/flutter/lib/src/gestures/lsq_solver.dart index 1f13d09f468..e0a8ede4549 100644 --- a/packages/flutter/lib/src/gestures/lsq_solver.dart +++ b/packages/flutter/lib/src/gestures/lsq_solver.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:typed_data'; @@ -76,7 +75,7 @@ class PolynomialFit { /// An indicator of the quality of the fit. /// /// Larger values indicate greater quality. - double confidence; + late double confidence; } /// Uses the least-squares algorithm to fit a polynomial to a set of data. @@ -98,7 +97,9 @@ class LeastSquaresSolver { final List w; /// Fits a polynomial of the given degree to the data points. - PolynomialFit solve(int degree) { + /// + /// When there is not enough data to fit a curve null is returned. + PolynomialFit? solve(int degree) { if (degree > x.length) // Not enough data to fit a curve. return null; diff --git a/packages/flutter/lib/src/gestures/monodrag.dart b/packages/flutter/lib/src/gestures/monodrag.dart index 59a7454f4f4..36f250adb7d 100644 --- a/packages/flutter/lib/src/gestures/monodrag.dart +++ b/packages/flutter/lib/src/gestures/monodrag.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 'package:flutter/foundation.dart'; import 'package:vector_math/vector_math_64.dart'; @@ -62,8 +61,8 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { /// /// {@macro flutter.gestures.gestureRecognizer.kind} DragGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, this.dragStartBehavior = DragStartBehavior.start, }) : assert(dragStartBehavior != null), super(debugOwner: debugOwner, kind: kind); @@ -101,7 +100,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { /// /// * [kPrimaryButton], the button this callback responds to. /// * [DragDownDetails], which is passed as an argument to this callback. - GestureDragDownCallback onDown; + GestureDragDownCallback? onDown; /// A pointer has contacted the screen with a primary button and has begun to /// move. @@ -118,7 +117,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { /// /// * [kPrimaryButton], the button this callback responds to. /// * [DragStartDetails], which is passed as an argument to this callback. - GestureDragStartCallback onStart; + GestureDragStartCallback? onStart; /// A pointer that is in contact with the screen with a primary button and /// moving has moved again. @@ -130,7 +129,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { /// /// * [kPrimaryButton], the button this callback responds to. /// * [DragUpdateDetails], which is passed as an argument to this callback. - GestureDragUpdateCallback onUpdate; + GestureDragUpdateCallback? onUpdate; /// A pointer that was previously in contact with the screen with a primary /// button and moving is no longer in contact with the screen and was moving @@ -143,48 +142,48 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { /// /// * [kPrimaryButton], the button this callback responds to. /// * [DragEndDetails], which is passed as an argument to this callback. - GestureDragEndCallback onEnd; + GestureDragEndCallback? onEnd; /// The pointer that previously triggered [onDown] did not complete. /// /// See also: /// /// * [kPrimaryButton], the button this callback responds to. - GestureDragCancelCallback onCancel; + GestureDragCancelCallback? onCancel; /// The minimum distance an input pointer drag must have moved to /// to be considered a fling gesture. /// /// This value is typically compared with the distance traveled along the /// scrolling axis. If null then [kTouchSlop] is used. - double minFlingDistance; + double? minFlingDistance; /// The minimum velocity for an input pointer drag to be considered fling. /// /// This value is typically compared with the magnitude of fling gesture's /// velocity along the scrolling axis. If null then [kMinFlingVelocity] /// is used. - double minFlingVelocity; + double? minFlingVelocity; /// Fling velocity magnitudes will be clamped to this value. /// /// If null then [kMaxFlingVelocity] is used. - double maxFlingVelocity; + double? maxFlingVelocity; _DragState _state = _DragState.ready; - OffsetPair _initialPosition; - OffsetPair _pendingDragOffset; - Duration _lastPendingEventTimestamp; + late OffsetPair _initialPosition; + late OffsetPair _pendingDragOffset; + Duration? _lastPendingEventTimestamp; // The buttons sent by `PointerDownEvent`. If a `PointerMoveEvent` comes with a // different set of buttons, the gesture is canceled. - int _initialButtons; - Matrix4 _lastTransform; + int? _initialButtons; + Matrix4? _lastTransform; /// Distance moved in the global coordinate space of the screen in drag direction. /// /// If drag is only allowed along a defined axis, this value may be negative to /// differentiate the direction of the drag. - double _globalDistanceMoved; + late double _globalDistanceMoved; /// Determines if a gesture is a fling or not based on velocity. /// @@ -194,7 +193,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { bool isFlingGesture(VelocityEstimate estimate); Offset _getDeltaForDetails(Offset delta); - double _getPrimaryValueFromOffset(Offset value); + double? _getPrimaryValueFromOffset(Offset value); bool get _hasSufficientGlobalDistanceToAccept; final Map _velocityTrackers = {}; @@ -246,7 +245,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { assert(_state != _DragState.ready); if (!event.synthesized && (event is PointerDownEvent || event is PointerMoveEvent)) { - final VelocityTracker tracker = _velocityTrackers[event.pointer]; + final VelocityTracker tracker = _velocityTrackers[event.pointer]!; assert(tracker != null); tracker.addPosition(event.timeStamp, event.localPosition); } @@ -269,7 +268,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { _lastPendingEventTimestamp = event.timeStamp; _lastTransform = event.transform; final Offset movedLocally = _getDeltaForDetails(event.localDelta); - final Matrix4 localToGlobalTransform = event.transform == null ? null : Matrix4.tryInvert(event.transform); + final Matrix4? localToGlobalTransform = event.transform == null ? null : Matrix4.tryInvert(event.transform!); _globalDistanceMoved += PointerEvent.transformDeltaViaPositions( transform: localToGlobalTransform, untransformedDelta: movedLocally, @@ -292,8 +291,8 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { if (_state != _DragState.accepted) { _state = _DragState.accepted; final OffsetPair delta = _pendingDragOffset; - final Duration timestamp = _lastPendingEventTimestamp; - final Matrix4 transform = _lastTransform; + final Duration timestamp = _lastPendingEventTimestamp!; + final Matrix4? transform = _lastTransform; Offset localUpdateDelta; switch (dragStartBehavior) { case DragStartBehavior.start: @@ -309,7 +308,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { _lastTransform = null; _checkStart(timestamp); if (localUpdateDelta != Offset.zero && onUpdate != null) { - final Matrix4 localToGlobal = transform != null ? Matrix4.tryInvert(transform) : null; + final Matrix4? localToGlobal = transform != null ? Matrix4.tryInvert(transform) : null; final Offset correctedLocalPosition = _initialPosition.local + localUpdateDelta; final Offset globalUpdateDelta = PointerEvent.transformDeltaViaPositions( untransformedEndPosition: correctedLocalPosition, @@ -372,7 +371,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { localPosition: _initialPosition.local, ); if (onDown != null) - invokeCallback('onDown', () => onDown(details)); + invokeCallback('onDown', () => onDown!(details)); } void _checkStart(Duration timestamp) { @@ -383,15 +382,15 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { localPosition: _initialPosition.local, ); if (onStart != null) - invokeCallback('onStart', () => onStart(details)); + invokeCallback('onStart', () => onStart!(details)); } void _checkUpdate({ - Duration sourceTimeStamp, - Offset delta, - double primaryDelta, - Offset globalPosition, - Offset localPosition, + Duration? sourceTimeStamp, + required Offset delta, + double? primaryDelta, + required Offset globalPosition, + Offset? localPosition, }) { assert(_initialButtons == kPrimaryButton); final DragUpdateDetails details = DragUpdateDetails( @@ -402,7 +401,7 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { localPosition: localPosition, ); if (onUpdate != null) - invokeCallback('onUpdate', () => onUpdate(details)); + invokeCallback('onUpdate', () => onUpdate!(details)); } void _checkEnd(int pointer) { @@ -410,13 +409,13 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { if (onEnd == null) return; - final VelocityTracker tracker = _velocityTrackers[pointer]; + final VelocityTracker tracker = _velocityTrackers[pointer]!; assert(tracker != null); DragEndDetails details; String Function() debugReport; - final VelocityEstimate estimate = tracker.getVelocityEstimate(); + final VelocityEstimate? estimate = tracker.getVelocityEstimate(); if (estimate != null && isFlingGesture(estimate)) { final Velocity velocity = Velocity(pixelsPerSecond: estimate.pixelsPerSecond) .clampMagnitude(minFlingVelocity ?? kMinFlingVelocity, maxFlingVelocity ?? kMaxFlingVelocity); @@ -438,13 +437,13 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { return '$estimate; judged to not be a fling.'; }; } - invokeCallback('onEnd', () => onEnd(details), debugReport: debugReport); + invokeCallback('onEnd', () => onEnd!(details), debugReport: debugReport); } void _checkCancel() { assert(_initialButtons == kPrimaryButton); if (onCancel != null) - invokeCallback('onCancel', onCancel); + invokeCallback('onCancel', onCancel!); } @override @@ -474,8 +473,8 @@ class VerticalDragGestureRecognizer extends DragGestureRecognizer { /// /// {@macro flutter.gestures.gestureRecognizer.kind} VerticalDragGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); @override @@ -513,8 +512,8 @@ class HorizontalDragGestureRecognizer extends DragGestureRecognizer { /// /// {@macro flutter.gestures.gestureRecognizer.kind} HorizontalDragGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); @override @@ -548,7 +547,7 @@ class HorizontalDragGestureRecognizer extends DragGestureRecognizer { /// some time has passed. class PanGestureRecognizer extends DragGestureRecognizer { /// Create a gesture recognizer for tracking movement on a plane. - PanGestureRecognizer({ Object debugOwner }) : super(debugOwner: debugOwner); + PanGestureRecognizer({ Object? debugOwner }) : super(debugOwner: debugOwner); @override bool isFlingGesture(VelocityEstimate estimate) { @@ -567,7 +566,7 @@ class PanGestureRecognizer extends DragGestureRecognizer { Offset _getDeltaForDetails(Offset delta) => delta; @override - double _getPrimaryValueFromOffset(Offset value) => null; + double? _getPrimaryValueFromOffset(Offset value) => null; @override String get debugDescription => 'pan'; diff --git a/packages/flutter/lib/src/gestures/multidrag.dart b/packages/flutter/lib/src/gestures/multidrag.dart index cc8b682ac2f..c043b0b408e 100644 --- a/packages/flutter/lib/src/gestures/multidrag.dart +++ b/packages/flutter/lib/src/gestures/multidrag.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:async'; import 'dart:ui' show Offset; @@ -19,7 +18,7 @@ import 'recognizer.dart'; import 'velocity_tracker.dart'; /// Signature for when [MultiDragGestureRecognizer] recognizes the start of a drag gesture. -typedef GestureMultiDragStartCallback = Drag Function(Offset position); +typedef GestureMultiDragStartCallback = Drag? Function(Offset position); /// Per-pointer state for a [MultiDragGestureRecognizer]. /// @@ -36,7 +35,7 @@ abstract class MultiDragPointerState { final Offset initialPosition; final VelocityTracker _velocityTracker = VelocityTracker(); - Drag _client; + Drag? _client; /// The offset of the pointer from the last position that was reported to the client. /// @@ -44,12 +43,12 @@ abstract class MultiDragPointerState { /// distance before this movement will be recognized as a drag. This field /// accumulates that movement so that we can report it to the client after /// the drag starts. - Offset get pendingDelta => _pendingDelta; - Offset _pendingDelta = Offset.zero; + Offset? get pendingDelta => _pendingDelta; + Offset? _pendingDelta = Offset.zero; - Duration _lastPendingEventTimestamp; + Duration? _lastPendingEventTimestamp; - GestureArenaEntry _arenaEntry; + GestureArenaEntry? _arenaEntry; void _setArenaEntry(GestureArenaEntry entry) { assert(_arenaEntry == null); assert(pendingDelta != null); @@ -61,7 +60,7 @@ abstract class MultiDragPointerState { @protected @mustCallSuper void resolve(GestureDisposition disposition) { - _arenaEntry.resolve(disposition); + _arenaEntry!.resolve(disposition); } void _move(PointerMoveEvent event) { @@ -71,14 +70,14 @@ abstract class MultiDragPointerState { if (_client != null) { assert(pendingDelta == null); // Call client last to avoid reentrancy. - _client.update(DragUpdateDetails( + _client!.update(DragUpdateDetails( sourceTimeStamp: event.timeStamp, delta: event.delta, globalPosition: event.position, )); } else { assert(pendingDelta != null); - _pendingDelta += event.delta; + _pendingDelta = _pendingDelta! + event.delta; _lastPendingEventTimestamp = event.timeStamp; checkForResolutionAfterMove(); } @@ -119,13 +118,13 @@ abstract class MultiDragPointerState { _client = client; final DragUpdateDetails details = DragUpdateDetails( sourceTimeStamp: _lastPendingEventTimestamp, - delta: pendingDelta, + delta: pendingDelta!, globalPosition: initialPosition, ); _pendingDelta = null; _lastPendingEventTimestamp = null; // Call client last to avoid reentrancy. - _client.update(details); + _client!.update(details); } void _up() { @@ -133,7 +132,7 @@ abstract class MultiDragPointerState { if (_client != null) { assert(pendingDelta == null); final DragEndDetails details = DragEndDetails(velocity: _velocityTracker.getVelocity()); - final Drag client = _client; + final Drag client = _client!; _client = null; // Call client last to avoid reentrancy. client.end(details); @@ -148,7 +147,7 @@ abstract class MultiDragPointerState { assert(_arenaEntry != null); if (_client != null) { assert(pendingDelta == null); - final Drag client = _client; + final Drag client = _client!; _client = null; // Call client last to avoid reentrancy. client.cancel(); @@ -195,28 +194,28 @@ abstract class MultiDragPointerState { abstract class MultiDragGestureRecognizer extends GestureRecognizer { /// Initialize the object. MultiDragGestureRecognizer({ - @required Object debugOwner, - PointerDeviceKind kind, + required Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); /// Called when this class recognizes the start of a drag gesture. /// /// The remaining notifications for this drag gesture are delivered to the /// [Drag] object returned by this callback. - GestureMultiDragStartCallback onStart; + GestureMultiDragStartCallback? onStart; - Map _pointers = {}; + Map? _pointers = {}; @override void addAllowedPointer(PointerDownEvent event) { assert(_pointers != null); assert(event.pointer != null); assert(event.position != null); - assert(!_pointers.containsKey(event.pointer)); + assert(!_pointers!.containsKey(event.pointer)); final T state = createNewPointerState(event); - _pointers[event.pointer] = state; - GestureBinding.instance.pointerRouter.addRoute(event.pointer, _handleEvent); - state._setArenaEntry(GestureBinding.instance.gestureArena.add(event.pointer, this)); + _pointers![event.pointer] = state; + GestureBinding.instance!.pointerRouter.addRoute(event.pointer, _handleEvent); + state._setArenaEntry(GestureBinding.instance!.gestureArena.add(event.pointer, this)); } /// Subclasses should override this method to create per-pointer state @@ -230,8 +229,8 @@ abstract class MultiDragGestureRecognizer exten assert(event.pointer != null); assert(event.timeStamp != null); assert(event.position != null); - assert(_pointers.containsKey(event.pointer)); - final T state = _pointers[event.pointer]; + assert(_pointers!.containsKey(event.pointer)); + final T state = _pointers![event.pointer]!; if (event is PointerMoveEvent) { state._move(event); // We might be disposed here. @@ -256,20 +255,20 @@ abstract class MultiDragGestureRecognizer exten @override void acceptGesture(int pointer) { assert(_pointers != null); - final T state = _pointers[pointer]; + final T? state = _pointers![pointer]; if (state == null) return; // We might already have canceled this drag if the up comes before the accept. state.accepted((Offset initialPosition) => _startDrag(initialPosition, pointer)); } - Drag _startDrag(Offset initialPosition, int pointer) { + Drag? _startDrag(Offset initialPosition, int pointer) { assert(_pointers != null); - final T state = _pointers[pointer]; + final T state = _pointers![pointer]!; assert(state != null); assert(state._pendingDelta != null); - Drag drag; + Drag? drag; if (onStart != null) - drag = invokeCallback('onStart', () => onStart(initialPosition)); + drag = invokeCallback('onStart', () => onStart!(initialPosition)); if (drag != null) { state._startDrag(drag); } else { @@ -281,8 +280,8 @@ abstract class MultiDragGestureRecognizer exten @override void rejectGesture(int pointer) { assert(_pointers != null); - if (_pointers.containsKey(pointer)) { - final T state = _pointers[pointer]; + if (_pointers!.containsKey(pointer)) { + final T state = _pointers![pointer]!; assert(state != null); state.rejected(); _removeState(pointer); @@ -295,15 +294,15 @@ abstract class MultiDragGestureRecognizer exten // for the given pointer because dispose() has already removed it. return; } - assert(_pointers.containsKey(pointer)); - GestureBinding.instance.pointerRouter.removeRoute(pointer, _handleEvent); - _pointers.remove(pointer).dispose(); + assert(_pointers!.containsKey(pointer)); + GestureBinding.instance!.pointerRouter.removeRoute(pointer, _handleEvent); + _pointers!.remove(pointer)!.dispose(); } @override void dispose() { - _pointers.keys.toList().forEach(_removeState); - assert(_pointers.isEmpty); + _pointers!.keys.toList().forEach(_removeState); + assert(_pointers!.isEmpty); _pointers = null; super.dispose(); } @@ -315,7 +314,7 @@ class _ImmediatePointerState extends MultiDragPointerState { @override void checkForResolutionAfterMove() { assert(pendingDelta != null); - if (pendingDelta.distance > kTouchSlop) + if (pendingDelta!.distance > kTouchSlop) resolve(GestureDisposition.accepted); } @@ -344,8 +343,8 @@ class _ImmediatePointerState extends MultiDragPointerState { class ImmediateMultiDragGestureRecognizer extends MultiDragGestureRecognizer<_ImmediatePointerState> { /// Create a gesture recognizer for tracking multiple pointers at once. ImmediateMultiDragGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); @override @@ -364,7 +363,7 @@ class _HorizontalPointerState extends MultiDragPointerState { @override void checkForResolutionAfterMove() { assert(pendingDelta != null); - if (pendingDelta.dx.abs() > kTouchSlop) + if (pendingDelta!.dx.abs() > kTouchSlop) resolve(GestureDisposition.accepted); } @@ -393,8 +392,8 @@ class HorizontalMultiDragGestureRecognizer extends MultiDragGestureRecognizer<_H /// Create a gesture recognizer for tracking multiple pointers at once /// but only if they first move horizontally. HorizontalMultiDragGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); @override @@ -413,7 +412,7 @@ class _VerticalPointerState extends MultiDragPointerState { @override void checkForResolutionAfterMove() { assert(pendingDelta != null); - if (pendingDelta.dy.abs() > kTouchSlop) + if (pendingDelta!.dy.abs() > kTouchSlop) resolve(GestureDisposition.accepted); } @@ -442,8 +441,8 @@ class VerticalMultiDragGestureRecognizer extends MultiDragGestureRecognizer<_Ver /// Create a gesture recognizer for tracking multiple pointers at once /// but only if they first move vertically. VerticalMultiDragGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); @override @@ -462,16 +461,16 @@ class _DelayedPointerState extends MultiDragPointerState { _timer = Timer(delay, _delayPassed); } - Timer _timer; - GestureMultiDragStartCallback _starter; + Timer? _timer; + GestureMultiDragStartCallback? _starter; void _delayPassed() { assert(_timer != null); assert(pendingDelta != null); - assert(pendingDelta.distance <= kTouchSlop); + assert(pendingDelta!.distance <= kTouchSlop); _timer = null; if (_starter != null) { - _starter(initialPosition); + _starter!(initialPosition); _starter = null; } else { resolve(GestureDisposition.accepted); @@ -505,7 +504,7 @@ class _DelayedPointerState extends MultiDragPointerState { return; } assert(pendingDelta != null); - if (pendingDelta.distance > kTouchSlop) { + if (pendingDelta!.distance > kTouchSlop) { resolve(GestureDisposition.rejected); _ensureTimerStopped(); } @@ -545,8 +544,8 @@ class DelayedMultiDragGestureRecognizer extends MultiDragGestureRecognizer<_Dela /// can be changed for specific behaviors. DelayedMultiDragGestureRecognizer({ this.delay = kLongPressTimeout, - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : assert(delay != null), super(debugOwner: debugOwner, kind: kind); diff --git a/packages/flutter/lib/src/gestures/multitap.dart b/packages/flutter/lib/src/gestures/multitap.dart index 8e29287434c..ea83c1f0cc1 100644 --- a/packages/flutter/lib/src/gestures/multitap.dart +++ b/packages/flutter/lib/src/gestures/multitap.dart @@ -2,11 +2,9 @@ // 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:async'; import 'dart:ui' show Offset; -import 'package:flutter/foundation.dart' show required; import 'package:vector_math/vector_math_64.dart'; import 'arena.dart'; @@ -43,7 +41,7 @@ typedef GestureMultiTapCancelCallback = void Function(int pointer); /// CountdownZoned tracks whether the specified duration has elapsed since /// creation, honoring [Zone]. class _CountdownZoned { - _CountdownZoned({ @required Duration duration }) + _CountdownZoned({ required Duration duration }) : assert(duration != null) { Timer(duration, _onTimeout); } @@ -61,9 +59,9 @@ class _CountdownZoned { /// larger gesture. class _TapTracker { _TapTracker({ - @required PointerDownEvent event, + required PointerDownEvent event, this.entry, - @required Duration doubleTapMinTime, + required Duration doubleTapMinTime, }) : assert(doubleTapMinTime != null), assert(event != null), assert(event.buttons != null), @@ -73,24 +71,24 @@ class _TapTracker { _doubleTapMinTimeCountdown = _CountdownZoned(duration: doubleTapMinTime); final int pointer; - final GestureArenaEntry entry; + final GestureArenaEntry? entry; final Offset _initialGlobalPosition; final int initialButtons; final _CountdownZoned _doubleTapMinTimeCountdown; bool _isTrackingPointer = false; - void startTrackingPointer(PointerRoute route, Matrix4 transform) { + void startTrackingPointer(PointerRoute route, Matrix4? transform) { if (!_isTrackingPointer) { _isTrackingPointer = true; - GestureBinding.instance.pointerRouter.addRoute(pointer, route, transform); + GestureBinding.instance!.pointerRouter.addRoute(pointer, route, transform); } } void stopTrackingPointer(PointerRoute route) { if (_isTrackingPointer) { _isTrackingPointer = false; - GestureBinding.instance.pointerRouter.removeRoute(pointer, route); + GestureBinding.instance!.pointerRouter.removeRoute(pointer, route); } } @@ -119,8 +117,8 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { /// /// {@macro flutter.gestures.gestureRecognizer.kind} DoubleTapGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); // Implementation notes: @@ -152,10 +150,10 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { /// See also: /// /// * [kPrimaryButton], the button this callback responds to. - GestureDoubleTapCallback onDoubleTap; + GestureDoubleTapCallback? onDoubleTap; - Timer _doubleTapTimer; - _TapTracker _firstTap; + Timer? _doubleTapTimer; + _TapTracker? _firstTap; final Map _trackers = {}; @override @@ -176,10 +174,10 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { @override void addAllowedPointer(PointerEvent event) { if (_firstTap != null) { - if (!_firstTap.isWithinGlobalTolerance(event, kDoubleTapSlop)) { + if (!_firstTap!.isWithinGlobalTolerance(event, kDoubleTapSlop)) { // Ignore out-of-bounds second taps. return; - } else if (!_firstTap.hasElapsedMinTime() || !_firstTap.hasSameButton(event as PointerDownEvent)) { + } else if (!_firstTap!.hasElapsedMinTime() || !_firstTap!.hasSameButton(event as PointerDownEvent)) { // Restart when the second tap is too close to the first, or when buttons // mismatch. _reset(); @@ -193,7 +191,7 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { _stopDoubleTapTimer(); final _TapTracker tracker = _TapTracker( event: event as PointerDownEvent, - entry: GestureBinding.instance.gestureArena.add(event.pointer, this), + entry: GestureBinding.instance!.gestureArena.add(event.pointer, this), doubleTapMinTime: kDoubleTapMinTime, ); _trackers[event.pointer] = tracker; @@ -201,8 +199,7 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { } void _handleEvent(PointerEvent event) { - final _TapTracker tracker = _trackers[event.pointer]; - assert(tracker != null); + final _TapTracker tracker = _trackers[event.pointer]!; if (event is PointerUpEvent) { if (_firstTap == null) _registerFirstTap(tracker); @@ -221,11 +218,11 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { @override void rejectGesture(int pointer) { - _TapTracker tracker = _trackers[pointer]; + _TapTracker? tracker = _trackers[pointer]; // If tracker isn't in the list, check if this is the first tap tracker if (tracker == null && _firstTap != null && - _firstTap.pointer == pointer) + _firstTap!.pointer == pointer) tracker = _firstTap; // If tracker is still null, we rejected ourselves already if (tracker != null) @@ -234,7 +231,7 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { void _reject(_TapTracker tracker) { _trackers.remove(tracker.pointer); - tracker.entry.resolve(GestureDisposition.rejected); + tracker.entry!.resolve(GestureDisposition.rejected); _freezeTracker(tracker); // If the first tap is in progress, and we've run out of taps to track, // reset won't have any work to do. But if we're in the second tap, we need @@ -255,17 +252,17 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { if (_firstTap != null) { // Note, order is important below in order for the resolve -> reject logic // to work properly. - final _TapTracker tracker = _firstTap; + final _TapTracker tracker = _firstTap!; _firstTap = null; _reject(tracker); - GestureBinding.instance.gestureArena.release(tracker.pointer); + GestureBinding.instance!.gestureArena.release(tracker.pointer); } _clearTrackers(); } void _registerFirstTap(_TapTracker tracker) { _startDoubleTapTimer(); - GestureBinding.instance.gestureArena.hold(tracker.pointer); + GestureBinding.instance!.gestureArena.hold(tracker.pointer); // Note, order is important below in order for the clear -> reject logic to // work properly. _freezeTracker(tracker); @@ -275,8 +272,8 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { } void _registerSecondTap(_TapTracker tracker) { - _firstTap.entry.resolve(GestureDisposition.accepted); - tracker.entry.resolve(GestureDisposition.accepted); + _firstTap!.entry!.resolve(GestureDisposition.accepted); + tracker.entry!.resolve(GestureDisposition.accepted); _freezeTracker(tracker); _trackers.remove(tracker.pointer); _checkUp(tracker.initialButtons); @@ -298,7 +295,7 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { void _stopDoubleTapTimer() { if (_doubleTapTimer != null) { - _doubleTapTimer.cancel(); + _doubleTapTimer!.cancel(); _doubleTapTimer = null; } } @@ -306,7 +303,7 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { void _checkUp(int buttons) { assert(buttons == kPrimaryButton); if (onDoubleTap != null) - invokeCallback('onDoubleTap', onDoubleTap); + invokeCallback('onDoubleTap', onDoubleTap!); } @override @@ -319,13 +316,13 @@ class DoubleTapGestureRecognizer extends GestureRecognizer { class _TapGesture extends _TapTracker { _TapGesture({ - this.gestureRecognizer, - PointerEvent event, - Duration longTapDelay, + required this.gestureRecognizer, + required PointerEvent event, + required Duration longTapDelay, }) : _lastPosition = OffsetPair.fromEventPosition(event), super( event: event as PointerDownEvent, - entry: GestureBinding.instance.gestureArena.add(event.pointer, gestureRecognizer), + entry: GestureBinding.instance!.gestureArena.add(event.pointer, gestureRecognizer), doubleTapMinTime: kDoubleTapMinTime, ) { startTrackingPointer(handleEvent, event.transform); @@ -340,10 +337,10 @@ class _TapGesture extends _TapTracker { final MultiTapGestureRecognizer gestureRecognizer; bool _wonArena = false; - Timer _timer; + Timer? _timer; OffsetPair _lastPosition; - OffsetPair _finalPosition; + OffsetPair? _finalPosition; void handleEvent(PointerEvent event) { assert(event.pointer == pointer); @@ -384,12 +381,12 @@ class _TapGesture extends _TapTracker { if (_wonArena) reject(); else - entry.resolve(GestureDisposition.rejected); // eventually calls reject() + entry!.resolve(GestureDisposition.rejected); // eventually calls reject() } void _check() { if (_wonArena && _finalPosition != null) - gestureRecognizer._dispatchTap(pointer, _finalPosition); + gestureRecognizer._dispatchTap(pointer, _finalPosition!); } } @@ -409,31 +406,31 @@ class MultiTapGestureRecognizer extends GestureRecognizer { /// [onLongTapDown] is called immediately after [onTapDown]. MultiTapGestureRecognizer({ this.longTapDelay = Duration.zero, - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); /// A pointer that might cause a tap has contacted the screen at a particular /// location. - GestureMultiTapDownCallback onTapDown; + GestureMultiTapDownCallback? onTapDown; /// A pointer that will trigger a tap has stopped contacting the screen at a /// particular location. - GestureMultiTapUpCallback onTapUp; + GestureMultiTapUpCallback? onTapUp; /// A tap has occurred. - GestureMultiTapCallback onTap; + GestureMultiTapCallback? onTap; /// The pointer that previously triggered [onTapDown] will not end up causing /// a tap. - GestureMultiTapCancelCallback onTapCancel; + GestureMultiTapCancelCallback? onTapCancel; /// The amount of time between [onTapDown] and [onLongTapDown]. Duration longTapDelay; /// A pointer that might cause a tap is still in contact with the screen at a /// particular location after [longTapDelay]. - GestureMultiTapDownCallback onLongTapDown; + GestureMultiTapDownCallback? onLongTapDown; final Map _gestureMap = {}; @@ -447,7 +444,7 @@ class MultiTapGestureRecognizer extends GestureRecognizer { ); if (onTapDown != null) invokeCallback('onTapDown', () { - onTapDown(event.pointer, TapDownDetails( + onTapDown!(event.pointer, TapDownDetails( globalPosition: event.position, localPosition: event.localPosition, kind: event.kind, @@ -458,13 +455,13 @@ class MultiTapGestureRecognizer extends GestureRecognizer { @override void acceptGesture(int pointer) { assert(_gestureMap.containsKey(pointer)); - _gestureMap[pointer].accept(); + _gestureMap[pointer]!.accept(); } @override void rejectGesture(int pointer) { assert(_gestureMap.containsKey(pointer)); - _gestureMap[pointer].reject(); + _gestureMap[pointer]!.reject(); assert(!_gestureMap.containsKey(pointer)); } @@ -472,7 +469,7 @@ class MultiTapGestureRecognizer extends GestureRecognizer { assert(_gestureMap.containsKey(pointer)); _gestureMap.remove(pointer); if (onTapCancel != null) - invokeCallback('onTapCancel', () => onTapCancel(pointer)); + invokeCallback('onTapCancel', () => onTapCancel!(pointer)); } void _dispatchTap(int pointer, OffsetPair position) { @@ -480,20 +477,20 @@ class MultiTapGestureRecognizer extends GestureRecognizer { _gestureMap.remove(pointer); if (onTapUp != null) invokeCallback('onTapUp', () { - onTapUp(pointer, TapUpDetails( + onTapUp!(pointer, TapUpDetails( localPosition: position.local, globalPosition: position.global, )); }); if (onTap != null) - invokeCallback('onTap', () => onTap(pointer)); + invokeCallback('onTap', () => onTap!(pointer)); } void _dispatchLongTap(int pointer, OffsetPair lastPosition) { assert(_gestureMap.containsKey(pointer)); if (onLongTapDown != null) invokeCallback('onLongTapDown', () { - onLongTapDown( + onLongTapDown!( pointer, TapDownDetails( globalPosition: lastPosition.global, diff --git a/packages/flutter/lib/src/gestures/pointer_router.dart b/packages/flutter/lib/src/gestures/pointer_router.dart index eec4fd4df7d..4648e494506 100644 --- a/packages/flutter/lib/src/gestures/pointer_router.dart +++ b/packages/flutter/lib/src/gestures/pointer_router.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 'package:flutter/foundation.dart'; import 'package:vector_math/vector_math_64.dart'; @@ -14,8 +13,8 @@ typedef PointerRoute = void Function(PointerEvent event); /// A routing table for [PointerEvent] events. class PointerRouter { - final Map> _routeMap = >{}; - final Map _globalRoutes = {}; + final Map> _routeMap = >{}; + final Map _globalRoutes = {}; /// Adds a route to the routing table. /// @@ -24,10 +23,10 @@ class PointerRouter { /// /// Routes added reentrantly within [PointerRouter.route] will take effect when /// routing the next event. - void addRoute(int pointer, PointerRoute route, [Matrix4 transform]) { - final Map routes = _routeMap.putIfAbsent( + void addRoute(int pointer, PointerRoute route, [Matrix4? transform]) { + final Map routes = _routeMap.putIfAbsent( pointer, - () => {}, + () => {}, ); assert(!routes.containsKey(route)); routes[route] = transform; @@ -42,7 +41,7 @@ class PointerRouter { /// immediately. void removeRoute(int pointer, PointerRoute route) { assert(_routeMap.containsKey(pointer)); - final Map routes = _routeMap[pointer]; + final Map routes = _routeMap[pointer]!; assert(routes.containsKey(route)); routes.remove(route); if (routes.isEmpty) @@ -55,7 +54,7 @@ class PointerRouter { /// /// Routes added reentrantly within [PointerRouter.route] will take effect when /// routing the next event. - void addGlobalRoute(PointerRoute route, [Matrix4 transform]) { + void addGlobalRoute(PointerRoute route, [Matrix4? transform]) { assert(!_globalRoutes.containsKey(route)); _globalRoutes[route] = transform; } @@ -72,12 +71,12 @@ class PointerRouter { _globalRoutes.remove(route); } - void _dispatch(PointerEvent event, PointerRoute route, Matrix4 transform) { + void _dispatch(PointerEvent event, PointerRoute route, Matrix4? transform) { try { event = event.transformed(transform); route(event); } catch (exception, stack) { - InformationCollector collector; + InformationCollector? collector; assert(() { collector = () sync* { yield DiagnosticsProperty('router', this, level: DiagnosticLevel.debug); @@ -101,13 +100,13 @@ class PointerRouter { /// Routes are called in the order in which they were added to the /// PointerRouter object. void route(PointerEvent event) { - final Map routes = _routeMap[event.pointer]; - final Map copiedGlobalRoutes = Map.from(_globalRoutes); + final Map? routes = _routeMap[event.pointer]; + final Map copiedGlobalRoutes = Map.from(_globalRoutes); if (routes != null) { _dispatchEventToRoutes( event, routes, - Map.from(routes), + Map.from(routes), ); } _dispatchEventToRoutes(event, _globalRoutes, copiedGlobalRoutes); @@ -115,10 +114,10 @@ class PointerRouter { void _dispatchEventToRoutes( PointerEvent event, - Map referenceRoutes, - Map copiedRoutes, + Map referenceRoutes, + Map copiedRoutes, ) { - copiedRoutes.forEach((PointerRoute route, Matrix4 transform) { + copiedRoutes.forEach((PointerRoute route, Matrix4? transform) { if (referenceRoutes.containsKey(route)) { _dispatch(event, route, transform); } diff --git a/packages/flutter/lib/src/gestures/pointer_signal_resolver.dart b/packages/flutter/lib/src/gestures/pointer_signal_resolver.dart index ccd49ffabf8..be7b348144b 100644 --- a/packages/flutter/lib/src/gestures/pointer_signal_resolver.dart +++ b/packages/flutter/lib/src/gestures/pointer_signal_resolver.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 'package:flutter/foundation.dart'; @@ -27,15 +26,15 @@ bool _isSameEvent(PointerSignalEvent event1, PointerSignalEvent event2) { /// at the end of event dispatch. The first callback registered will be the one /// that is called. class PointerSignalResolver { - PointerSignalResolvedCallback _firstRegisteredCallback; + PointerSignalResolvedCallback? _firstRegisteredCallback; - PointerSignalEvent _currentEvent; + PointerSignalEvent? _currentEvent; /// Registers interest in handling [event]. void register(PointerSignalEvent event, PointerSignalResolvedCallback callback) { assert(event != null); assert(callback != null); - assert(_currentEvent == null || _isSameEvent(_currentEvent, event)); + assert(_currentEvent == null || _isSameEvent(_currentEvent!, event)); if (_firstRegisteredCallback != null) { return; } @@ -53,11 +52,11 @@ class PointerSignalResolver { assert(_currentEvent == null); return; } - assert(_isSameEvent(_currentEvent, event)); + assert(_isSameEvent(_currentEvent!, event)); try { - _firstRegisteredCallback(_currentEvent); + _firstRegisteredCallback!(_currentEvent!); } catch (exception, stack) { - InformationCollector collector; + InformationCollector? collector; assert(() { collector = () sync* { yield DiagnosticsProperty('Event', event, style: DiagnosticsTreeStyle.errorProperty); diff --git a/packages/flutter/lib/src/gestures/recognizer.dart b/packages/flutter/lib/src/gestures/recognizer.dart index a9721eb8dd9..229a851706e 100644 --- a/packages/flutter/lib/src/gestures/recognizer.dart +++ b/packages/flutter/lib/src/gestures/recognizer.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:async'; import 'dart:collection'; @@ -67,17 +66,17 @@ abstract class GestureRecognizer extends GestureArenaMember with DiagnosticableT /// by providing the optional [kind] argument. If [kind] is null, /// the recognizer will accept pointer events from all device kinds. /// {@endtemplate} - GestureRecognizer({ this.debugOwner, PointerDeviceKind kind }) : _kindFilter = kind; + GestureRecognizer({ this.debugOwner, PointerDeviceKind? kind }) : _kindFilter = kind; /// The recognizer's owner. /// /// This is used in the [toString] serialization to report the object for which /// this gesture recognizer was created, to aid in debugging. - final Object debugOwner; + final Object? debugOwner; /// The kind of device that's allowed to be recognized. If null, events from /// all device kinds will be tracked and recognized. - final PointerDeviceKind _kindFilter; + final PointerDeviceKind? _kindFilter; /// Holds a mapping between pointer IDs and the kind of devices they are /// coming from. @@ -141,7 +140,7 @@ abstract class GestureRecognizer extends GestureArenaMember with DiagnosticableT @protected PointerDeviceKind getKindForPointer(int pointer) { assert(_pointerToKind.containsKey(pointer)); - return _pointerToKind[pointer]; + return _pointerToKind[pointer]!; } /// Releases any resources used by the object. @@ -167,13 +166,13 @@ abstract class GestureRecognizer extends GestureArenaMember with DiagnosticableT /// callback that returns a string describing useful debugging information, /// e.g. the arguments passed to the callback. @protected - T invokeCallback(String name, RecognizerCallback callback, { String debugReport() }) { + T? invokeCallback(String name, RecognizerCallback callback, { String Function()? debugReport }) { assert(callback != null); - T result; + T? result; try { assert(() { if (debugPrintRecognizerCallbacksTrace) { - final String report = debugReport != null ? debugReport() : null; + final String? report = debugReport != null ? debugReport() : null; // The 19 in the line below is the width of the prefix used by // _debugLogDiagnostic in arena.dart. final String prefix = debugPrintGestureArenaDiagnostics ? ' ' * 19 + '❙ ' : ''; @@ -183,7 +182,7 @@ abstract class GestureRecognizer extends GestureArenaMember with DiagnosticableT }()); result = callback(); } catch (exception, stack) { - InformationCollector collector; + InformationCollector? collector; assert(() { collector = () sync* { yield StringProperty('Handler', name); @@ -222,8 +221,8 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { /// /// {@macro flutter.gestures.gestureRecognizer.kind} OneSequenceGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); final Map _entries = {}; @@ -267,7 +266,7 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { @protected @mustCallSuper void resolvePointer(int pointer, GestureDisposition disposition) { - final GestureArenaEntry entry = _entries[pointer]; + final GestureArenaEntry? entry = _entries[pointer]; if (entry != null) { entry.resolve(disposition); _entries.remove(pointer); @@ -278,7 +277,7 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { void dispose() { resolve(GestureDisposition.rejected); for (final int pointer in _trackedPointers) - GestureBinding.instance.pointerRouter.removeRoute(pointer, handleEvent); + GestureBinding.instance!.pointerRouter.removeRoute(pointer, handleEvent); _trackedPointers.clear(); assert(_entries.isEmpty); super.dispose(); @@ -294,10 +293,10 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { /// A recognizer can be assigned to a team only when it is not participating /// in the arena. For example, a common time to assign a recognizer to a team /// is shortly after creating the recognizer. - GestureArenaTeam get team => _team; - GestureArenaTeam _team; + GestureArenaTeam? get team => _team; + GestureArenaTeam? _team; /// The [team] can only be set once. - set team(GestureArenaTeam value) { + set team(GestureArenaTeam? value) { assert(value != null); assert(_entries.isEmpty); assert(_trackedPointers.isEmpty); @@ -307,8 +306,8 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { GestureArenaEntry _addPointerToArena(int pointer) { if (_team != null) - return _team.add(pointer, this); - return GestureBinding.instance.gestureArena.add(pointer, this); + return _team!.add(pointer, this); + return GestureBinding.instance!.gestureArena.add(pointer, this); } /// Causes events related to the given pointer ID to be routed to this recognizer. @@ -321,8 +320,8 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { /// /// Use [stopTrackingPointer] to remove the route added by this function. @protected - void startTrackingPointer(int pointer, [Matrix4 transform]) { - GestureBinding.instance.pointerRouter.addRoute(pointer, handleEvent, transform); + void startTrackingPointer(int pointer, [Matrix4? transform]) { + GestureBinding.instance!.pointerRouter.addRoute(pointer, handleEvent, transform); _trackedPointers.add(pointer); assert(!_entries.containsValue(pointer)); _entries[pointer] = _addPointerToArena(pointer); @@ -337,7 +336,7 @@ abstract class OneSequenceGestureRecognizer extends GestureRecognizer { @protected void stopTrackingPointer(int pointer) { if (_trackedPointers.contains(pointer)) { - GestureBinding.instance.pointerRouter.removeRoute(pointer, handleEvent); + GestureBinding.instance!.pointerRouter.removeRoute(pointer, handleEvent); _trackedPointers.remove(pointer); if (_trackedPointers.isEmpty) didStopTrackingLastPointer(pointer); @@ -391,8 +390,8 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni this.deadline, this.preAcceptSlopTolerance = kTouchSlop, this.postAcceptSlopTolerance = kTouchSlop, - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : assert( preAcceptSlopTolerance == null || preAcceptSlopTolerance >= 0, 'The preAcceptSlopTolerance must be positive or null', @@ -408,7 +407,7 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni /// /// The [didExceedDeadline] will not be called if the primary pointer is /// accepted, rejected, or all pointers are up or canceled before [deadline]. - final Duration deadline; + final Duration? deadline; /// The maximum distance in logical pixels the gesture is allowed to drift /// from the initial touch down position before the gesture is accepted. @@ -417,7 +416,7 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni /// /// Can be null to indicate that the gesture can drift for any distance. /// Defaults to 18 logical pixels. - final double preAcceptSlopTolerance; + final double? preAcceptSlopTolerance; /// The maximum distance in logical pixels the gesture is allowed to drift /// after the gesture has been accepted. @@ -427,7 +426,7 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni /// /// Can be null to indicate that the gesture can drift for any distance. /// Defaults to 18 logical pixels. - final double postAcceptSlopTolerance; + final double? postAcceptSlopTolerance; /// The current state of the recognizer. /// @@ -435,15 +434,15 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni GestureRecognizerState state = GestureRecognizerState.ready; /// The ID of the primary pointer this recognizer is tracking. - int primaryPointer; + int? primaryPointer; /// The location at which the primary pointer contacted the screen. - OffsetPair initialPosition; + OffsetPair? initialPosition; // Whether this pointer is accepted by winning the arena or as defined by // a subclass calling acceptGesture. bool _gestureAccepted = false; - Timer _timer; + Timer? _timer; @override void addAllowedPointer(PointerDownEvent event) { @@ -453,7 +452,7 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni primaryPointer = event.pointer; initialPosition = OffsetPair(local: event.localPosition, global: event.position); if (deadline != null) - _timer = Timer(deadline, () => didExceedDeadlineWithEvent(event)); + _timer = Timer(deadline!, () => didExceedDeadlineWithEvent(event)); } } @@ -464,15 +463,15 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni final bool isPreAcceptSlopPastTolerance = !_gestureAccepted && preAcceptSlopTolerance != null && - _getGlobalDistance(event) > preAcceptSlopTolerance; + _getGlobalDistance(event) > preAcceptSlopTolerance!; final bool isPostAcceptSlopPastTolerance = _gestureAccepted && postAcceptSlopTolerance != null && - _getGlobalDistance(event) > postAcceptSlopTolerance; + _getGlobalDistance(event) > postAcceptSlopTolerance!; if (event is PointerMoveEvent && (isPreAcceptSlopPastTolerance || isPostAcceptSlopPastTolerance)) { resolve(GestureDisposition.rejected); - stopTrackingPointer(primaryPointer); + stopTrackingPointer(primaryPointer!); } else { handlePrimaryPointer(event); } @@ -534,13 +533,13 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni void _stopTimer() { if (_timer != null) { - _timer.cancel(); + _timer!.cancel(); _timer = null; } } double _getGlobalDistance(PointerEvent event) { - final Offset offset = event.position - initialPosition.global; + final Offset offset = event.position - initialPosition!.global; return offset.distance; } @@ -559,8 +558,8 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni class OffsetPair { /// Creates a [OffsetPair] combining a [local] and [global] [Offset]. const OffsetPair({ - @required this.local, - @required this.global, + required this.local, + required this.global, }); /// Creates a [OffsetPair] from [PointerEvent.localPosition] and diff --git a/packages/flutter/lib/src/gestures/scale.dart b/packages/flutter/lib/src/gestures/scale.dart index 7fb645338f9..e604853eef8 100644 --- a/packages/flutter/lib/src/gestures/scale.dart +++ b/packages/flutter/lib/src/gestures/scale.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; @@ -38,7 +37,7 @@ class ScaleStartDetails { /// Creates details for [GestureScaleStartCallback]. /// /// The [focalPoint] argument must not be null. - ScaleStartDetails({ this.focalPoint = Offset.zero, Offset localFocalPoint, }) + ScaleStartDetails({ this.focalPoint = Offset.zero, Offset? localFocalPoint, }) : assert(focalPoint != null), localFocalPoint = localFocalPoint ?? focalPoint; /// The initial focal point of the pointers in contact with the screen. @@ -75,7 +74,7 @@ class ScaleUpdateDetails { /// argument must be greater than or equal to zero. ScaleUpdateDetails({ this.focalPoint = Offset.zero, - Offset localFocalPoint, + Offset? localFocalPoint, this.scale = 1.0, this.horizontalScale = 1.0, this.verticalScale = 1.0, @@ -225,37 +224,37 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { /// /// {@macro flutter.gestures.gestureRecognizer.kind} ScaleGestureRecognizer({ - Object debugOwner, - PointerDeviceKind kind, + Object? debugOwner, + PointerDeviceKind? kind, }) : super(debugOwner: debugOwner, kind: kind); /// The pointers in contact with the screen have established a focal point and /// initial scale of 1.0. - GestureScaleStartCallback onStart; + GestureScaleStartCallback? onStart; /// The pointers in contact with the screen have indicated a new focal point /// and/or scale. - GestureScaleUpdateCallback onUpdate; + GestureScaleUpdateCallback? onUpdate; /// The pointers are no longer in contact with the screen. - GestureScaleEndCallback onEnd; + GestureScaleEndCallback? onEnd; _ScaleState _state = _ScaleState.ready; - Matrix4 _lastTransform; + Matrix4? _lastTransform; - Offset _initialFocalPoint; - Offset _currentFocalPoint; - double _initialSpan; - double _currentSpan; - double _initialHorizontalSpan; - double _currentHorizontalSpan; - double _initialVerticalSpan; - double _currentVerticalSpan; - _LineBetweenPointers _initialLine; - _LineBetweenPointers _currentLine; - Map _pointerLocations; - List _pointerQueue; // A queue to sort pointers in order of entrance + late Offset _initialFocalPoint; + late Offset _currentFocalPoint; + late double _initialSpan; + late double _currentSpan; + late double _initialHorizontalSpan; + late double _currentHorizontalSpan; + late double _initialVerticalSpan; + late double _currentVerticalSpan; + _LineBetweenPointers? _initialLine; + _LineBetweenPointers? _currentLine; + late Map _pointerLocations; + late List _pointerQueue; // A queue to sort pointers in order of entrance final Map _velocityTrackers = {}; double get _scaleFactor => _initialSpan > 0.0 ? _currentSpan / _initialSpan : 1.0; @@ -268,15 +267,15 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { if (_initialLine == null || _currentLine == null) { return 0.0; } - final double fx = _initialLine.pointerStartLocation.dx; - final double fy = _initialLine.pointerStartLocation.dy; - final double sx = _initialLine.pointerEndLocation.dx; - final double sy = _initialLine.pointerEndLocation.dy; + final double fx = _initialLine!.pointerStartLocation.dx; + final double fy = _initialLine!.pointerStartLocation.dy; + final double sx = _initialLine!.pointerEndLocation.dx; + final double sy = _initialLine!.pointerEndLocation.dy; - final double nfx = _currentLine.pointerStartLocation.dx; - final double nfy = _currentLine.pointerStartLocation.dy; - final double nsx = _currentLine.pointerEndLocation.dx; - final double nsy = _currentLine.pointerEndLocation.dy; + final double nfx = _currentLine!.pointerStartLocation.dx; + final double nfy = _currentLine!.pointerStartLocation.dy; + final double nsx = _currentLine!.pointerEndLocation.dx; + final double nsy = _currentLine!.pointerEndLocation.dy; final double angle1 = math.atan2(fy - sy, fx - sx); final double angle2 = math.atan2(nfy - nsy, nfx - nsx); @@ -307,8 +306,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { bool didChangeConfiguration = false; bool shouldStartIfAccepted = false; if (event is PointerMoveEvent) { - final VelocityTracker tracker = _velocityTrackers[event.pointer]; - assert(tracker != null); + final VelocityTracker tracker = _velocityTrackers[event.pointer]!; if (!event.synthesized) tracker.addPosition(event.timeStamp, event.position); _pointerLocations[event.pointer] = event.position; @@ -341,7 +339,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { // Compute the focal point Offset focalPoint = Offset.zero; for (final int pointer in _pointerLocations.keys) - focalPoint += _pointerLocations[pointer]; + focalPoint += _pointerLocations[pointer]!; _currentFocalPoint = count > 0 ? focalPoint / count.toDouble() : Offset.zero; // Span is the average deviation from focal point. Horizontal and vertical @@ -351,9 +349,9 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { double totalHorizontalDeviation = 0.0; double totalVerticalDeviation = 0.0; for (final int pointer in _pointerLocations.keys) { - totalDeviation += (_currentFocalPoint - _pointerLocations[pointer]).distance; - totalHorizontalDeviation += (_currentFocalPoint.dx - _pointerLocations[pointer].dx).abs(); - totalVerticalDeviation += (_currentFocalPoint.dy - _pointerLocations[pointer].dy).abs(); + totalDeviation += (_currentFocalPoint - _pointerLocations[pointer]!).distance; + totalHorizontalDeviation += (_currentFocalPoint.dx - _pointerLocations[pointer]!.dx).abs(); + totalVerticalDeviation += (_currentFocalPoint.dy - _pointerLocations[pointer]!.dy).abs(); } _currentSpan = count > 0 ? totalDeviation / count : 0.0; _currentHorizontalSpan = count > 0 ? totalHorizontalDeviation / count : 0.0; @@ -369,22 +367,22 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { if (count < 2) { _initialLine = _currentLine; } else if (_initialLine != null && - _initialLine.pointerStartId == _pointerQueue[0] && - _initialLine.pointerEndId == _pointerQueue[1]) { + _initialLine!.pointerStartId == _pointerQueue[0] && + _initialLine!.pointerEndId == _pointerQueue[1]) { /// Rotation updated, set the [_currentLine] _currentLine = _LineBetweenPointers( pointerStartId: _pointerQueue[0], - pointerStartLocation: _pointerLocations[_pointerQueue[0]], + pointerStartLocation: _pointerLocations[_pointerQueue[0]]!, pointerEndId: _pointerQueue[1], - pointerEndLocation: _pointerLocations[_pointerQueue[1]], + pointerEndLocation: _pointerLocations[_pointerQueue[1]]!, ); } else { /// A new rotation process is on the way, set the [_initialLine] _initialLine = _LineBetweenPointers( pointerStartId: _pointerQueue[0], - pointerStartLocation: _pointerLocations[_pointerQueue[0]], + pointerStartLocation: _pointerLocations[_pointerQueue[0]]!, pointerEndId: _pointerQueue[1], - pointerEndLocation: _pointerLocations[_pointerQueue[1]], + pointerEndLocation: _pointerLocations[_pointerQueue[1]]!, ); _currentLine = null; } @@ -398,17 +396,16 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { _initialVerticalSpan = _currentVerticalSpan; if (_state == _ScaleState.started) { if (onEnd != null) { - final VelocityTracker tracker = _velocityTrackers[pointer]; - assert(tracker != null); + final VelocityTracker tracker = _velocityTrackers[pointer]!; Velocity velocity = tracker.getVelocity(); if (_isFlingGesture(velocity)) { final Offset pixelsPerSecond = velocity.pixelsPerSecond; if (pixelsPerSecond.distanceSquared > kMaxFlingVelocity * kMaxFlingVelocity) velocity = Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * kMaxFlingVelocity); - invokeCallback('onEnd', () => onEnd(ScaleEndDetails(velocity: velocity))); + invokeCallback('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity))); } else { - invokeCallback('onEnd', () => onEnd(ScaleEndDetails(velocity: Velocity.zero))); + invokeCallback('onEnd', () => onEnd!(ScaleEndDetails(velocity: Velocity.zero))); } } _state = _ScaleState.accepted; @@ -437,7 +434,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { if (_state == _ScaleState.started && onUpdate != null) invokeCallback('onUpdate', () { - onUpdate(ScaleUpdateDetails( + onUpdate!(ScaleUpdateDetails( scale: _scaleFactor, horizontalScale: _horizontalScaleFactor, verticalScale: _verticalScaleFactor, @@ -452,7 +449,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { assert(_state == _ScaleState.started); if (onStart != null) invokeCallback('onStart', () { - onStart(ScaleStartDetails( + onStart!(ScaleStartDetails( focalPoint: _currentFocalPoint, localFocalPoint: PointerEvent.transformPosition(_lastTransform, _currentFocalPoint), )); diff --git a/packages/flutter/lib/src/gestures/tap.dart b/packages/flutter/lib/src/gestures/tap.dart index a0e99ea7730..8433db57cc1 100644 --- a/packages/flutter/lib/src/gestures/tap.dart +++ b/packages/flutter/lib/src/gestures/tap.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 'package:vector_math/vector_math_64.dart' show Matrix4; import 'package:flutter/foundation.dart'; @@ -24,7 +23,7 @@ class TapDownDetails { /// The [globalPosition] argument must not be null. TapDownDetails({ this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, this.kind, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -33,7 +32,7 @@ class TapDownDetails { final Offset globalPosition; /// The kind of the device that initiated the event. - final PointerDeviceKind kind; + final PointerDeviceKind? kind; /// The local position at which the pointer contacted the screen. final Offset localPosition; @@ -61,7 +60,7 @@ class TapUpDetails { /// The [globalPosition] argument must not be null. TapUpDetails({ this.globalPosition = Offset.zero, - Offset localPosition, + Offset? localPosition, }) : assert(globalPosition != null), localPosition = localPosition ?? globalPosition; @@ -133,14 +132,14 @@ typedef GestureTapCancelCallback = void Function(); /// any buttons. abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer { /// Creates a tap gesture recognizer. - BaseTapGestureRecognizer({ Object debugOwner }) + BaseTapGestureRecognizer({ Object? debugOwner }) : super(deadline: kPressTimeout , debugOwner: debugOwner); bool _sentTapDown = false; bool _wonArenaForPrimaryPointer = false; - PointerDownEvent _down; - PointerUpEvent _up; + PointerDownEvent? _down; + PointerUpEvent? _up; /// A pointer has contacted the screen, which might be the start of a tap. /// @@ -153,7 +152,7 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer /// If this recognizer doesn't win the arena, [handleTapCancel] is called next. /// Otherwise, [handleTapUp] is called next. @protected - void handleTapDown({ PointerDownEvent down }); + void handleTapDown({ required PointerDownEvent down }); /// A pointer has stopped contacting the screen, which is recognized as a tap. /// @@ -166,7 +165,7 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer /// If this recognizer doesn't win the arena, [handleTapCancel] is called /// instead. @protected - void handleTapUp({ PointerDownEvent down, PointerUpEvent up }); + void handleTapUp({ required PointerDownEvent down, required PointerUpEvent up }); /// A pointer that previously triggered [handleTapDown] will not end up /// causing a tap. @@ -182,7 +181,7 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer /// /// If this recognizer wins the arena, [handleTapUp] is called instead. @protected - void handleTapCancel({ PointerDownEvent down, PointerCancelEvent cancel, String reason }); + void handleTapCancel({ required PointerDownEvent down, PointerCancelEvent? cancel, required String reason }); @override void addAllowedPointer(PointerDownEvent event) { @@ -205,7 +204,7 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer @override @protected - void startTrackingPointer(int pointer, [Matrix4 transform]) { + void startTrackingPointer(int pointer, [Matrix4? transform]) { // The recognizer should never track any pointers when `_down` is null, // because calling `_checkDown` in this state will throw exception. assert(_down != null); @@ -223,9 +222,9 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer _checkCancel(event, ''); } _reset(); - } else if (event.buttons != _down.buttons) { + } else if (event.buttons != _down!.buttons) { resolve(GestureDisposition.rejected); - stopTrackingPointer(primaryPointer); + stopTrackingPointer(primaryPointer!); } } @@ -273,7 +272,7 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer if (_sentTapDown) { return; } - handleTapDown(down: _down); + handleTapDown(down: _down!); _sentTapDown = true; } @@ -281,12 +280,12 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer if (!_wonArenaForPrimaryPointer || _up == null) { return; } - handleTapUp(down: _down, up: _up); + handleTapUp(down: _down!, up: _up!); _reset(); } - void _checkCancel(PointerCancelEvent event, String note) { - handleTapCancel(down: _down, cancel: event, reason: note); + void _checkCancel(PointerCancelEvent? event, String note) { + handleTapCancel(down: _down!, cancel: event, reason: note); } void _reset() { @@ -332,7 +331,7 @@ abstract class BaseTapGestureRecognizer extends PrimaryPointerGestureRecognizer /// * [MultiTapGestureRecognizer] class TapGestureRecognizer extends BaseTapGestureRecognizer { /// Creates a tap gesture recognizer. - TapGestureRecognizer({ Object debugOwner }) : super(debugOwner: debugOwner); + TapGestureRecognizer({ Object? debugOwner }) : super(debugOwner: debugOwner); /// A pointer has contacted the screen at a particular location with a primary /// button, which might be the start of a tap. @@ -349,7 +348,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [onSecondaryTapDown], a similar callback but for a secondary button. /// * [TapDownDetails], which is passed as an argument to this callback. /// * [GestureDetector.onTapDown], which exposes this callback. - GestureTapDownCallback onTapDown; + GestureTapDownCallback? onTapDown; /// A pointer has stopped contacting the screen at a particular location, /// which is recognized as a tap of a primary button. @@ -365,7 +364,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [onSecondaryTapUp], a similar callback but for a secondary button. /// * [TapUpDetails], which is passed as an argument to this callback. /// * [GestureDetector.onTapUp], which exposes this callback. - GestureTapUpCallback onTapUp; + GestureTapUpCallback? onTapUp; /// A pointer has stopped contacting the screen, which is recognized as a tap /// of a primary button. @@ -380,7 +379,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [kPrimaryButton], the button this callback responds to. /// * [onTapUp], which has the same timing but with details. /// * [GestureDetector.onTap], which exposes this callback. - GestureTapCallback onTap; + GestureTapCallback? onTap; /// A pointer that previously triggered [onTapDown] will not end up causing /// a tap. @@ -396,7 +395,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [kPrimaryButton], the button this callback responds to. /// * [onSecondaryTapCancel], a similar callback but for a secondary button. /// * [GestureDetector.onTapCancel], which exposes this callback. - GestureTapCancelCallback onTapCancel; + GestureTapCancelCallback? onTapCancel; /// A pointer has stopped contacting the screen, which is recognized as a tap /// of a secondary button. @@ -412,7 +411,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [kSecondaryButton], the button this callback responds to. /// * [onSecondaryTapUp], which has the same timing but with details. /// * [GestureDetector.onSecondaryTap], which exposes this callback. - GestureTapCallback onSecondaryTap; + GestureTapCallback? onSecondaryTap; /// A pointer has contacted the screen at a particular location with a /// secondary button, which might be the start of a secondary tap. @@ -429,7 +428,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [onTapDown], a similar callback but for a primary button. /// * [TapDownDetails], which is passed as an argument to this callback. /// * [GestureDetector.onSecondaryTapDown], which exposes this callback. - GestureTapDownCallback onSecondaryTapDown; + GestureTapDownCallback? onSecondaryTapDown; /// A pointer has stopped contacting the screen at a particular location, /// which is recognized as a tap of a secondary button. @@ -448,7 +447,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [onTapUp], a similar callback but for a primary button. /// * [TapUpDetails], which is passed as an argument to this callback. /// * [GestureDetector.onSecondaryTapUp], which exposes this callback. - GestureTapUpCallback onSecondaryTapUp; + GestureTapUpCallback? onSecondaryTapUp; /// A pointer that previously triggered [onSecondaryTapDown] will not end up /// causing a tap. @@ -463,7 +462,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { /// * [kSecondaryButton], the button this callback responds to. /// * [onTapCancel], a similar callback but for a primary button. /// * [GestureDetector.onTapCancel], which exposes this callback. - GestureTapCancelCallback onSecondaryTapCancel; + GestureTapCancelCallback? onSecondaryTapCancel; @override bool isPointerAllowed(PointerDownEvent event) { @@ -490,7 +489,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { @protected @override - void handleTapDown({PointerDownEvent down}) { + void handleTapDown({required PointerDownEvent down}) { final TapDownDetails details = TapDownDetails( globalPosition: down.position, localPosition: down.localPosition, @@ -499,11 +498,11 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { switch (down.buttons) { case kPrimaryButton: if (onTapDown != null) - invokeCallback('onTapDown', () => onTapDown(details)); + invokeCallback('onTapDown', () => onTapDown!(details)); break; case kSecondaryButton: if (onSecondaryTapDown != null) - invokeCallback('onSecondaryTapDown', () => onSecondaryTapDown(details)); + invokeCallback('onSecondaryTapDown', () => onSecondaryTapDown!(details)); break; default: } @@ -511,7 +510,7 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { @protected @override - void handleTapUp({PointerDownEvent down, PointerUpEvent up}) { + void handleTapUp({ required PointerDownEvent down, required PointerUpEvent up}) { final TapUpDetails details = TapUpDetails( globalPosition: up.position, localPosition: up.localPosition, @@ -519,15 +518,15 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { switch (down.buttons) { case kPrimaryButton: if (onTapUp != null) - invokeCallback('onTapUp', () => onTapUp(details)); + invokeCallback('onTapUp', () => onTapUp!(details)); if (onTap != null) - invokeCallback('onTap', onTap); + invokeCallback('onTap', onTap!); break; case kSecondaryButton: if (onSecondaryTapUp != null) - invokeCallback('onSecondaryTapUp', () => onSecondaryTapUp(details)); + invokeCallback('onSecondaryTapUp', () => onSecondaryTapUp!(details)); if (onSecondaryTap != null) - invokeCallback('onSecondaryTap', () => onSecondaryTap()); + invokeCallback('onSecondaryTap', () => onSecondaryTap!()); break; default: } @@ -535,16 +534,16 @@ class TapGestureRecognizer extends BaseTapGestureRecognizer { @protected @override - void handleTapCancel({PointerDownEvent down, PointerCancelEvent cancel, String reason}) { + void handleTapCancel({ required PointerDownEvent down, PointerCancelEvent? cancel, required String reason }) { final String note = reason == '' ? reason : '$reason '; switch (down.buttons) { case kPrimaryButton: if (onTapCancel != null) - invokeCallback('${note}onTapCancel', onTapCancel); + invokeCallback('${note}onTapCancel', onTapCancel!); break; case kSecondaryButton: if (onSecondaryTapCancel != null) - invokeCallback('${note}onSecondaryTapCancel', onSecondaryTapCancel); + invokeCallback('${note}onSecondaryTapCancel', onSecondaryTapCancel!); break; default: } diff --git a/packages/flutter/lib/src/gestures/team.dart b/packages/flutter/lib/src/gestures/team.dart index b5eceaed3d8..724617cbe4b 100644 --- a/packages/flutter/lib/src/gestures/team.dart +++ b/packages/flutter/lib/src/gestures/team.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 'arena.dart'; import 'binding.dart'; @@ -27,8 +26,8 @@ class _CombiningGestureArenaMember extends GestureArenaMember { final int _pointer; bool _resolved = false; - GestureArenaMember _winner; - GestureArenaEntry _entry; + GestureArenaMember? _winner; + GestureArenaEntry? _entry; @override void acceptGesture(int pointer) { @@ -40,7 +39,7 @@ class _CombiningGestureArenaMember extends GestureArenaMember { if (member != _winner) member.rejectGesture(pointer); } - _winner.acceptGesture(pointer); + _winner!.acceptGesture(pointer); } @override @@ -54,7 +53,7 @@ class _CombiningGestureArenaMember extends GestureArenaMember { void _close() { assert(!_resolved); _resolved = true; - final _CombiningGestureArenaMember combiner = _owner._combiners.remove(_pointer); + final _CombiningGestureArenaMember? combiner = _owner._combiners.remove(_pointer); assert(combiner == this); } @@ -62,7 +61,7 @@ class _CombiningGestureArenaMember extends GestureArenaMember { assert(!_resolved); assert(_pointer == pointer); _members.add(member); - _entry ??= GestureBinding.instance.gestureArena.add(pointer, this); + _entry ??= GestureBinding.instance!.gestureArena.add(pointer, this); return _CombiningGestureArenaEntry(this, member); } @@ -73,11 +72,11 @@ class _CombiningGestureArenaMember extends GestureArenaMember { _members.remove(member); member.rejectGesture(_pointer); if (_members.isEmpty) - _entry.resolve(disposition); + _entry!.resolve(disposition); } else { assert(disposition == GestureDisposition.accepted); _winner ??= _owner.captain ?? member; - _entry.resolve(disposition); + _entry!.resolve(disposition); } } } @@ -131,7 +130,7 @@ class GestureArenaTeam { /// If not null, when any one of the [GestureArenaTeam] members claims victory /// the captain accepts the gesture. /// If null, the member that claims a victory accepts the gesture. - GestureArenaMember captain; + GestureArenaMember? captain; /// Adds a new member to the arena on behalf of this team. /// diff --git a/packages/flutter/lib/src/gestures/velocity_tracker.dart b/packages/flutter/lib/src/gestures/velocity_tracker.dart index 89b728ba605..4de7134d20a 100644 --- a/packages/flutter/lib/src/gestures/velocity_tracker.dart +++ b/packages/flutter/lib/src/gestures/velocity_tracker.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:ui' show Offset; @@ -19,7 +18,7 @@ class Velocity { /// /// The [pixelsPerSecond] argument must not be null. const Velocity({ - @required this.pixelsPerSecond, + required this.pixelsPerSecond, }) : assert(pixelsPerSecond != null); /// A velocity that isn't moving at all. @@ -95,10 +94,10 @@ class VelocityEstimate { /// /// [pixelsPerSecond], [confidence], [duration], and [offset] must not be null. const VelocityEstimate({ - @required this.pixelsPerSecond, - @required this.confidence, - @required this.duration, - @required this.offset, + required this.pixelsPerSecond, + required this.confidence, + required this.duration, + required this.offset, }) : assert(pixelsPerSecond != null), assert(confidence != null), assert(duration != null), @@ -154,7 +153,7 @@ class VelocityTracker { static const int _minSampleSize = 3; // Circular buffer; current sample at _index. - final List<_PointAtTime> _samples = List<_PointAtTime>(_historySize); + final List<_PointAtTime?> _samples = List<_PointAtTime?>.filled(_historySize, null, growable: false); int _index = 0; /// Adds a position as the given time to the tracker. @@ -171,7 +170,7 @@ class VelocityTracker { /// Information is added using [addPosition]. /// /// Returns null if there is no data on which to base an estimate. - VelocityEstimate getVelocityEstimate() { + VelocityEstimate? getVelocityEstimate() { final List x = []; final List y = []; final List w = []; @@ -179,7 +178,7 @@ class VelocityTracker { int sampleCount = 0; int index = _index; - final _PointAtTime newestSample = _samples[index]; + final _PointAtTime? newestSample = _samples[index]; if (newestSample == null) return null; @@ -189,7 +188,7 @@ class VelocityTracker { // Starting with the most recent PointAtTime sample, iterate backwards while // the samples represent continuous motion. do { - final _PointAtTime sample = _samples[index]; + final _PointAtTime? sample = _samples[index]; if (sample == null) break; @@ -212,10 +211,10 @@ class VelocityTracker { if (sampleCount >= _minSampleSize) { final LeastSquaresSolver xSolver = LeastSquaresSolver(time, x, w); - final PolynomialFit xFit = xSolver.solve(2); + final PolynomialFit? xFit = xSolver.solve(2); if (xFit != null) { final LeastSquaresSolver ySolver = LeastSquaresSolver(time, y, w); - final PolynomialFit yFit = ySolver.solve(2); + final PolynomialFit? yFit = ySolver.solve(2); if (yFit != null) { return VelocityEstimate( // convert from pixels/ms to pixels/s pixelsPerSecond: Offset(xFit.coefficients[1] * 1000, yFit.coefficients[1] * 1000), @@ -245,7 +244,7 @@ class VelocityTracker { /// Returns [Velocity.zero] if there is no data from which to compute an /// estimate or if the estimated velocity is zero. Velocity getVelocity() { - final VelocityEstimate estimate = getVelocityEstimate(); + final VelocityEstimate? estimate = getVelocityEstimate(); if (estimate == null || estimate.pixelsPerSecond == Offset.zero) return Velocity.zero; return Velocity(pixelsPerSecond: estimate.pixelsPerSecond);