initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:
- In initState, subscribe to the object.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState().
Implementation
@override
void initState() {
widget.controller.addListener(_update);
widget.controller.rotationController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
)..addListener(() {
if (mounted) {
setState(() {
rotationZ = (rotationZ -
(widget.controller.rotationSpeed *
((math.pow((2 * math.pi), 2) / 360)))) %
(2 * math.pi);
});
// Update hover coordinates during rotation if mouse is over the globe
_updateHoverCoordinatesDuringRotation();
if (widget.controller.rotationController.isCompleted) {
if (widget.controller.isRotating) {
widget.controller.rotationController.repeat();
}
}
}
});
widget.controller.onPointConnectionAdded = _addConnection;
widget.controller.onResetGlobeRotation = resetRotation;
widget.controller.onStartDayNightCycleAnimation =
startDayNightCycleAnimation;
widget.controller.onStopDayNightCycleAnimation = stopDayNightCycleAnimation;
// Globe.GL-style continuous animation controller
// Uses a simple repeating animation to drive frame updates
_lineMovingController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 16), // ~60fps
)
..addListener(() {
if (mounted) {
// Check if there are any moving connections or connections with dashAnimateTime
bool hasAnimatingConnections = false;
for (var connection in widget.controller.connections) {
if (connection.isMoving || connection.style.dashAnimateTime > 0) {
hasAnimatingConnections = true;
break;
}
}
// Check if there are any orbiting satellites
bool hasOrbitingSatellites = false;
for (var satellite in widget.controller.satellites) {
if (satellite.orbit != null) {
hasOrbitingSatellites = true;
break;
}
}
// Trigger foreground repaint for animations
// Use modulo to prevent integer overflow after long runtime
if (hasAnimatingConnections || hasOrbitingSatellites) {
_animationNotifier.value = (_animationNotifier.value + 1) % 1000000;
}
}
})
..repeat();
rotationX = 0;
rotationY = 0; // Initialize rotationY
rotationZ = 0;
_decelerationController = AnimationController(
vsync: this,
duration: const Duration(
milliseconds: 1200), // Longer for smoother deceleration
)..addListener(() {
if (mounted) {
// Use easeOutQuint for smoother, more natural deceleration like globe.gl
final t =
Curves.easeOutQuint.transform(_decelerationController.value);
rotationX =
_initialRotationX + (_targetRotationX - _initialRotationX) * t;
rotationY =
_initialRotationY + (_targetRotationY - _initialRotationY) * t;
rotationZ =
_initialRotationZ + (_targetRotationZ - _initialRotationZ) * t;
setState(() {});
}
});
// Initialize zoom animation controller for smooth zoom transitions
_zoomAnimationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 300),
)..addListener(() {
if (mounted) {
final t =
Curves.easeOutCubic.transform(_zoomAnimationController!.value);
widget.controller.zoom =
_initialZoom + (_targetZoom - _initialZoom) * t;
widget.onZoomChanged?.call(widget.controller.zoom);
setState(() {});
}
});
// Initialize day/night cycle animation controller
_initDayNightCycleController();
// Initialize GPU shader rendering
_initShaderRendering();
Future.delayed(Duration.zero, () {
widget.controller.load();
});
super.initState();
}