Select Git revision
-
Benoît Harrault authoredBenoît Harrault authored
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;
}
}