Skip to content
Snippets Groups Projects
Select Git revision
  • bb6f95082764664bd83aed502bf4548130e48246
  • master default protected
  • 32-upgrade-framework-and-dependencies
  • 15-improve-app-metadata
  • Release_0.9.0_28 protected
  • Release_0.8.2_27 protected
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.0_24 protected
  • Release_0.6.0_23 protected
  • Release_0.5.0_22 protected
  • Release_0.4.2_21 protected
  • Release_0.4.1_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.1_18 protected
  • Release_0.3.0_17 protected
  • Release_0.2.1_16 protected
  • Release_0.2.0_15 protected
  • Release_0.1.1_14 protected
  • Release_0.1.0_13 protected
  • Release_0.0.12_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
24 results

settings_game_cubit.dart

Blame
  • game_cubit.dart 1.97 KiB
    import 'package:equatable/equatable.dart';
    import 'package:flutter/material.dart';
    import 'package:hydrated_bloc/hydrated_bloc.dart';
    
    import 'package:sortgame/models/game.dart';
    import 'package:sortgame/models/settings_game.dart';
    import 'package:sortgame/models/settings_global.dart';
    
    part 'game_state.dart';
    
    class GameCubit extends HydratedCubit<GameState> {
      GameCubit()
          : super(GameState(
              currentGame: Game.createNull(),
            ));
    
      void updateState(Game game) {
        emit(GameState(
          currentGame: game,
        ));
      }
    
      void refresh() {
        final Game game = Game(
          items: state.currentGame.items,
          gameSettings: state.currentGame.gameSettings,
          globalSettings: state.currentGame.globalSettings,
          isRunning: state.currentGame.isRunning,
          isFinished: state.currentGame.isFinished,
          position: state.currentGame.position,
          score: state.currentGame.score,
        );
    
        updateState(game);
      }
    
      void quitGame() {
        state.currentGame.updateGameIsRunning(false);
        refresh();
      }
    
      void increasePosition() {
        if (state.currentGame.position < state.currentGame.items.length) {
          state.currentGame.increasePosition();
        } else {
          state.currentGame.updateGameIsFinished(true);
        }
        refresh();
      }
    
      void increaseScore(int increment) {
        state.currentGame.increaseScore(increment);
        refresh();
      }
    
      void startNewGame({
        required GameSettings gameSettings,
        required GlobalSettings globalSettings,
      }) {
        Game newGame = Game.createNew(
          gameSettings: gameSettings,
          globalSettings: globalSettings,
        );
    
        newGame.dump();
    
        updateState(newGame);
        refresh();
      }