In this section, we will wrap each piece with a draggable so that the user is able to drag the chess pieces to their desired position. Let's look at the implementation in detail:
- Recall that we declared a hashmap to store the positions of the pieces. A move will consist of moving a piece from one box and placing it in another. Suppose we have two variables, 'from' and 'to', that store the indexes of the boxes for moving a piece. When a move is made, we pick up the piece that was at 'from' and put it in 'to'. Due to this, the box at 'from' becomes empty. Following the same logic, we'll define the refreshBoard() method, which is called every time a move is made:
void refreshBoard(String from, String to) { setState(() ...