onInit method

  1. @override
void onInit()
override

Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.

Implementation

@override
void onInit() {
super.onInit();

if (fromAirport == null || toAirport == null) {
  throw Exception("fromAirport and toAirport must be provided!");
}

source.value = fromAirport!;
destination.value = toAirport!;

print('from airport lat ${fromAirport!.latitude}');
print('from airport long ${fromAirport!.longitude}');
print('to airport long ${toAirport!.longitude}');
print('to airport lat ${toAirport!.latitude}');

// Generate path points
pathCoordinates = interpolatePath(source.value, destination.value, 100);

// Set up the animation
animationController = AnimationController(
  vsync: this,
  duration: Duration(seconds: 12),
);

animation = Tween<double>(begin: 0, end: 1).animate(animationController)
..addListener(() async {
  if (!(flightSearchController.isSearching.value ||
      flightSearchController.isStreamingExclusive.value)) {
    animationController.stop();
    return;
  }

  if (pathCoordinates == null) {
    return;
  }

  if (animationController.isAnimating) {
    int index = (animation.value * (pathCoordinates!.length - 1)).toInt();
    currentPosition.value = pathCoordinates![index];

    if (index < pathCoordinates!.length - 1) {
      // Calculate bearing to the next point
      bearing.value = calculateBearing(
        currentPosition.value,
        pathCoordinates![index + 1],
      );
    }

    // Convert current position to screen position
    if (mapController != null) {
      final screenPoint = await mapController!.toScreenLocation(currentPosition.value);
      screenPosition.value = Offset(screenPoint.x.toDouble(), screenPoint.y.toDouble());

      // Move the camera to follow the plane
      mapController!.moveCamera(
        CameraUpdate.newLatLng(currentPosition.value),
      );
    }
  }
});


// Start the animation
animationController.repeat();
}