Color Effects¶
Color effects are used to change the color of a component over time. They can be used to tint a component, change its opacity, or apply a color filter.
ColorEffect¶
This effect will change the base color of the paint, causing the rendered component to be tinted by the provided color between a provided range.
Usage example:
1import 'package:doc_flame_examples/ember.dart';
2import 'package:flame/components.dart';
3import 'package:flame/effects.dart';
4import 'package:flame/game.dart';
5import 'package:flutter/animation.dart';
6import 'package:flutter/cupertino.dart';
7
8class ColorEffectExample extends FlameGame {
9 bool reset = false;
10
11 @override
12 Future<void> onLoad() async {
13 final ember = EmberPlayer(
14 position: size / 2,
15 size: size / 4,
16 onTap: (ember) {
17 ember.add(
18 ColorEffect(
19 reset ? const Color(0xFF1039DB) : const Color(0xFF00FF00),
20 EffectController(duration: 1.5),
21 opacityTo: 0.6,
22 ),
23 );
24 reset = !reset;
25 },
26 )..anchor = Anchor.center;
27
28 add(ember);
29 }
30}
final effect = ColorEffect(
const Color(0xFF00FF00),
EffectController(duration: 1.5),
opacityFrom: 0.2,
opacityTo: 0.8,
);
The opacityFrom
and opacityTo
arguments will determine “how much” of the color that will be
applied to the component. In this example the effect will start with 20% and will go up to 80%.
Note: Due to how this effect is implemented, and how Flutter’s ColorFilter
class works, this
effect can’t be mixed with other ColorEffect
s, when more than one is added to the component, only
the last one will have effect.
OpacityToEffect
¶
This effect will change the opacity of the target over time to the specified alpha-value.
It can only be applied to components that implement the OpacityProvider
.
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/components.dart';
3import 'package:flame/effects.dart';
4import 'package:flame/game.dart';
5
6class OpacityToEffectGame extends FlameGame {
7 bool reset = false;
8
9 @override
10 Future<void> onLoad() async {
11 final flower = Flower(
12 position: size / 2,
13 size: 60,
14 onTap: _onTap,
15 )..anchor = Anchor.center;
16
17 add(flower);
18 }
19
20 void _onTap(Flower flower) {
21 if (reset) {
22 flower.add(
23 OpacityEffect.fadeIn(
24 EffectController(duration: 0.75),
25 ),
26 );
27 } else {
28 flower.add(
29 OpacityEffect.to(
30 0.2,
31 EffectController(duration: 0.75),
32 ),
33 );
34 }
35 reset = !reset;
36 }
37}
final effect = OpacityEffect.to(
0.2,
EffectController(duration: 0.75),
);
If the component uses multiple paints, the effect can target one more more of those paints
using the target
parameter. The HasPaint
mixin implements OpacityProvider
and exposes APIs
to easily create providers for desired paintIds. For single paintId opacityProviderOf
can be used
and for multiple paintIds and opacityProviderOfList
can be used.
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/components.dart';
3import 'package:flame/effects.dart';
4import 'package:flame/game.dart';
5
6class OpacityEffectWithTargetGame extends FlameGame {
7 bool reset = false;
8
9 // This reference needs to be stored because every new instance of
10 // OpacityProvider caches the opacity ratios at the time on creation.
11 late OpacityProvider _borderOpacityProvider;
12
13 @override
14 Future<void> onLoad() async {
15 final flower = Flower(
16 position: size / 2,
17 size: 60,
18 onTap: _onTap,
19 )..anchor = Anchor.center;
20
21 _borderOpacityProvider = flower.opacityProviderOfList(
22 paintIds: const [FlowerPaint.paintId1, FlowerPaint.paintId2],
23 );
24
25 add(flower);
26 }
27
28 void _onTap(Flower flower) {
29 if (reset = !reset) {
30 flower.add(
31 OpacityEffect.fadeIn(
32 EffectController(duration: 0.75),
33 target: _borderOpacityProvider,
34 ),
35 );
36 } else {
37 flower.add(
38 OpacityEffect.to(
39 0.2,
40 EffectController(duration: 0.75),
41 target: _borderOpacityProvider,
42 ),
43 );
44 }
45 reset = !reset;
46 }
47}
final effect = OpacityEffect.to(
0.2,
EffectController(duration: 0.75),
target: component.opacityProviderOfList(
paintIds: const [paintId1, paintId2],
),
);
The opacity value of 0 corresponds to a fully transparent component, and the opacity value of 1 is
fully opaque. Convenience constructors OpacityEffect.fadeOut()
and OpacityEffect.fadeIn()
will
animate the target into full transparency / full visibility respectively.
OpacityByEffect
¶
This effect will change the opacity of the target relative to the specified alpha-value. For example,
the following effect will change the opacity of the target by 90%
:
1import 'package:doc_flame_examples/ember.dart';
2import 'package:flame/components.dart';
3import 'package:flame/effects.dart';
4import 'package:flame/game.dart';
5
6class OpacityByEffectGame extends FlameGame {
7 bool reset = false;
8
9 @override
10 Future<void> onLoad() async {
11 final ember = EmberPlayer(
12 position: size / 2,
13 size: size / 4,
14 onTap: (ember) {
15 ember.add(
16 OpacityEffect.by(
17 reset ? 0.9 : -0.9,
18 EffectController(duration: 0.75),
19 ),
20 );
21 reset = !reset;
22 },
23 )..anchor = Anchor.center;
24
25 add(ember);
26 }
27}
final effect = OpacityEffect.by(
0.9,
EffectController(duration: 0.75),
);
Currently this effect can only be applied to components that have a HasPaint
mixin. If the target component
uses multiple paints, the effect can target any individual color using the paintId
parameter.
GlowEffect¶
Note
This effect is currently experimental, and its API may change in the future.
This effect will apply the glowing shade around target relative to the specified
glow-strength
. The color of shade will be targets paint color. For example, the following effect
will apply the glowing shade around target by strength of 10
:
1import 'package:flame/components.dart';
2import 'package:flame/effects.dart';
3import 'package:flame/game.dart';
4
5import 'package:flutter/material.dart';
6
7class GlowEffectExample extends FlameGame {
8 @override
9 Future<void> onLoad() async {
10 final paint = Paint()..color = const Color(0xff39FF14);
11
12 add(
13 CircleComponent(
14 radius: size.y / 4,
15 position: size / 2,
16 anchor: Anchor.center,
17 paint: paint,
18 )..add(
19 GlowEffect(
20 10.0,
21 EffectController(
22 duration: 2,
23 infinite: true,
24 ),
25 ),
26 ),
27 );
28 }
29}
final effect = GlowEffect(
10.0,
EffectController(duration: 3),
);
Currently this effect can only be applied to components that have a HasPaint
mixin.