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 OpacityEffect extends ComponentEffect<HasPaint> {
9 : final double opacity;
10 : final double duration;
11 : final String? paintId;
12 :
13 : late Color _original;
14 : late Color _final;
15 :
16 : late double _difference;
17 :
18 1 : OpacityEffect({
19 : required this.opacity,
20 : required this.duration,
21 : this.paintId,
22 : Curve? curve,
23 : bool isInfinite = false,
24 : bool isAlternating = false,
25 1 : }) : super(
26 : isInfinite,
27 : isAlternating,
28 : curve: curve,
29 : );
30 :
31 0 : OpacityEffect.fadeOut({
32 : this.duration = 1,
33 : this.paintId,
34 : Curve? curve,
35 : bool isInfinite = false,
36 : bool isAlternating = false,
37 : }) : opacity = 0,
38 0 : super(
39 : isInfinite,
40 : isAlternating,
41 : curve: curve,
42 : );
43 :
44 0 : OpacityEffect.fadeIn({
45 : this.duration = 1,
46 : this.paintId,
47 : Curve? curve,
48 : bool isInfinite = false,
49 : bool isAlternating = false,
50 : }) : opacity = 1,
51 0 : super(
52 : isInfinite,
53 : isAlternating,
54 : curve: curve,
55 : );
56 :
57 1 : @override
58 : void initialize(HasPaint component) {
59 1 : super.initialize(component);
60 2 : peakTime = duration;
61 :
62 4 : _original = component.getPaint(paintId).color;
63 4 : _final = _original.withOpacity(opacity);
64 :
65 5 : _difference = _original.opacity - opacity;
66 : }
67 :
68 0 : @override
69 : void setComponentToEndState() {
70 0 : component?.setColor(
71 0 : _final,
72 0 : paintId: paintId,
73 : );
74 : }
75 :
76 0 : @override
77 : void setComponentToOriginalState() {
78 0 : component?.setColor(
79 0 : _original,
80 0 : paintId: paintId,
81 : );
82 : }
83 :
84 1 : @override
85 : void update(double dt) {
86 1 : super.update(dt);
87 2 : component?.setOpacity(
88 6 : _original.opacity - _difference * curveProgress,
89 1 : paintId: paintId,
90 : );
91 : }
92 : }
|