Skip to content

Instantly share code, notes, and snippets.

@deven98
Created January 15, 2019 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deven98/a8aff24bebec5aef06eae95a4430b384 to your computer and use it in GitHub Desktop.
Save deven98/a8aff24bebec5aef06eae95a4430b384 to your computer and use it in GitHub Desktop.
GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: columnCount,
),
itemBuilder: (context, position) {
// Get row and column number of square
int rowNumber = (position / columnCount).floor();
int columnNumber = (position % columnCount);
Image image;
if (openedSquares[position] == false) {
if (flaggedSquares[position] == true) {
image = getImage(ImageType.flagged);
} else {
image = getImage(ImageType.facingDown);
}
} else {
if (board[rowNumber][columnNumber].hasBomb) {
image = getImage(ImageType.bomb);
} else {
image = getImage(
getImageTypeFromNumber(
board[rowNumber][columnNumber].bombsAround),
);
}
}
return InkWell(
// Opens square
onTap: () {
if (board[rowNumber][columnNumber].hasBomb) {
_handleGameOver();
}
if (board[rowNumber][columnNumber].bombsAround == 0) {
_handleTap(rowNumber, columnNumber);
} else {
setState(() {
openedSquares[position] = true;
squaresLeft = squaresLeft - 1;
});
}
if(squaresLeft <= bombCount) {
_handleWin();
}
},
// Flags square
onLongPress: () {
if (openedSquares[position] == false) {
setState(() {
flaggedSquares[position] = true;
});
}
},
splashColor: Colors.grey,
child: Container(
color: Colors.grey,
child: image,
),
);
},
itemCount: rowCount * columnCount,
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment