This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove BlocBuilder from GamePage (#110)
- Loading branch information
Showing
3 changed files
with
85 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/trashy_road/lib/src/game/widgets/trashy_road_game_widget.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flame/game.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:trashy_road/src/game/game.dart'; | ||
import 'package:trashy_road/src/loading/loading.dart'; | ||
|
||
final _random = Random(0); | ||
|
||
class TrashyRoadGameWidget extends StatefulWidget { | ||
const TrashyRoadGameWidget({super.key}); | ||
|
||
@override | ||
State<TrashyRoadGameWidget> createState() => _TrashyRoadGameWidgetState(); | ||
} | ||
|
||
class _TrashyRoadGameWidgetState extends State<TrashyRoadGameWidget> { | ||
TrashyRoadGame? _game; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final gameBloc = context.read<GameBloc>(); | ||
final loadingBloc = context.read<PreloadCubit>(); | ||
|
||
TrashyRoadGame gameBuilder() { | ||
return kDebugMode | ||
? DebugTrashyRoadGame( | ||
gameBloc: gameBloc, | ||
images: loadingBloc.images, | ||
random: _random, | ||
) | ||
: TrashyRoadGame( | ||
gameBloc: gameBloc, | ||
images: loadingBloc.images, | ||
random: _random, | ||
); | ||
} | ||
|
||
_game ??= gameBuilder(); | ||
|
||
return BlocListener<GameBloc, GameState>( | ||
listenWhen: (previous, current) { | ||
final hasLost = previous.status == GameStatus.playing && | ||
current.status == GameStatus.resetting; | ||
return hasLost; | ||
}, | ||
listener: (context, state) { | ||
if (!mounted) return; | ||
setState(() => _game = gameBuilder()); | ||
gameBloc.add(const GameReadyEvent()); | ||
}, | ||
child: GameWidget(game: _game!), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export 'inventory_hud.dart'; | ||
export 'trashy_road_game_widget.dart'; |