execute method

  1. @override
Future<Map<String, dynamic>> execute(
  1. List arguments,
  2. Map<int, RxElement> elementMap
)
override

Implementation

@override
Future<Map<String, dynamic>> execute(List arguments, elementMap) async {
  // Validate input arguments
  if (arguments.length < 6) {
    throw Exception('Insufficient arguments provided!');
  }

  // Validate and retrieve the element from the arguments
  RxElement? element = arguments.isNotEmpty ? elementMap[arguments[0]] : null;
  if (element == null) {
    throw Exception('Element not found for the provided ID.');
  }

  // Retrieve the necessary properties
  var left = element.properties['ScreenX']?.toDouble();
  var top = element.properties['ScreenY']?.toDouble();
  var width = element.properties['Width']?.toDouble();
  var height = element.properties['Height']?.toDouble();

  if (left == null || top == null || width == null || height == null) {
    throw Exception('Element properties are incomplete.');
  }

  // Retrieve swipe parameters
  double angleDegree = arguments[2]; // Angle in degrees
  String distance = arguments[3]; // Distance to swipe in pixels
  int durationMs = arguments[4]; // Duration of the swipe in milliseconds
  int numSteps = arguments[5]; // Number of intermediate steps for the swipe

  Distance d = Distance.parse(distance);
  int offset = d.calculateFixedDistance(width, height, angleDegree);

  double angle = angleDegree * (pi / 180.0);
  double ax = cos(angle);
  double ay = sin(angle);

  const int minDurationMs = 0;
  int stepCount = max(numSteps, 2);
  int pixelPerStep = max(offset ~/ (stepCount - 1), 1);
  int durationPerStep = max(minDurationMs, durationMs ~/ stepCount);

  double centerX = left + width / 2;
  double centerY = top + height / 2;

  FlutterView view = WidgetsBinding.instance.platformDispatcher.views.first;
  var devicePixelRatio = view.devicePixelRatio;
  double startX = centerX / devicePixelRatio;
  double startY = centerY / devicePixelRatio;

  double endX = startX + pixelPerStep * ax;
  double endY = startY + pixelPerStep * ay;

  pointerDown(startX, startY);
  await pointerMove(startX, startY, endX, endY, stepCount, Duration(milliseconds: durationPerStep));
  pointerUp(endX, endY);

  return {'success': true, 'startPoint': left, 'endPoint': top};
}