build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
context
using BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
// determine style
final style = this.style ?? DefaultShogiBoardStyle.of(context).style;
// determine number of columns and rows depending if coordinates should be shown
final numberColumns = style.showCoordIndicators
? BoardConfig.numberColumns + 1
: BoardConfig.numberColumns;
final numberRows = style.showCoordIndicators
? BoardConfig.numberRows + 1
: BoardConfig.numberRows;
return LayoutBuilder(
builder: (_, constraints) {
// determine the maximum size of the board. this is the min of the style.maxSize and device/layout max width, max height
final maxSize = min(
style.maxSize,
min(
min(constraints.maxWidth, MediaQuery.of(context).size.width),
min(constraints.maxHeight, MediaQuery.of(context).size.height),
),
);
// determine size multiplier
final totalMultiplier =
((BoardConfig.numberRows + (showPiecesInHand ? 2 : 0)) *
_boardCellMultiplier) +
(style.showCoordIndicators ? _coordCellMultiplier : 0);
final sizePerMultiplierUnit = maxSize / totalMultiplier;
// determine the size per board cell and coord cell
final sizeBoardCell = sizePerMultiplierUnit * _boardCellMultiplier;
final sizeCoordCell = sizePerMultiplierUnit * _coordCellMultiplier;
// determine the total width and height of the board
final totalWidth = sizeBoardCell * BoardConfig.numberColumns +
(style.showCoordIndicators ? sizeCoordCell : 0);
final totalHeight = sizeBoardCell * BoardConfig.numberRows +
(style.showCoordIndicators ? sizeCoordCell : 0) +
sizeBoardCell * (showPiecesInHand ? 2 : 0);
// determine rows of widgets
final rows = List<Widget>.filled(numberRows, Container());
for (var y = 0; y < numberRows; y++) {
final row = List<Widget>.filled(numberRows, Container());
for (var x = numberColumns - 1; x >= 0; x--) {
final boardPiece = gameBoard.boardPieces.pieceAtPosition(
column: style.showCoordIndicators ? x : x + 1,
row: style.showCoordIndicators ? y : y + 1,
);
// if should show coord and top row/first column, assign CoordIndicatorCell else BoardCell
row[numberColumns - 1 - x] =
style.showCoordIndicators && (y == 0 || x == 0)
? CoordIndicatorCell(
size: sizeCoordCell,
coord: y == 0 ? x : y,
isTop: y == 0,
coordIndicatorType: style.coordIndicatorType,
color: style.borderColor,
)
: BoardCell(
size: sizeBoardCell,
edge: Edge(
top: y == (style.showCoordIndicators ? 1 : 0),
bottom: y == numberRows - 1,
left: x == numberColumns - 1,
right: x == (style.showCoordIndicators ? 1 : 0),
),
cellColor: style.cellColor,
borderColor: style.borderColor,
child: boardPiece != null
? Piece(
boardPiece: boardPiece.displayString(
usesJapanese: style.usesJapanese),
isSente: boardPiece.isSente,
size: sizeBoardCell,
pieceColor: boardPiece.isPromoted
? style.promotedPieceColor
: style.pieceColor,
)
: null,
);
}
// if top row and should show coord indicators, then wrap in an additional row to allow correct spacing
rows[y] = style.showCoordIndicators && y == 0
? Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: row.take(numberRows - 1).toList(),
),
),
row.last,
],
)
: Row(
children: row,
);
}
// construct board
return SizedBox(
width: totalWidth,
height: totalHeight,
child: Column(
children: <Widget>[
if (showPiecesInHand)
_PiecesInHand(
pieces: gameBoard.goteOrderedPiecesInHand
.map((p) =>
p.displayString(usesJapanese: style.usesJapanese))
.toList()
.convertToMapWithCountUniqueElements(),
isSente: false,
size: sizeBoardCell,
pieceColor: style.pieceColor,
rightEdgeSpacer:
style.showCoordIndicators ? sizeCoordCell : 0,
),
...rows,
if (showPiecesInHand)
_PiecesInHand(
pieces: gameBoard.senteOrderedPiecesInHand
.map((p) =>
p.displayString(usesJapanese: style.usesJapanese))
.toList()
.convertToMapWithCountUniqueElements(),
isSente: true,
size: sizeBoardCell,
pieceColor: style.pieceColor,
rightEdgeSpacer:
style.showCoordIndicators ? sizeCoordCell : 0,
),
],
),
);
},
);
}