Skip to content
Snippets Groups Projects
Select Git revision
  • 2cb4e009b3fd1aea0d8b1762e5d58ae65ed1eab1
  • master default protected
  • 61-upgrade-framework-and-dependencies
  • 44-improve-app-metadata
  • Release_0.10.0_57 protected
  • Release_0.9.2_56 protected
  • Release_0.9.1_55 protected
  • Release_0.9.0_54 protected
  • Release_0.8.0_53 protected
  • Release_0.7.0_52 protected
  • Release_0.6.0_51 protected
  • Release_0.5.3_50 protected
  • Release_0.5.2_49 protected
  • Release_0.5.1_48 protected
  • Release_0.5.0_47 protected
  • Release_0.4.1_46 protected
  • Release_0.4.0_45 protected
  • Release_0.3.1_44 protected
  • Release_0.3.0_43 protected
  • Release_0.2.1_42 protected
  • Release_0.2.0_41 protected
  • Release_0.1.19_40 protected
  • Release_0.1.18_39 protected
  • Release_0.1.17_38 protected
24 results

cell.dart

Blame
  • cell.dart 2.74 KiB
    import 'package:flutter/material.dart';
    
    import '../provider/data.dart';
    import '../utils/board_utils.dart';
    
    class Cell {
      bool isMined = false;
      bool isExplored = false;
      bool isMarked = false;
      bool isExploded = false;
      int minesCountAround = 0;
    
      Cell(
        @required this.isMined,
      );
    
      Container widget(Data myProvider, int row, int col) {
        bool showSolution = myProvider.gameWin || myProvider.gameFail;
    
        Color backgroundColor = this.getBackgroundColor(myProvider);
        String imageAsset = this.getImageAssetName(myProvider.skin, showSolution);
    
        return Container(
          decoration: BoxDecoration(
            color: backgroundColor,
            border: Border.all(
              color: Colors.grey,
              width: 1,
            ),
          ),
          child: GestureDetector(
            child: Image(
              image: AssetImage(imageAsset),
              fit: BoxFit.fill,
            ),
            onTap: () {
              if (!myProvider.isBoardMined) {
                myProvider.updateCells(BoardUtils.createBoard(myProvider, row, col));
                myProvider.updateIsBoardMined(true);
              }
    
              if (!(myProvider.gameWin || myProvider.gameFail)) {
                if (myProvider.reportMode) {
                  BoardUtils.reportCell(myProvider, row, col);
                } else {
                  BoardUtils.walkOnCell(myProvider, row, col);
                }
              }
              BoardUtils.checkGameIsFinished(myProvider);
            },
          ),
        );
      }
    
      String getImageAssetName(String skin, bool showSolution) {
        String imageAsset = 'assets/skins/' + skin + '_tile_unknown.png';
    
        if (!showSolution) {
          // Running game
          if (this.isExplored) {
            if (this.isMined) {
              // Boom
              imageAsset = 'assets/skins/' + skin + '_tile_mine.png';
            } else {
              // Show mines count around
              imageAsset = 'assets/skins/' + skin + '_tile_' + this.minesCountAround.toString() + '.png';
            }
          } else {
            if (this.isMarked) {
              // Danger!
              imageAsset = 'assets/skins/' + skin + '_tile_flag.png';
            }
          }
        } else {
          // Finished game
          if (this.isMined) {
            if (this.isExploded) {
              // Mine exploded
              imageAsset = 'assets/skins/' + skin + '_tile_mine.png';
            } else {
              // Mine not found
              imageAsset = 'assets/skins/' + skin + '_tile_mine_not_found.png';
            }
          } else {
            // Show all mines counts
            imageAsset = 'assets/skins/' + skin + '_tile_' + this.minesCountAround.toString() + '.png';
          }
        }
    
        return imageAsset;
      }
    
      Color getBackgroundColor(Data myProvider) {
        if (myProvider.gameWin) {
          return Colors.green[300];
        } else if (myProvider.gameFail) {
          return Colors.pink[200];
        }
    
        return Colors.white;
      }
    }