fadeIn method

Future<void> fadeIn({
  1. Duration duration = const Duration(milliseconds: 500),
})

Fades in the torch smoothly over the specified duration. Uses torch strength control for smooth transition on supported devices. Falls back to immediate enable on devices without strength control.

duration - The duration of the fade in effect (default: 500ms).

Implementation

Future<void> fadeIn(
    {Duration duration = const Duration(milliseconds: 500)}) async {
  final maxStrength = await getMaxStrength();

  if (maxStrength == 0) {
    // Device doesn't support strength control, just enable immediately
    await enable();
    return;
  }

  // Ensure torch is on first
  await enable();

  // Gradually increase strength
  const steps = 20;
  final stepDuration = duration.inMilliseconds ~/ steps;
  final strengthIncrement = maxStrength / steps;

  for (int i = 1; i <= steps; i++) {
    final strength = (strengthIncrement * i).round();
    await setStrength(strength);
    if (i < steps) {
      await Future.delayed(Duration(milliseconds: stepDuration));
    }
  }
}