Line data Source code
1 : import 'dart:ui'; 2 : 3 : import 'package:flutter/animation.dart'; 4 : 5 : import '../components/mixins/has_paint.dart'; 6 : import 'effects.dart'; 7 : 8 : class ColorEffect extends ComponentEffect<HasPaint> { 9 : final Color color; 10 : final double duration; 11 : final String? paintId; 12 : 13 : late Paint _original; 14 : 15 : late ColorTween _tween; 16 : 17 1 : ColorEffect({ 18 : required this.color, 19 : required this.duration, 20 : this.paintId, 21 : Curve? curve, 22 : bool isInfinite = false, 23 : bool isAlternating = false, 24 1 : }) : super( 25 : isInfinite, 26 : isAlternating, 27 : curve: curve, 28 : ); 29 : 30 1 : @override 31 : void initialize(HasPaint component) { 32 1 : super.initialize(component); 33 2 : peakTime = duration; 34 : 35 3 : _original = component.getPaint(paintId); 36 : 37 2 : _tween = ColorTween( 38 2 : begin: _original.color, 39 1 : end: color, 40 : ); 41 : } 42 : 43 0 : @override 44 : void setComponentToEndState() { 45 0 : component?.tint(color); 46 : } 47 : 48 0 : @override 49 : void setComponentToOriginalState() { 50 0 : component?.paint = _original; 51 : } 52 : 53 1 : @override 54 : void update(double dt) { 55 1 : super.update(dt); 56 3 : final color = _tween.lerp(curveProgress); 57 : if (color != null) { 58 2 : component?.tint(color); 59 : } 60 : } 61 : }