Skip to content

Commit

Permalink
Merge pull request #47 from tom-anders/boardEditorHighlights
Browse files Browse the repository at this point in the history
feat(ChessboardEditor): support highlighting squares
  • Loading branch information
veloce authored Aug 13, 2024
2 parents 773e7db + 92e1db7 commit a279336
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.1.0

- `ChessboardEditor` now supports highlighting squares

## 4.0.0

### New features:
Expand Down
22 changes: 20 additions & 2 deletions lib/src/widgets/board_editor.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:chessground/chessground.dart';
import 'package:chessground/src/widgets/geometry.dart';
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/widgets.dart';

import '../board_settings.dart';
Expand Down Expand Up @@ -42,6 +44,7 @@ class ChessboardEditor extends StatefulWidget with ChessboardGeometry {
required this.pieces,
this.pointerMode = EditorPointerMode.drag,
this.settings = const ChessboardEditorSettings(),
this.squareHighlights = const IMap.empty(),
this.onEditedSquare,
this.onDroppedPiece,
this.onDiscardedPiece,
Expand All @@ -66,6 +69,8 @@ class ChessboardEditor extends StatefulWidget with ChessboardGeometry {
/// The current mode of the pointer tool.
final EditorPointerMode pointerMode;

final IMap<Square, SquareHighlight> squareHighlights;

/// Called when the given square was edited by the user.
///
/// This is called when the user touches or hover over a square while in edit
Expand Down Expand Up @@ -176,6 +181,19 @@ class _BoardEditorState extends State<ChessboardEditor> {
: widget.settings.colorScheme.blackCoordBackground
: widget.settings.colorScheme.background;

final List<Widget> highlightedBackground = [
background,
for (final MapEntry(key: square, value: highlight)
in widget.squareHighlights.entries)
PositionedSquare(
key: ValueKey('${square.name}-highlight'),
size: widget.size,
orientation: widget.orientation,
square: square,
child: highlight,
),
];

return SizedBox.square(
dimension: widget.size,
child: GestureDetector(
Expand All @@ -193,10 +211,10 @@ class _BoardEditorState extends State<ChessboardEditor> {
borderRadius: widget.settings.borderRadius,
boxShadow: widget.settings.boxShadow,
),
child: background,
child: Stack(children: highlightedBackground),
)
else
background,
...highlightedBackground,
...squareWidgets,
],
),
Expand Down

0 comments on commit a279336

Please sign in to comment.