Skip to content

Instantly share code, notes, and snippets.

@montyr75
Created April 6, 2020 18:59
Show Gist options
  • Save montyr75/8e2ef7e1591e4446dc1b7b6a082c4f46 to your computer and use it in GitHub Desktop.
Save montyr75/8e2ef7e1591e4446dc1b7b6a082c4f46 to your computer and use it in GitHub Desktop.
Calculate grid position in 1D model
// If you have an array or string containing cell data for a "grid",
// you can calculate x and y coords using division and the row length.
// define the row length in the virtual grid
const rowLength = 8;
// create a 1D model representing the "grid"
final onScreenKeyboard = 'abcde123fghij456klmno789pqrst.@0uvwxyz_/^ ';
// this function will return the grid position of the given content (content must be unique)
Point getPos(String grid, String content) {
final contentIndex = grid.indexOf(content);
return Point(contentIndex ~/ rowLength, contentIndex % rowLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment