lerpOpacity static method

Widget lerpOpacity(
  1. Widget a,
  2. Widget b,
  3. double t, {
  4. AlignmentGeometry alignment = Alignment.center,
})

Creates a smooth opacity-based transition between two widgets.

This lerp function implements a cross-fade effect where the first widget fades out during the first half of the transition (t: 0.0-0.5) while the second widget fades in during the second half (t: 0.5-1.0).

Parameters:

  • a (Widget): The outgoing widget to fade out.
  • b (Widget): The incoming widget to fade in.
  • t (double): Animation progress from 0.0 to 1.0.
  • alignment (AlignmentGeometry): How to align widgets during transition.

Returns a Stack with both widgets positioned and faded appropriately.

Implementation

static Widget lerpOpacity(Widget a, Widget b, double t,
    {AlignmentGeometry alignment = Alignment.center}) {
  if (t == 0) {
    return a;
  } else if (t == 1) {
    return b;
  }
  double startOpacity = 1 - (t.clamp(0, 0.5) * 2);
  double endOpacity = t.clamp(0.5, 1) * 2 - 1;
  return Stack(
    fit: StackFit.passthrough,
    children: [
      Positioned.fill(
        child: Opacity(
            opacity: startOpacity,
            child: Align(
              alignment: alignment,
              child: a,
            )),
      ),
      Opacity(
        opacity: endOpacity,
        child: b,
      ),
    ],
  );
}