attach method

void attach(
  1. TickerProvider vsync,
  2. Duration duration,
  3. VoidCallback listener
)

Attaches this controller to a TickerProvider (usually a State).

Restores the previous progress and playback state if detach was called earlier.

Implementation

void attach(TickerProvider vsync, Duration duration, VoidCallback listener) {
  if (_controller != null) {
    final oldValue = _controller!.value;
    if (_listener != null) {
      _controller!.removeListener(_listener!);
    }
    _controller!.dispose();
    _controller = AnimationController(vsync: vsync, duration: duration);
    _controller!.value = oldValue.clamp(0.0, 1.0);
    _listener = listener;
    _controller!.addListener(listener);
    if (_wasAnimating) {
      _controller!.forward();
      _wasAnimating = false;
    }
  } else {
    _controller = AnimationController(vsync: vsync, duration: duration);
    _controller!.value = _savedProgress.clamp(0.0, 1.0);
    _listener = listener;
    _controller!.addListener(listener);
    if (_wasAnimating) {
      _controller!.forward();
      _wasAnimating = false;
    }
  }
}