_InteractiveViewerState code cleanup (#153645)

This pull request makes the interactive viewer's `TransformationController` a non-nullable value, initialized at its declaration site. (related: #153644)

It also renames a few private methods to follow the [style guide](https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md#naming-rules-for-typedefs-and-function-variables).
This commit is contained in:
Nate Wilson
2024-08-20 18:52:08 -06:00
committed by GitHub
parent d0d23300e6
commit 1cbf1a4cbf

View File

@@ -512,7 +512,8 @@ class InteractiveViewer extends StatefulWidget {
}
class _InteractiveViewerState extends State<InteractiveViewer> with TickerProviderStateMixin {
TransformationController? _transformationController;
late TransformationController _transformer = widget.transformationController
?? TransformationController();
final GlobalKey _childKey = GlobalKey();
final GlobalKey _parentKey = GlobalKey();
@@ -675,7 +676,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
// Don't allow a scale that results in an overall scale beyond min/max
// scale.
final double currentScale = _transformationController!.value.getMaxScaleOnAxis();
final double currentScale = _transformer.value.getMaxScaleOnAxis();
final double totalScale = math.max(
currentScale * scale,
// Ensure that the scale cannot make the child so big that it can't fit
@@ -699,11 +700,8 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
if (rotation == 0) {
return matrix.clone();
}
final Offset focalPointScene = _transformationController!.toScene(
focalPoint,
);
return matrix
.clone()
final Offset focalPointScene = _transformer.toScene(focalPoint);
return matrix.clone()
..translate(focalPointScene.dx, focalPointScene.dy)
..rotateZ(-rotation)
..translate(-focalPointScene.dx, -focalPointScene.dy);
@@ -742,31 +740,29 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
if (_controller.isAnimating) {
_controller.stop();
_controller.reset();
_animation?.removeListener(_onAnimate);
_animation?.removeListener(_handleInertiaAnimation);
_animation = null;
}
if (_scaleController.isAnimating) {
_scaleController.stop();
_scaleController.reset();
_scaleAnimation?.removeListener(_onScaleAnimate);
_scaleAnimation?.removeListener(_handleScaleAnimation);
_scaleAnimation = null;
}
_gestureType = null;
_currentAxis = null;
_scaleStart = _transformationController!.value.getMaxScaleOnAxis();
_referenceFocalPoint = _transformationController!.toScene(
details.localFocalPoint,
);
_scaleStart = _transformer.value.getMaxScaleOnAxis();
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
_rotationStart = _currentRotation;
}
// Handle an update to an ongoing gesture. All of pan, scale, and rotate are
// handled with GestureDetector's scale gesture.
void _onScaleUpdate(ScaleUpdateDetails details) {
final double scale = _transformationController!.value.getMaxScaleOnAxis();
final double scale = _transformer.value.getMaxScaleOnAxis();
_scaleAnimationFocalPoint = details.localFocalPoint;
final Offset focalPointScene = _transformationController!.toScene(
final Offset focalPointScene = _transformer.toScene(
details.localFocalPoint,
);
@@ -792,20 +788,17 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
// previous call to _onScaleUpdate.
final double desiredScale = _scaleStart! * details.scale;
final double scaleChange = desiredScale / scale;
_transformationController!.value = _matrixScale(
_transformationController!.value,
scaleChange,
);
_transformer.value = _matrixScale(_transformer.value, scaleChange);
// While scaling, translate such that the user's two fingers stay on
// the same places in the scene. That means that the focal point of
// the scale should be on the same place in the scene before and after
// the scale.
final Offset focalPointSceneScaled = _transformationController!.toScene(
final Offset focalPointSceneScaled = _transformer.toScene(
details.localFocalPoint,
);
_transformationController!.value = _matrixTranslate(
_transformationController!.value,
_transformer.value = _matrixTranslate(
_transformer.value,
focalPointSceneScaled - _referenceFocalPoint!,
);
@@ -814,7 +807,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
// the translate came in contact with a boundary. In that case, update
// _referenceFocalPoint so subsequent updates happen in relation to
// the new effective focal point.
final Offset focalPointSceneCheck = _transformationController!.toScene(
final Offset focalPointSceneCheck = _transformer.toScene(
details.localFocalPoint,
);
if (_round(_referenceFocalPoint!) != _round(focalPointSceneCheck)) {
@@ -827,8 +820,8 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
return;
}
final double desiredRotation = _rotationStart! + details.rotation;
_transformationController!.value = _matrixRotate(
_transformationController!.value,
_transformer.value = _matrixRotate(
_transformer.value,
_currentRotation - desiredRotation,
details.localFocalPoint,
);
@@ -847,13 +840,11 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
// Translate so that the same point in the scene is underneath the
// focal point before and after the movement.
final Offset translationChange = focalPointScene - _referenceFocalPoint!;
_transformationController!.value = _matrixTranslate(
_transformationController!.value,
_transformer.value = _matrixTranslate(
_transformer.value,
translationChange,
);
_referenceFocalPoint = _transformationController!.toScene(
details.localFocalPoint,
);
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
}
widget.onInteractionUpdate?.call(details);
}
@@ -866,8 +857,8 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
_rotationStart = null;
_referenceFocalPoint = null;
_animation?.removeListener(_onAnimate);
_scaleAnimation?.removeListener(_onScaleAnimate);
_animation?.removeListener(_handleInertiaAnimation);
_scaleAnimation?.removeListener(_handleScaleAnimation);
_controller.reset();
_scaleController.reset();
@@ -882,7 +873,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
_currentAxis = null;
return;
}
final Vector3 translationVector = _transformationController!.value.getTranslation();
final Vector3 translationVector = _transformer.value.getTranslation();
final Offset translation = Offset(translationVector.x, translationVector.y);
final FrictionSimulation frictionSimulationX = FrictionSimulation(
widget.interactionEndFrictionCoefficient,
@@ -906,14 +897,14 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
curve: Curves.decelerate,
));
_controller.duration = Duration(milliseconds: (tFinal * 1000).round());
_animation!.addListener(_onAnimate);
_animation!.addListener(_handleInertiaAnimation);
_controller.forward();
case _GestureType.scale:
if (details.scaleVelocity.abs() < 0.1) {
_currentAxis = null;
return;
}
final double scale = _transformationController!.value.getMaxScaleOnAxis();
final double scale = _transformer.value.getMaxScaleOnAxis();
final FrictionSimulation frictionSimulation = FrictionSimulation(
widget.interactionEndFrictionCoefficient * widget.scaleFactor,
scale,
@@ -928,7 +919,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
curve: Curves.decelerate
));
_scaleController.duration = Duration(milliseconds: (tFinal * 1000).round());
_scaleAnimation!.addListener(_onScaleAnimate);
_scaleAnimation!.addListener(_handleScaleAnimation);
_scaleController.forward();
case _GestureType.rotate || null:
break;
@@ -937,50 +928,46 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
// Handle mousewheel and web trackpad scroll events.
void _receivedPointerSignal(PointerSignalEvent event) {
final Offset local = event.localPosition;
final Offset global = event.position;
final double scaleChange;
if (event is PointerScrollEvent) {
if (event.kind == PointerDeviceKind.trackpad && !widget.trackpadScrollCausesScale) {
// Trackpad scroll, so treat it as a pan.
widget.onInteractionStart?.call(
ScaleStartDetails(
focalPoint: event.position,
localFocalPoint: event.localPosition,
),
ScaleStartDetails(focalPoint: global, localFocalPoint: local),
);
final Offset localDelta = PointerEvent.transformDeltaViaPositions(
untransformedEndPosition: event.position + event.scrollDelta,
untransformedEndPosition: global + event.scrollDelta,
untransformedDelta: event.scrollDelta,
transform: event.transform,
);
if (!_gestureIsSupported(_GestureType.pan)) {
widget.onInteractionUpdate?.call(ScaleUpdateDetails(
focalPoint: event.position - event.scrollDelta,
localFocalPoint: event.localPosition - event.scrollDelta,
focalPoint: global - event.scrollDelta,
localFocalPoint: local - event.scrollDelta,
focalPointDelta: -localDelta,
));
widget.onInteractionEnd?.call(ScaleEndDetails());
return;
}
final Offset focalPointScene = _transformationController!.toScene(
event.localPosition,
final Offset focalPointScene = _transformer.toScene(local);
final Offset newFocalPointScene = _transformer.toScene(
local - localDelta,
);
final Offset newFocalPointScene = _transformationController!.toScene(
event.localPosition - localDelta,
);
_transformationController!.value = _matrixTranslate(
_transformationController!.value,
newFocalPointScene - focalPointScene
_transformer.value = _matrixTranslate(
_transformer.value,
newFocalPointScene - focalPointScene,
);
widget.onInteractionUpdate?.call(ScaleUpdateDetails(
focalPoint: event.position - event.scrollDelta,
localFocalPoint: event.localPosition - localDelta,
focalPointDelta: -localDelta
focalPoint: global - event.scrollDelta,
localFocalPoint: local - localDelta,
focalPointDelta: -localDelta,
));
widget.onInteractionEnd?.call(ScaleEndDetails());
return;
@@ -998,107 +985,84 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
return;
}
widget.onInteractionStart?.call(
ScaleStartDetails(
focalPoint: event.position,
localFocalPoint: event.localPosition,
),
ScaleStartDetails(focalPoint: global, localFocalPoint: local),
);
if (!_gestureIsSupported(_GestureType.scale)) {
widget.onInteractionUpdate?.call(ScaleUpdateDetails(
focalPoint: event.position,
localFocalPoint: event.localPosition,
focalPoint: global,
localFocalPoint: local,
scale: scaleChange,
));
widget.onInteractionEnd?.call(ScaleEndDetails());
return;
}
final Offset focalPointScene = _transformationController!.toScene(
event.localPosition,
);
_transformationController!.value = _matrixScale(
_transformationController!.value,
scaleChange,
);
final Offset focalPointScene = _transformer.toScene(local);
_transformer.value = _matrixScale(_transformer.value, scaleChange);
// After scaling, translate such that the event's position is at the
// same scene point before and after the scale.
final Offset focalPointSceneScaled = _transformationController!.toScene(
event.localPosition,
);
_transformationController!.value = _matrixTranslate(
_transformationController!.value,
final Offset focalPointSceneScaled = _transformer.toScene(local);
_transformer.value = _matrixTranslate(
_transformer.value,
focalPointSceneScaled - focalPointScene,
);
widget.onInteractionUpdate?.call(ScaleUpdateDetails(
focalPoint: event.position,
localFocalPoint: event.localPosition,
focalPoint: global,
localFocalPoint: local,
scale: scaleChange,
));
widget.onInteractionEnd?.call(ScaleEndDetails());
}
// Handle inertia drag animation.
void _onAnimate() {
void _handleInertiaAnimation() {
if (!_controller.isAnimating) {
_currentAxis = null;
_animation?.removeListener(_onAnimate);
_animation?.removeListener(_handleInertiaAnimation);
_animation = null;
_controller.reset();
return;
}
// Translate such that the resulting translation is _animation.value.
final Vector3 translationVector = _transformationController!.value.getTranslation();
final Vector3 translationVector = _transformer.value.getTranslation();
final Offset translation = Offset(translationVector.x, translationVector.y);
final Offset translationScene = _transformationController!.toScene(
translation,
);
final Offset animationScene = _transformationController!.toScene(
_animation!.value,
);
final Offset translationChangeScene = animationScene - translationScene;
_transformationController!.value = _matrixTranslate(
_transformationController!.value,
translationChangeScene,
_transformer.value = _matrixTranslate(
_transformer.value,
_transformer.toScene(_animation!.value) - _transformer.toScene(translation),
);
}
// Handle inertia scale animation.
void _onScaleAnimate() {
void _handleScaleAnimation() {
if (!_scaleController.isAnimating) {
_currentAxis = null;
_scaleAnimation?.removeListener(_onScaleAnimate);
_scaleAnimation?.removeListener(_handleScaleAnimation);
_scaleAnimation = null;
_scaleController.reset();
return;
}
final double desiredScale = _scaleAnimation!.value;
final double scaleChange = desiredScale / _transformationController!.value.getMaxScaleOnAxis();
final Offset referenceFocalPoint = _transformationController!.toScene(
final double scaleChange = desiredScale / _transformer.value.getMaxScaleOnAxis();
final Offset referenceFocalPoint = _transformer.toScene(
_scaleAnimationFocalPoint,
);
_transformationController!.value = _matrixScale(
_transformationController!.value,
scaleChange,
);
_transformer.value = _matrixScale(_transformer.value, scaleChange);
// While scaling, translate such that the user's two fingers stay on
// the same places in the scene. That means that the focal point of
// the scale should be on the same place in the scene before and after
// the scale.
final Offset focalPointSceneScaled = _transformationController!.toScene(
final Offset focalPointSceneScaled = _transformer.toScene(
_scaleAnimationFocalPoint,
);
_transformationController!.value = _matrixTranslate(
_transformationController!.value,
_transformer.value = _matrixTranslate(
_transformer.value,
focalPointSceneScaled - referenceFocalPoint,
);
}
void _onTransformationControllerChange() {
void _handleTransformation() {
// A change to the TransformationController's value is a change to the
// state.
setState(() {});
@@ -1107,50 +1071,35 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this);
_scaleController = AnimationController(vsync: this);
_transformationController = widget.transformationController
?? TransformationController();
_transformationController!.addListener(_onTransformationControllerChange);
_controller = AnimationController(
vsync: this,
);
_scaleController = AnimationController(
vsync: this
);
_transformer.addListener(_handleTransformation);
}
@override
void didUpdateWidget(InteractiveViewer oldWidget) {
super.didUpdateWidget(oldWidget);
// Handle all cases of needing to dispose and initialize
// transformationControllers.
if (oldWidget.transformationController == null) {
if (widget.transformationController != null) {
_transformationController!.removeListener(_onTransformationControllerChange);
_transformationController!.dispose();
_transformationController = widget.transformationController;
_transformationController!.addListener(_onTransformationControllerChange);
}
} else {
if (widget.transformationController == null) {
_transformationController!.removeListener(_onTransformationControllerChange);
_transformationController = TransformationController();
_transformationController!.addListener(_onTransformationControllerChange);
} else if (widget.transformationController != oldWidget.transformationController) {
_transformationController!.removeListener(_onTransformationControllerChange);
_transformationController = widget.transformationController;
_transformationController!.addListener(_onTransformationControllerChange);
}
final TransformationController? newController = widget.transformationController;
if (newController == oldWidget.transformationController) {
return;
}
_transformer.removeListener(_handleTransformation);
if (oldWidget.transformationController == null) {
_transformer.dispose();
}
_transformer = newController ?? TransformationController();
_transformer.addListener(_handleTransformation);
}
@override
void dispose() {
_controller.dispose();
_scaleController.dispose();
_transformationController!.removeListener(_onTransformationControllerChange);
_transformer.removeListener(_handleTransformation);
if (widget.transformationController == null) {
_transformationController!.dispose();
_transformer.dispose();
}
super.dispose();
}
@@ -1163,7 +1112,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
childKey: _childKey,
clipBehavior: widget.clipBehavior,
constrained: widget.constrained,
matrix: _transformationController!.value,
matrix: _transformer.value,
alignment: widget.alignment,
child: widget.child!,
);
@@ -1174,7 +1123,7 @@ class _InteractiveViewerState extends State<InteractiveViewer> with TickerProvid
assert(!widget.constrained);
child = LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final Matrix4 matrix = _transformationController!.value;
final Matrix4 matrix = _transformer.value;
return _InteractiveViewerBuilt(
childKey: _childKey,
clipBehavior: widget.clipBehavior,