fadeOut method
Fades out the torch smoothly over the specified duration. Uses torch strength control for smooth transition on supported devices. Falls back to immediate disable on devices without strength control.
duration - The duration of the fade out effect (default: 500ms).
Implementation
Future<void> fadeOut(
{Duration duration = const Duration(milliseconds: 500)}) async {
final maxStrength = await getMaxStrength();
final currentStrength = await getCurrentStrength();
if (maxStrength == 0 || currentStrength == 0) {
// Device doesn't support strength control or torch is off, just disable immediately
await disable();
return;
}
// Gradually decrease strength
const steps = 20;
final stepDuration = duration.inMilliseconds ~/ steps;
final strengthDecrement = currentStrength / steps;
for (int i = 1; i <= steps; i++) {
final strength = (currentStrength - (strengthDecrement * i)).round();
await setStrength(strength.clamp(0, maxStrength));
if (i < steps) {
await Future.delayed(Duration(milliseconds: stepDuration));
}
}
// Ensure torch is off at the end
await disable();
}