I’m working on a Flutter app where I track the user’s movement (speed) using Geolocator and display different overlays based on the speed. If the speed is below 5 km/h, I want to show a “Park Mode” overlay, and if the speed is above 5 km/h, I want to show a “Drive Mode” overlay.
To achieve this, I’m using:
flutter_background_service for background tasks.
Geolocator for location tracking and speed calculation.
flutter_overlay_window_sdk34 for displaying overlays.
The app is successfully calculating the speed, but the overlays are not being triggered automatically as expected when the speed changes. The permissions for overlay windows have been granted, and I can manually display the overlays, but they do not trigger on their own in the background.
Here’s a simplified version of my implementation.
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
double speed = position.speed; // speed in m/s
double speedKmh = speed / 1000;
if (speedKmh < 5.0) {
await FlutterOverlayWindow.requestPermission();
try {
await OverlayUtils.showCustomOverlay(overlayTitle: 'Park Mode');
} catch (e) {
print("Error showing park mode overlay: $e");
}
} else if (speedKmh > 5.0) {
await FlutterOverlayWindow.requestPermission();
try {
await OverlayUtils.showCustomOverlay(overlayTitle: 'Drive Mode');
} catch (e) {
print("Error showing drive mode overlay: $e");
}
}
return Future.value(true);
});
}
Problem:
The overlays don’t automatically trigger when the speed condition is met, even though the app correctly calculates the speed. The overlay logic works when triggered manually, but it doesn’t work as intended in the background.
What I’ve Tried:
Verifying overlay window permissions (they are granted).
Checking if the background service is running (it is).
Manually showing the overlay works fine, but not when triggered from the background task.
Question:
How can I get the overlays to trigger automatically based on the user’s speed while the app is running in the background? Is there an issue with how the background service interacts with the flutter_overlay_window_sdk34 package, or am I missing something in my implementation?
Any help or guidance would be appreciated. Thanks!