fix: apply code review

pull/206/head
arturplaczek 3 years ago
parent 57337138c8
commit 111295d89e

@ -1,13 +1,13 @@
// ignore_for_file: public_member_api_docs
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/gen/gen.dart'; import 'package:pinball/gen/gen.dart';
/// {@template game_hud} /// {@template game_hud}
/// Overlay of a [PinballGame] that displays the current [GameState.score], /// Overlay of a [PinballGame].
/// [GameState.balls] and animation when the player get the bonus. ///
/// Displays the current [GameState.score], [GameState.balls] and animates when
/// the player gets a [GameBonus].
/// {@endtemplate} /// {@endtemplate}
class GameHud extends StatefulWidget { class GameHud extends StatefulWidget {
/// {@macro game_hud} /// {@macro game_hud}
@ -33,7 +33,7 @@ class _GameHudState extends State<GameHud> {
height: _width / _ratio, height: _width / _ratio,
width: _width, width: _width,
child: BlocListener<GameBloc, GameState>( child: BlocListener<GameBloc, GameState>(
listenWhen: _listenWhen, listenWhen: _listenWhenBonusChanges,
listener: (_, __) => setState(() => showAnimation = true), listener: (_, __) => setState(() => showAnimation = true),
child: AnimatedSwitcher( child: AnimatedSwitcher(
duration: kThemeAnimationDuration, duration: kThemeAnimationDuration,
@ -52,7 +52,7 @@ class _GameHudState extends State<GameHud> {
); );
} }
bool _listenWhen(GameState previous, GameState current) { bool _listenWhenBonusChanges(GameState previous, GameState current) {
final previousCount = previous.bonusHistory.length; final previousCount = previous.bonusHistory.length;
final currentCount = current.bonusHistory.length; final currentCount = current.bonusHistory.length;
return previousCount != currentCount; return previousCount != currentCount;
@ -74,10 +74,10 @@ class _ScoreViewDecoration extends StatelessWidget {
child: DecoratedBox( child: DecoratedBox(
decoration: BoxDecoration( decoration: BoxDecoration(
image: DecorationImage( image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage( image: AssetImage(
Assets.images.score.miniScoreBackground.path, Assets.images.score.miniScoreBackground.path,
), ),
fit: BoxFit.cover,
), ),
), ),
child: child, child: child,
@ -86,6 +86,7 @@ class _ScoreViewDecoration extends StatelessWidget {
} }
} }
// TODO(arturplaczek): implement new Sprite animation when will be merged.
class _AnimationView extends StatelessWidget { class _AnimationView extends StatelessWidget {
const _AnimationView({ const _AnimationView({
Key? key, Key? key,

@ -17,7 +17,7 @@ class ScoreBalls extends StatelessWidget {
return Row( return Row(
children: [ children: [
Text( Text(
l10n.ballCt, l10n.rounds,
style: AppTextStyle.subtitle1.copyWith( style: AppTextStyle.subtitle1.copyWith(
color: AppColors.orange, color: AppColors.orange,
), ),
@ -47,12 +47,14 @@ class ScoreBall extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final color = isActive ? AppColors.orange : AppColors.orange.withAlpha(128); final color = isActive ? AppColors.orange : AppColors.orange.withAlpha(128);
const size = 8.0;
return Padding( return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8), padding: const EdgeInsets.symmetric(horizontal: 8),
child: Container( child: Container(
color: color, color: color,
height: 8, height: size,
width: 8, width: size,
), ),
); );
} }

@ -2,10 +2,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
import 'package:pinball/theme/theme.dart'; import 'package:pinball/theme/theme.dart';
import 'package:pinball_components/pinball_components.dart';
class ScoreView extends StatelessWidget { class ScoreView extends StatelessWidget {
const ScoreView({Key? key}) : super(key: key); const ScoreView({Key? key}) : super(key: key);
@ -72,11 +72,9 @@ class _ScoreText extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final score = context.select((GameBloc bloc) => bloc.state.score); final score = context.select((GameBloc bloc) => bloc.state.score);
final numberFormatter = NumberFormat.decimalPattern('en_US');
final formattedScore = numberFormatter.format(score).replaceAll(',', '.');
return Text( return Text(
formattedScore, score.formatScore(),
style: AppTextStyle.headline1.copyWith( style: AppTextStyle.headline1.copyWith(
color: AppColors.white, color: AppColors.white,
), ),

@ -76,8 +76,8 @@
"@enterInitials": { "@enterInitials": {
"description": "Text displayed on the ending dialog when game finishes to ask the user for his initials" "description": "Text displayed on the ending dialog when game finishes to ask the user for his initials"
}, },
"ballCt": "Ball Ct:", "rounds": "Rounds:",
"@ballCt": { "@rounds": {
"description": "Text displayed on the scoreboard widget" "description": "Text displayed on the scoreboard widget"
} }
} }

@ -73,7 +73,6 @@ void main() {
final state = initialState.copyWith( final state = initialState.copyWith(
bonusHistory: [GameBonus.dashNest], bonusHistory: [GameBonus.dashNest],
); );
stateController.add(state); stateController.add(state);
await tester.pumpApp( await tester.pumpApp(
@ -92,7 +91,6 @@ void main() {
final state = initialState.copyWith( final state = initialState.copyWith(
bonusHistory: [gameBonus], bonusHistory: [gameBonus],
); );
whenListen( whenListen(
gameBloc, gameBloc,
Stream.value(state), Stream.value(state),
@ -100,7 +98,6 @@ void main() {
); );
await _pumpAppWithWidget(tester); await _pumpAppWithWidget(tester);
await tester.pump(); await tester.pump();
expect(find.byType(BonusAnimation), findsOneWidget); expect(find.byType(BonusAnimation), findsOneWidget);
@ -115,7 +112,6 @@ void main() {
final state = initialState.copyWith( final state = initialState.copyWith(
bonusHistory: [GameBonus.dashNest], bonusHistory: [GameBonus.dashNest],
); );
whenListen( whenListen(
gameBloc, gameBloc,
Stream.value(state), Stream.value(state),
@ -123,9 +119,9 @@ void main() {
); );
await _pumpAppWithWidget(tester); await _pumpAppWithWidget(tester);
await tester.pump(); await tester.pump();
// TODO(arturplaczek): remove magic number once this is merged:
// https://github.com/flame-engine/flame/pull/1564
await Future<void>.delayed(const Duration(seconds: 4)); await Future<void>.delayed(const Duration(seconds: 4));
await expectLater(find.byType(ScoreView), findsOneWidget); await expectLater(find.byType(ScoreView), findsOneWidget);

@ -3,9 +3,9 @@ import 'dart:async';
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_components/pinball_components.dart';
import '../../../helpers/helpers.dart'; import '../../../helpers/helpers.dart';
@ -30,11 +30,6 @@ void main() {
); );
}); });
String _formatScore(int score) {
final numberFormatter = NumberFormat.decimalPattern('en_US');
return numberFormatter.format(score).replaceAll(',', '.');
}
group('ScoreView', () { group('ScoreView', () {
testWidgets('renders score', (tester) async { testWidgets('renders score', (tester) async {
await tester.pumpApp( await tester.pumpApp(
@ -43,7 +38,7 @@ void main() {
); );
await tester.pump(); await tester.pump();
expect(find.text(_formatScore(score)), findsOneWidget); expect(find.text(score.formatScore()), findsOneWidget);
}); });
testWidgets('renders game over', (tester) async { testWidgets('renders game over', (tester) async {
@ -70,7 +65,7 @@ void main() {
gameBloc: gameBloc, gameBloc: gameBloc,
); );
expect(find.text(_formatScore(score)), findsOneWidget); expect(find.text(score.formatScore()), findsOneWidget);
final newState = initialState.copyWith( final newState = initialState.copyWith(
score: 987654321, score: 987654321,
@ -80,7 +75,7 @@ void main() {
await tester.pump(); await tester.pump();
expect(find.text(_formatScore(newState.score)), findsOneWidget); expect(find.text(newState.score.formatScore()), findsOneWidget);
}); });
}); });
} }

Loading…
Cancel
Save