polygon_anim.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import 'package:flutter/material.dart';
  2. class PolygonAnimController extends AnimationController {
  3. late List<Tween<Offset>> _tweens;
  4. late List<Animation<Offset>> _animations;
  5. late List<Offset> _currentCoords;
  6. void Function(List<Offset> coords)? onCoordsChange;
  7. PolygonAnimController(
  8. {required int len, required super.vsync, required super.duration}) {
  9. // _animController = AnimationController(
  10. // vsync: this, duration: const Duration(milliseconds: 400));
  11. _tweens = List.generate(len, (index) => Tween<Offset>());
  12. _animations = List.generate(len, (index) => _tweens[index].animate(this));
  13. _currentCoords = List.generate(len, (index) => const Offset(0, 0));
  14. addListener(_listener);
  15. }
  16. @override
  17. void dispose() {
  18. removeListener(_listener);
  19. super.dispose();
  20. }
  21. void _listener() {
  22. List<Offset> values =
  23. List.generate(_animations.length, (index) => _animations[index].value);
  24. onCoordsChange?.call(values);
  25. }
  26. void startAnim(List<Offset> freshCoords) {
  27. assert(freshCoords.length == _tweens.length, "Their length must be equal!");
  28. for (int i = 0, len = _tweens.length; i < len; i++) {
  29. _tweens[i].begin = _currentCoords[i];
  30. _tweens[i].end = freshCoords[i];
  31. }
  32. _currentCoords = freshCoords;
  33. reset();
  34. forward();
  35. }
  36. }