1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import 'package:flutter/material.dart';
- class PolygonAnimController extends AnimationController {
- late List<Tween<Offset>> _tweens;
- late List<Animation<Offset>> _animations;
- late List<Offset> _currentCoords;
- void Function(List<Offset> coords)? onCoordsChange;
- PolygonAnimController(
- {required int len, required super.vsync, required super.duration}) {
- // _animController = AnimationController(
- // vsync: this, duration: const Duration(milliseconds: 400));
- _tweens = List.generate(len, (index) => Tween<Offset>());
- _animations = List.generate(len, (index) => _tweens[index].animate(this));
- _currentCoords = List.generate(len, (index) => const Offset(0, 0));
- addListener(_listener);
- }
- @override
- void dispose() {
- removeListener(_listener);
- super.dispose();
- }
- void _listener() {
- List<Offset> values =
- List.generate(_animations.length, (index) => _animations[index].value);
- onCoordsChange?.call(values);
- }
- void startAnim(List<Offset> freshCoords) {
- assert(freshCoords.length == _tweens.length, "Their length must be equal!");
- for (int i = 0, len = _tweens.length; i < len; i++) {
- _tweens[i].begin = _currentCoords[i];
- _tweens[i].end = freshCoords[i];
- }
- _currentCoords = freshCoords;
- reset();
- forward();
- }
- }
|