Skip to content

Commit

Permalink
ref: Refactoring if chains into switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
nate-thegrate authored and adil192 committed Jun 9, 2024
1 parent 861a33c commit ca99e4f
Showing 1 changed file with 58 additions and 54 deletions.
112 changes: 58 additions & 54 deletions lib/components/canvas/interactive_canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -876,60 +876,64 @@ class _InteractiveCanvasViewerState extends State<InteractiveCanvasViewer>
return;
}

if (_gestureType == _GestureType.pan) {
if (details.velocity.pixelsPerSecond.distance < kMinFlingVelocity) {
_currentAxis = null;
return;
}
final Vector3 translationVector =
_transformationController!.value.getTranslation();
final Offset translation =
Offset(translationVector.x, translationVector.y);
final FrictionSimulation frictionSimulationX = FrictionSimulation(
widget.interactionEndFrictionCoefficient,
translation.dx,
details.velocity.pixelsPerSecond.dx,
);
final FrictionSimulation frictionSimulationY = FrictionSimulation(
widget.interactionEndFrictionCoefficient,
translation.dy,
details.velocity.pixelsPerSecond.dy,
);
final double tFinal = _getFinalTime(
details.velocity.pixelsPerSecond.distance,
widget.interactionEndFrictionCoefficient,
);
_animation = Tween<Offset>(
begin: translation,
end: Offset(frictionSimulationX.finalX, frictionSimulationY.finalX),
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.decelerate,
));
_controller.duration = Duration(milliseconds: (tFinal * 1000).round());
_animation!.addListener(_onAnimate);
_controller.forward();
} else if (_gestureType == _GestureType.scale) {
if (details.scaleVelocity.abs() < 0.1) {
_currentAxis = null;
return;
}
final double scale = _transformationController!.value.getMaxScaleOnAxis();
final FrictionSimulation frictionSimulation = FrictionSimulation(
widget.interactionEndFrictionCoefficient * widget.scaleFactor,
scale,
details.scaleVelocity / 10);
final double tFinal = _getFinalTime(
details.scaleVelocity.abs(), widget.interactionEndFrictionCoefficient,
effectivelyMotionless: 0.1);
_scaleAnimation =
Tween<double>(begin: scale, end: frictionSimulation.x(tFinal))
.animate(CurvedAnimation(
parent: _scaleController, curve: Curves.decelerate));
_scaleController.duration =
Duration(milliseconds: (tFinal * 1000).round());
_scaleAnimation!.addListener(_onScaleAnimate);
_scaleController.forward();
switch (_gestureType) {
case _GestureType.pan:
if (details.velocity.pixelsPerSecond.distance < kMinFlingVelocity) {
_currentAxis = null;
return;
}
final Vector3 translationVector =
_transformationController!.value.getTranslation();
final Offset translation =
Offset(translationVector.x, translationVector.y);
final FrictionSimulation frictionSimulationX = FrictionSimulation(
widget.interactionEndFrictionCoefficient,
translation.dx,
details.velocity.pixelsPerSecond.dx,
);
final FrictionSimulation frictionSimulationY = FrictionSimulation(
widget.interactionEndFrictionCoefficient,
translation.dy,
details.velocity.pixelsPerSecond.dy,
);
final double tFinal = _getFinalTime(
details.velocity.pixelsPerSecond.distance,
widget.interactionEndFrictionCoefficient,
);
_animation = Tween<Offset>(
begin: translation,
end: Offset(frictionSimulationX.finalX, frictionSimulationY.finalX),
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.decelerate,
));
_controller.duration = Duration(milliseconds: (tFinal * 1000).round());
_animation!.addListener(_onAnimate);
_controller.forward();
case _GestureType.scale:
if (details.scaleVelocity.abs() < 0.1) {
_currentAxis = null;
return;
}
final double scale =
_transformationController!.value.getMaxScaleOnAxis();
final FrictionSimulation frictionSimulation = FrictionSimulation(
widget.interactionEndFrictionCoefficient * widget.scaleFactor,
scale,
details.scaleVelocity / 10);
final double tFinal = _getFinalTime(details.scaleVelocity.abs(),
widget.interactionEndFrictionCoefficient,
effectivelyMotionless: 0.1);
_scaleAnimation =
Tween<double>(begin: scale, end: frictionSimulation.x(tFinal))
.animate(CurvedAnimation(
parent: _scaleController, curve: Curves.decelerate));
_scaleController.duration =
Duration(milliseconds: (tFinal * 1000).round());
_scaleAnimation!.addListener(_onScaleAnimate);
_scaleController.forward();
case _GestureType.rotate || null:
break;
}
}

Expand Down

0 comments on commit ca99e4f

Please sign in to comment.