Skip to content

Commit

Permalink
refactor: Fix lint issues from latest flutter release (#3390)
Browse files Browse the repository at this point in the history
Updates to use the new color method `withValues`.

---------

Co-authored-by: Lukas Klingsbo <[email protected]>
Co-authored-by: Jochum van der Ploeg <[email protected]>
  • Loading branch information
3 people authored Dec 13, 2024
1 parent d4094b2 commit 978ad31
Show file tree
Hide file tree
Showing 104 changed files with 284 additions and 150 deletions.
1 change: 1 addition & 0 deletions .github/.cspell/words_dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ tappable
tappables
toolset
underutilize
endianness
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:


env:
FLUTTER_MIN_VERSION: '3.24.0'
FLUTTER_MIN_VERSION: '3.27.0'

jobs:
# BEGIN LINTING STAGE
Expand Down
6 changes: 3 additions & 3 deletions doc/flame/examples/lib/drag_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Trail extends Component {
final path = _paths[i];
final opacity = _opacities[i];
if (opacity > 0) {
_linePaint.color = _color.withOpacity(opacity);
_linePaint.color = _color.withValues(alpha: opacity);
_linePaint.strokeWidth = lineWidth * opacity;
canvas.drawPath(path, _linePaint);
}
Expand Down Expand Up @@ -216,11 +216,11 @@ class Star extends PositionComponent with DragCallbacks {
@override
void render(Canvas canvas) {
if (isDragged) {
_paint.color = color.withOpacity(0.5);
_paint.color = color.withValues(alpha: 0.5);
canvas.drawPath(_path, _paint);
canvas.drawPath(_path, _borderPaint);
} else {
_paint.color = color.withOpacity(1);
_paint.color = color.withValues(alpha: 1);
canvas.drawPath(_path, _shadowPaint);
canvas.drawPath(_path, _paint);
}
Expand Down
6 changes: 3 additions & 3 deletions doc/flame/examples/lib/pointer_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HoverTarget extends PositionComponent with HoverCallbacks {
final _paint = Paint()
..color = HSLColor.fromAHSL(1, _random.nextDouble() * 360, 1, 0.8)
.toColor()
.withOpacity(0.5);
.withValues(alpha: 0.5);

@override
void render(Canvas canvas) {
Expand All @@ -41,11 +41,11 @@ class HoverTarget extends PositionComponent with HoverCallbacks {

@override
void onHoverEnter() {
_paint.color = _paint.color.withOpacity(1);
_paint.color = _paint.color.withValues(alpha: 1);
}

@override
void onHoverExit() {
_paint.color = _paint.color.withOpacity(0.5);
_paint.color = _paint.color.withValues(alpha: 0.5);
}
}
2 changes: 1 addition & 1 deletion doc/flame/examples/lib/ray_cast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RayCastExample extends FlameGame with HasCollisionDetection {
final velocity = 60;
double get resetPosition => -canvasSize.y;

Paint paint = Paint()..color = Colors.red.withOpacity(0.6);
Paint paint = Paint()..color = Colors.red.withValues(alpha: 0.6);

RaycastResult<ShapeHitbox>? result;

Expand Down
2 changes: 1 addition & 1 deletion doc/flame/examples/lib/ray_trace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:flutter/material.dart';

class RayTraceExample extends FlameGame
with HasCollisionDetection, TapDetector {
Paint paint = Paint()..color = Colors.red.withOpacity(0.6);
Paint paint = Paint()..color = Colors.red.withValues(alpha: 0.6);
bool isClicked = false;

Vector2 get origin => canvasSize / 2;
Expand Down
2 changes: 1 addition & 1 deletion doc/flame/examples/lib/tap_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ExpandingCircle extends Component {
removeFromParent();
} else {
final opacity = 1 - radius / maxRadius;
_paint.color = _baseColor.withOpacity(opacity);
_paint.color = _baseColor.withValues(alpha: opacity);
_paint.strokeWidth = _outerRadius - _innerRadius;
}
}
Expand Down
2 changes: 1 addition & 1 deletion doc/flame/rendering/particles.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ game.add(
ParticleSystemComponent(
particle: CircleParticle(
radius: game.size.x / 2,
paint: Paint()..color = Colors.red.withOpacity(.5),
paint: Paint()..color = Colors.red.withValues(alpha: .5),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion examples/games/padracing/lib/lap_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LapLine extends BodyComponent with ContactCallbacks {
@override
Body createBody() {
paint.color = (isFinish ? GameColors.green.color : GameColors.green.color)
..withOpacity(0.5);
..withValues(alpha: 0.5);
paint
..style = PaintingStyle.fill
..shader = Gradient.radial(
Expand Down
2 changes: 1 addition & 1 deletion examples/games/padracing/lib/trail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Trail extends Component with HasPaint {
@override
Future<void> onLoad() async {
paint
..color = (tire.paint.color.withOpacity(0.9))
..color = (tire.paint.color.withValues(alpha: 0.9))
..strokeWidth = 1.0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class MouseJointWorld extends Forge2DWorld
@override
void onDragStart(DragStartEvent info) {
super.onDragStart(info);
if (mouseJoint != null) {
return;
}
final mouseJointDef = MouseJointDef()
..maxForce = 3000 * ball.body.mass * 10
..dampingRatio = 0.1
Expand All @@ -47,10 +50,8 @@ class MouseJointWorld extends Forge2DWorld
..bodyA = groundBody
..bodyB = ball.body;

if (mouseJoint == null) {
mouseJoint = MouseJoint(mouseJointDef);
createJoint(mouseJoint!);
}
mouseJoint = MouseJoint(mouseJointDef);
createJoint(mouseJoint!);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ExpandingCircle extends CircleComponent {
removeFromParent();
} else {
final opacity = 1 - radius / maxRadius;
paint.color = const Color(0xffffffff).withOpacity(opacity);
paint.color = const Color(0xffffffff).withValues(alpha: opacity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class TextButton extends ButtonComponent {
),
buttonDown: RectangleComponent(
size: Vector2(200, 100),
paint: Paint()..color = BasicPalette.orange.color.withOpacity(0.5),
paint: Paint()
..color = BasicPalette.orange.color.withValues(alpha: 0.5),
),
children: [
TextComponent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ abstract class MyCollidable extends PositionComponent
final Vector2 velocity;
final delta = Vector2.zero();
double angleDelta = 0;
final Color _defaultColor = Colors.blue.withOpacity(0.8);
final Color _collisionColor = Colors.green.withOpacity(0.8);
final Color _defaultColor = Colors.blue.withValues(alpha: 0.8);
final Color _collisionColor = Colors.green.withValues(alpha: 0.8);
late final Paint _dragIndicatorPaint;
final ScreenHitbox screenHitbox;
ShapeHitbox? hitbox;
Expand Down Expand Up @@ -219,7 +219,7 @@ class CollidableCircle extends MyCollidable {
class SnowmanPart extends CircleHitbox {
@override
final renderShape = true;
final startColor = Colors.white.withOpacity(0.8);
final startColor = Colors.white.withValues(alpha: 0.8);
final Color hitColor;

SnowmanPart(double radius, Vector2 position, this.hitColor)
Expand All @@ -234,7 +234,7 @@ class SnowmanPart extends CircleHitbox {
if (other.hitboxParent is ScreenHitbox) {
paint.color = startColor;
} else {
paint.color = hitColor.withOpacity(0.8);
paint.color = hitColor.withValues(alpha: 0.8);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ around trying not to hit them.
Ray2? ray;
Ray2? reflection;
Vector2 origin = Vector2(250, 100);
Paint paint = Paint()..color = Colors.amber.withOpacity(0.6);
Paint paint = Paint()..color = Colors.amber.withValues(alpha: 0.6);
final speed = 100;
final inertia = 3.0;
final safetyDistance = 50;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ with with mouse.
Paint tapPaint = Paint();

final _colorTween = ColorTween(
begin: Colors.blue.withOpacity(0.2),
end: Colors.red.withOpacity(0.2),
begin: Colors.blue.withValues(alpha: 0.2),
end: Colors.red.withValues(alpha: 0.2),
);

static const numberOfRays = 2000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ bounce on will appear.
''';

final _colorTween = ColorTween(
begin: Colors.amber.withOpacity(1.0),
end: Colors.lightBlueAccent.withOpacity(1.0),
begin: Colors.amber.withValues(alpha: 1.0),
end: Colors.lightBlueAccent.withValues(alpha: 1.0),
);
final random = Random();
Ray2? ray;
Expand Down
4 changes: 2 additions & 2 deletions examples/lib/stories/components/keys_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ class SelectableClass extends SpriteComponent {
super.size,
super.key,
super.sprite,
}) : super(paint: Paint()..color = Colors.white.withOpacity(0.5));
}) : super(paint: Paint()..color = Colors.white.withValues(alpha: 0.5));

bool _selected = false;
bool get selected => _selected;
set selected(bool value) {
_selected = value;
paint = Paint()
..color = value ? Colors.white : Colors.white.withOpacity(0.5);
..color = value ? Colors.white : Colors.white.withValues(alpha: 0.5);
}
}
2 changes: 1 addition & 1 deletion examples/lib/stories/effects/opacity_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OpacityEffectExample extends FlameGame with TapDetector {

@override
void onTap() {
final opacity = sprite.paint.color.opacity;
final opacity = sprite.paint.color.a;
if (opacity >= 0.5) {
sprite.add(OpacityEffect.fadeOut(EffectController(duration: 1)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TapCallbacksSquare extends RectangleComponent with TapCallbacks {
position: position ?? Vector2.all(100),
size: Vector2.all(100),
paint: continuePropagation
? (Paint()..color = Colors.green.withOpacity(0.9))
? (Paint()..color = Colors.green.withValues(alpha: 0.9))
: PaintExtension.random(withAlpha: 0.9, base: 100),
);

Expand Down
3 changes: 2 additions & 1 deletion examples/lib/stories/rendering/particles_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ class ParticlesExample extends FlameGame {
renderer: (canvas, particle) {
final paint = randomElement(paints);
// Override the color to dynamically update opacity
paint.color = paint.color.withOpacity(1 - particle.progress);
paint.color =
paint.color.withValues(alpha: 1 - particle.progress);

canvas.drawCircle(
Offset.zero,
Expand Down
21 changes: 12 additions & 9 deletions packages/flame/lib/src/components/mixins/has_paint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ mixin HasPaint<T extends Object> on Component
throw ArgumentError('Opacity needs to be between 0 and 1');
}

setColor(getPaint(paintId).color.withOpacity(opacity), paintId: paintId);
setColor(
getPaint(paintId).color.withValues(alpha: opacity),
paintId: paintId,
);
}

/// Returns the current opacity.
double getOpacity({T? paintId}) {
return getPaint(paintId).color.opacity;
return getPaint(paintId).color.a;
}

/// Changes the opacity of the paint.
Expand All @@ -102,7 +105,7 @@ mixin HasPaint<T extends Object> on Component

/// Returns the current opacity.
int getAlpha({T? paintId}) {
return getPaint(paintId).color.alpha;
return getPaint(paintId).color.a ~/ 255;
}

/// Shortcut for changing the color of the paint.
Expand All @@ -118,13 +121,13 @@ mixin HasPaint<T extends Object> on Component
}

@override
double get opacity => paint.color.opacity;
double get opacity => paint.color.a;

@override
set opacity(double value) {
paint.color = paint.color.withOpacity(value);
paint.color = paint.color.withValues(alpha: value);
for (final paint in _paints.values) {
paint.color = paint.color.withOpacity(value);
paint.color = paint.color.withValues(alpha: value);
}
}

Expand Down Expand Up @@ -186,7 +189,7 @@ class _MultiPaintOpacityProvider<T extends Object> implements OpacityProvider {
];
_layerOpacityRatios = target.paintLayersInternal
?.map(
(paint) => paint.color.opacity / maxOpacity,
(paint) => paint.color.a / maxOpacity,
)
.toList(growable: false);
}
Expand All @@ -206,7 +209,7 @@ class _MultiPaintOpacityProvider<T extends Object> implements OpacityProvider {
}
if (includeLayers) {
target.paintLayersInternal?.forEach(
(paint) => maxOpacity = max(paint.color.opacity, maxOpacity),
(paint) => maxOpacity = max(paint.color.a, maxOpacity),
);
}

Expand All @@ -226,7 +229,7 @@ class _MultiPaintOpacityProvider<T extends Object> implements OpacityProvider {
for (var i = 0; i < (paintLayersInternal?.length ?? 0); ++i) {
paintLayersInternal![i].color = paintLayersInternal[i]
.color
.withOpacity(value * _layerOpacityRatios![i]);
.withValues(alpha: value * _layerOpacityRatios![i]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/effects/color_effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class ColorEffect extends ComponentEffect<HasPaint> {

@override
void apply(double progress) {
final currentColor = color.withOpacity(
min(max(_tween.transform(progress), 0), 1),
final currentColor = color.withValues(
alpha: min(max(_tween.transform(progress), 0), 1),
);
target.tint(currentColor, paintId: paintId);
}
Expand Down
16 changes: 8 additions & 8 deletions packages/flame/lib/src/extensions/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ extension ColorExtension on Color {

final f = 1 - amount;
return Color.fromARGB(
alpha,
(red * f).round(),
(green * f).round(),
(blue * f).round(),
a ~/ 255,
(r * f).round(),
(g * f).round(),
(b * f).round(),
);
}

Expand All @@ -30,10 +30,10 @@ extension ColorExtension on Color {
assert(amount >= 0 && amount <= 1);

return Color.fromARGB(
alpha,
red + ((255 - red) * amount).round(),
green + ((255 - green) * amount).round(),
blue + ((255 - blue) * amount).round(),
a ~/ 255,
(r + ((1.0 - r) * amount)) ~/ 255,
(g + ((1.0 - g) * amount)) ~/ 255,
(b + ((1.0 - b) * amount)) ~/ 255,
);
}

Expand Down
Loading

0 comments on commit 978ad31

Please sign in to comment.