diff --git a/doc/tutorials/klondike/step3.md b/doc/tutorials/klondike/step3.md index 71124690b33..3669ad6aad9 100644 --- a/doc/tutorials/klondike/step3.md +++ b/doc/tutorials/klondike/step3.md @@ -55,13 +55,14 @@ symbol on the canvas. The sprite object is initialized using the ``` Then comes the static list of all `Suit` objects in the game. Note that we -define it as `late`, meaning that it will be only initialized the first time -it is needed. This is important: as we seen above, the constructor tries to -retrieve an image from the global cache, so it can only be invoked after the -image is loaded into the cache. +define it as static variable so it is evaluated lazily (as if it was marked +with the `late` keyword) meaning that it will be only initialized the first +time it is needed. This is important: as we can see above, the constructor +tries to retrieve an image from the global cache, so it can only be invoked +after the image is loaded into the cache. ```dart - static late final List _singletons = [ + static final List _singletons = [ Suit._(0, '♥', 1176, 17, 172, 183), Suit._(1, '♦', 973, 14, 177, 182), Suit._(2, '♣', 974, 226, 184, 172), @@ -120,7 +121,7 @@ class Rank { final Sprite redSprite; final Sprite blackSprite; - static late final List _singletons = [ + static final List _singletons = [ Rank._(1, 'A', 335, 164, 789, 161, 120, 129), Rank._(2, '2', 20, 19, 15, 322, 83, 125), Rank._(3, '3', 122, 19, 117, 322, 80, 127), @@ -272,7 +273,7 @@ Various properties used in the `_renderBack()` method are defined as follows: const Radius.circular(KlondikeGame.cardRadius), ); static final RRect backRRectInner = cardRRect.deflate(40); - static late final Sprite flameSprite = klondikeSprite(1367, 6, 357, 501); + static final Sprite flameSprite = klondikeSprite(1367, 6, 357, 501); ``` I declared these properties as static because they will all be the same across @@ -307,9 +308,9 @@ depending on whether the card is of a "red" suit or "black": Next, we also need the images for the court cards: ```dart - static late final Sprite redJack = klondikeSprite(81, 565, 562, 488); - static late final Sprite redQueen = klondikeSprite(717, 541, 486, 515); - static late final Sprite redKing = klondikeSprite(1305, 532, 407, 549); + static final Sprite redJack = klondikeSprite(81, 565, 562, 488); + static final Sprite redQueen = klondikeSprite(717, 541, 486, 515); + static final Sprite redKing = klondikeSprite(1305, 532, 407, 549); ``` Note that I'm calling these sprites `redJack`, `redQueen`, and `redKing`. This @@ -325,11 +326,11 @@ blending mode: Color(0x880d8bff), BlendMode.srcATop, ); - static late final Sprite blackJack = klondikeSprite(81, 565, 562, 488) + static final Sprite blackJack = klondikeSprite(81, 565, 562, 488) ..paint = blueFilter; - static late final Sprite blackQueen = klondikeSprite(717, 541, 486, 515) + static final Sprite blackQueen = klondikeSprite(717, 541, 486, 515) ..paint = blueFilter; - static late final Sprite blackKing = klondikeSprite(1305, 532, 407, 549) + static final Sprite blackKing = klondikeSprite(1305, 532, 407, 549) ..paint = blueFilter; ``` diff --git a/doc/tutorials/klondike/step4.md b/doc/tutorials/klondike/step4.md index 2e9a0491879..00371993d0f 100644 --- a/doc/tutorials/klondike/step4.md +++ b/doc/tutorials/klondike/step4.md @@ -576,19 +576,21 @@ it so that it would check whether the card is allowed to be moved before startin ```dart void onDragStart(DragStartEvent event) { if (pile?.canMoveCard(this) ?? false) { - _isDragging = true; + super.onDragStart(event); priority = 100; } } ``` -We have also added the boolean `_isDragging` variable here: make sure to define it, and then to -check this flag in the `onDragUpdate()` method, and to set it back to false in the `onDragEnd()`: +We have also added a call to `super.onDragStart()` which sets an `_isDragged` variable to `true` +in the `DragCallbacks` mixin, we need to check this flag via the public `isDragged` getter in +the `onDragUpdate()` method and use `super.onDragEnd()` in `onDragEnd()` so the flag is set back +to `false`: ```dart @override void onDragUpdate(DragUpdateEvent event) { - if (!_isDragging) { + if (!isDragged) { return; } final cameraZoom = (findGame()! as FlameGame) @@ -600,11 +602,11 @@ check this flag in the `onDragUpdate()` method, and to set it back to false in t @override void onDragEnd(DragEndEvent event) { - _isDragging = false; + super.onDragEnd(event); } ``` -Now the only the proper cards can be dragged, but they still drop at random positions on the table, +Now only the proper cards can be dragged, but they still drop at random positions on the table, so let's work on that. @@ -620,10 +622,10 @@ Thus, my first attempt at revising the `onDragEnd` callback looks like this: ```dart @override void onDragEnd(DragEndEvent event) { - if (!_isDragging) { + if (!isDragged) { return; } - _isDragging = false; + super.onDragEnd(event); final dropPiles = parent! .componentsAtPoint(position + size / 2) .whereType() @@ -790,10 +792,10 @@ Now, putting this all together, the `Card`'s `onDragEnd` method will look like t ```dart @override void onDragEnd(DragEndEvent event) { - if (!_isDragging) { + if (!isDragged) { return; } - _isDragging = false; + super.onDragEnd(event); final dropPiles = parent! .componentsAtPoint(position + size / 2) .whereType() @@ -898,7 +900,7 @@ Heading back into the `Card` class, we can use this method in order to populate @override void onDragStart(DragStartEvent event) { if (pile?.canMoveCard(this) ?? false) { - _isDragging = true; + super.onDragStart(); priority = 100; if (pile is TableauPile) { attachedCards.clear(); @@ -918,7 +920,7 @@ the `onDragUpdate` method: ```dart @override void onDragUpdate(DragUpdateEvent event) { - if (!_isDragging) { + if (!isDragged) { return; } final cameraZoom = (findGame()! as FlameGame) @@ -952,10 +954,10 @@ attached cards into the pile, and the same when it comes to returning the cards ```dart @override void onDragEnd(DragEndEvent event) { - if (!_isDragging) { + if (!isDragged) { return; } - _isDragging = false; + super.onDragEnd(event); final dropPiles = parent! .componentsAtPoint(position + size / 2) .whereType()