Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ChessboardEditor): support highlighting squares #47

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading