From 10e4109c81b266147ec35744e484c2ec7ea15acd Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Fri, 25 Aug 2023 11:15:17 -0700 Subject: [PATCH] refactor: Fix lint issues across the codebase - Part 2 (#2677) Fix a handful of lint issues across the codebase, identified using DCM. Nothing controversial, I expect; slowly getting these out of the way so we can focus on discussing bigger things. --- .../games/padracing/lib/padracing_game.dart | 4 +- examples/games/padracing/lib/tire.dart | 7 +-- .../lib/components/enemy_component.dart | 2 +- examples/games/trex/lib/background/cloud.dart | 2 +- examples/games/trex/lib/random_extension.dart | 2 +- .../audio/basic_audio_example.dart | 6 +-- .../input/hardware_keyboard_example.dart | 10 ++-- .../input/sprite_button_component.dart | 6 ++- .../lib/src/core/component_tree.dart | 2 +- .../src/widgets/toolbar/toolbar_button.dart | 48 +++++++++++++------ 10 files changed, 56 insertions(+), 33 deletions(-) diff --git a/examples/games/padracing/lib/padracing_game.dart b/examples/games/padracing/lib/padracing_game.dart index 2f8b989745..f159c2a8fe 100644 --- a/examples/games/padracing/lib/padracing_game.dart +++ b/examples/games/padracing/lib/padracing_game.dart @@ -44,8 +44,8 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { @override Color backgroundColor() => Colors.black; - static Vector2 trackSize = Vector2.all(500); - static double playZoom = 8.0; + static final Vector2 trackSize = Vector2.all(500); + static const double playZoom = 8.0; static const int numberOfLaps = 3; late final World cameraWorld; late CameraComponent startCamera; diff --git a/examples/games/padracing/lib/tire.dart b/examples/games/padracing/lib/tire.dart index d21e814681..82af7a6356 100644 --- a/examples/games/padracing/lib/tire.dart +++ b/examples/games/padracing/lib/tire.dart @@ -69,9 +69,10 @@ class Tire extends BodyComponent { @override Body createBody() { - final jointAnchor = isFrontTire - ? Vector2(isLeftTire ? -3.0 : 3.0, 3.5) - : Vector2(isLeftTire ? -3.0 : 3.0, -4.25); + final jointAnchor = Vector2( + isLeftTire ? -3.0 : 3.0, + isFrontTire ? 3.5 : -4.25, + ); final def = BodyDef() ..type = BodyType.dynamic diff --git a/examples/games/rogue_shooter/lib/components/enemy_component.dart b/examples/games/rogue_shooter/lib/components/enemy_component.dart index 51a1e424f0..6c95e639e7 100644 --- a/examples/games/rogue_shooter/lib/components/enemy_component.dart +++ b/examples/games/rogue_shooter/lib/components/enemy_component.dart @@ -6,7 +6,7 @@ import 'package:rogue_shooter/rogue_shooter_game.dart'; class EnemyComponent extends SpriteAnimationComponent with HasGameRef, CollisionCallbacks { static const speed = 150; - static Vector2 initialSize = Vector2.all(25); + static final Vector2 initialSize = Vector2.all(25); EnemyComponent({required super.position}) : super(size: initialSize, anchor: Anchor.center); diff --git a/examples/games/trex/lib/background/cloud.dart b/examples/games/trex/lib/background/cloud.dart index a41bf6f030..e0239dd1f4 100644 --- a/examples/games/trex/lib/background/cloud.dart +++ b/examples/games/trex/lib/background/cloud.dart @@ -15,7 +15,7 @@ class Cloud extends SpriteComponent size: initialSize, ); - static Vector2 initialSize = Vector2(92.0, 28.0); + static final Vector2 initialSize = Vector2(92.0, 28.0); static const double maxCloudGap = 400.0; static const double minCloudGap = 100.0; diff --git a/examples/games/trex/lib/random_extension.dart b/examples/games/trex/lib/random_extension.dart index 0ef7c6e019..f3d4fd46ca 100644 --- a/examples/games/trex/lib/random_extension.dart +++ b/examples/games/trex/lib/random_extension.dart @@ -1,6 +1,6 @@ import 'dart:math'; -Random random = Random(); +final random = Random(); extension RandomExtension on Random { double fromRange(double min, double max) => diff --git a/examples/lib/stories/bridge_libraries/audio/basic_audio_example.dart b/examples/lib/stories/bridge_libraries/audio/basic_audio_example.dart index 38a7cf7935..ffcb6997a0 100644 --- a/examples/lib/stories/bridge_libraries/audio/basic_audio_example.dart +++ b/examples/lib/stories/bridge_libraries/audio/basic_audio_example.dart @@ -17,9 +17,9 @@ class BasicAudioExample extends FlameGame with TapDetector { 3. Uses the Bgm utility for background music. '''; - static Paint black = BasicPalette.black.paint(); - static Paint gray = const PaletteEntry(Color(0xFFCCCCCC)).paint(); - static TextPaint text = TextPaint( + static final Paint black = BasicPalette.black.paint(); + static final Paint gray = const PaletteEntry(Color(0xFFCCCCCC)).paint(); + static final TextPaint text = TextPaint( style: TextStyle(color: BasicPalette.white.color), ); diff --git a/examples/lib/stories/input/hardware_keyboard_example.dart b/examples/lib/stories/input/hardware_keyboard_example.dart index 82ea23ea76..69ff4fd825 100644 --- a/examples/lib/stories/input/hardware_keyboard_example.dart +++ b/examples/lib/stories/input/hardware_keyboard_example.dart @@ -7,7 +7,7 @@ import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; class HardwareKeyboardExample extends FlameGame { - static String description = ''' + static const String description = ''' This example uses the HardwareKeyboardDetector mixin in order to keep track of which keys on a keyboard are currently pressed. @@ -67,7 +67,7 @@ class MyKeyboardDetector extends HardwareKeyboardDetector /// The names of keyboard keys (at least the most important ones). We can't /// rely on `key.debugName` because this property is not available in release /// builds. - static Map keyNames = { + static final Map keyNames = { PhysicalKeyboardKey.hyper: 'Hyper', PhysicalKeyboardKey.superKey: 'Super', PhysicalKeyboardKey.fn: 'Fn', @@ -233,12 +233,12 @@ class KeyboardKey extends PositionComponent { /// they are waiting to be removed. bool visible = true; - static Vector2 padding = Vector2(24, 12); - static Paint borderPaint = Paint() + static final Vector2 padding = Vector2(24, 12); + static final Paint borderPaint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 3 ..color = const Color(0xffb5ffd0); - static TextPaint textRenderer = TextPaint( + static final TextPaint textRenderer = TextPaint( style: const TextStyle( fontSize: 20, fontWeight: FontWeight.bold, diff --git a/packages/flame/lib/src/components/input/sprite_button_component.dart b/packages/flame/lib/src/components/input/sprite_button_component.dart index b4d8ca99db..bd3d3a4b66 100644 --- a/packages/flame/lib/src/components/input/sprite_button_component.dart +++ b/packages/flame/lib/src/components/input/sprite_button_component.dart @@ -46,8 +46,10 @@ class SpriteButtonComponent extends SpriteGroupComponent button != null, 'The button sprite has to be set either in onLoad or in the constructor', ); - sprites = {ButtonState.up: button!}; - sprites![ButtonState.down] = buttonDown ?? button!; + sprites = { + ButtonState.up: button!, + ButtonState.down: buttonDown ?? button!, + }; super.onMount(); } diff --git a/packages/flame_studio/lib/src/core/component_tree.dart b/packages/flame_studio/lib/src/core/component_tree.dart index 61209a22cf..60e750689a 100644 --- a/packages/flame_studio/lib/src/core/component_tree.dart +++ b/packages/flame_studio/lib/src/core/component_tree.dart @@ -63,7 +63,7 @@ class ComponentTreeObserver extends StateNotifier { } } - static Duration refreshFrequency = const Duration(milliseconds: 300); + static const Duration refreshFrequency = Duration(milliseconds: 300); void _refresh() { if (!mounted) { diff --git a/packages/flame_studio/lib/src/widgets/toolbar/toolbar_button.dart b/packages/flame_studio/lib/src/widgets/toolbar/toolbar_button.dart index 8d23937d8a..f6163eabd7 100644 --- a/packages/flame_studio/lib/src/widgets/toolbar/toolbar_button.dart +++ b/packages/flame_studio/lib/src/widgets/toolbar/toolbar_button.dart @@ -68,6 +68,13 @@ class ToolbarButtonState extends ConsumerState { } } +enum _ToolbarButtonRenderState { + disabled, + active, + hovered, + normal, +} + class _ToolbarButtonPainter extends CustomPainter { _ToolbarButtonPainter( this.icon, @@ -89,26 +96,26 @@ class _ToolbarButtonPainter extends CustomPainter { canvas.save(); canvas.scale(size.height / 20.0); + final renderState = _renderState; + final radius = Radius.circular(theme.buttonRadius); - final color = isDisabled - ? theme.buttonDisabledColor - : isActive - ? theme.buttonActiveColor - : isHovered - ? theme.buttonHoverColor - : theme.buttonColor; + final color = switch (renderState) { + _ToolbarButtonRenderState.disabled => theme.buttonDisabledColor, + _ToolbarButtonRenderState.active => theme.buttonActiveColor, + _ToolbarButtonRenderState.hovered => theme.buttonHoverColor, + _ToolbarButtonRenderState.normal => theme.buttonColor, + }; canvas.drawRRect( RRect.fromLTRBR(0, 0, size.width / scale, 20.0, radius), Paint()..color = color, ); - final textColor = isDisabled - ? theme.buttonDisabledTextColor - : isActive - ? theme.buttonActiveTextColor - : isHovered - ? theme.buttonHoverTextColor - : theme.buttonTextColor; + final textColor = switch (renderState) { + _ToolbarButtonRenderState.disabled => theme.buttonDisabledTextColor, + _ToolbarButtonRenderState.active => theme.buttonActiveTextColor, + _ToolbarButtonRenderState.hovered => theme.buttonHoverTextColor, + _ToolbarButtonRenderState.normal => theme.buttonTextColor, + }; canvas.translate(size.width / scale / 2, 10); canvas.drawPath(icon, Paint()..color = textColor); canvas.restore(); @@ -121,4 +128,17 @@ class _ToolbarButtonPainter extends CustomPainter { isDisabled != old.isDisabled || icon != old.icon; } + + _ToolbarButtonRenderState get _renderState { + if (isDisabled) { + return _ToolbarButtonRenderState.disabled; + } + if (isActive) { + return _ToolbarButtonRenderState.active; + } + if (isHovered) { + return _ToolbarButtonRenderState.hovered; + } + return _ToolbarButtonRenderState.normal; + } }