calculateEndRotate function

double calculateEndRotate(
  1. RouletteGroup group,
  2. int targetIndex,
  3. bool clockwise,
  4. int minRotateCircles, {
  5. double offset = 0,
})

Calculate the end rotate value by targetIndex. The returned value contains the circles to roll. Make sure when you run this method the group has at least one RouletteUnit.

group is the RouletteGroup to run rotate animation. targetIndex is the end index in the group. clockwise determin whether the animator should run in closewise didrection. minRotateCircles circles rolled before. random Provide a random number generator for randomization, default null and no random value.

Implementation

double calculateEndRotate(
  RouletteGroup group,
  int targetIndex,
  bool clockwise,
  int minRotateCircles, {
  double offset = 0,
}) {
  final units = group.units;
  assert(units.isNotEmpty, "You cannot roll an empty roulette.");
  assert(
    targetIndex >= 0 && targetIndex < group.divide,
    "targetIndex is out of group range.",
  );
  final passUnits = clockwise
      ? units.reversed.take(group.divide - targetIndex - 1)
      : units.take(targetIndex);
  final preRotation = minRotateCircles * 2 * pi;
  final totalRotateWeight =
      passUnits.sum((unit) => unit.weight); // Weights should rotate
  final targetRotate = 2 * pi * totalRotateWeight / group.totalWeights;
  final targetCoverRotate =
      2 * pi * units[targetIndex].weight / group.totalWeights;
  var offsetValue = offset * targetCoverRotate; // Random rotate out
  final totalRotate = preRotation + targetRotate + offsetValue;
  return clockwise ? totalRotate : -totalRotate;
}