From 1b21065c518cd201d6d8d3325714c90bb1dff55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr> Date: Wed, 8 Jun 2022 18:32:17 +0200 Subject: [PATCH] Add animation on fill cells with color --- android/gradle.properties | 4 ++-- lib/entities/cell.dart | 14 ++++---------- lib/utils/board_utils.dart | 35 +++++++++++++++++++++++++---------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index 4bb5439..6bf54a6 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,5 +1,5 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true -app.versionName=0.0.9 -app.versionCode=9 +app.versionName=0.0.10 +app.versionCode=10 diff --git a/lib/entities/cell.dart b/lib/entities/cell.dart index 9129f4d..5a9a347 100644 --- a/lib/entities/cell.dart +++ b/lib/entities/cell.dart @@ -14,16 +14,10 @@ class Cell { String imageAsset = this.getImageAssetName(myProvider); return Container( - child: AnimatedSwitcher( - duration: const Duration(milliseconds: 100), - transitionBuilder: (Widget child, Animation<double> animation) { - return ScaleTransition(child: child, scale: animation); - }, - child: Image( - image: AssetImage(imageAsset), - fit: BoxFit.fill, - key: ValueKey<int>(imageAsset.hashCode), - ), + child: Image( + image: AssetImage(imageAsset), + fit: BoxFit.fill, + key: ValueKey<int>(imageAsset.hashCode), ), ); } diff --git a/lib/utils/board_utils.dart b/lib/utils/board_utils.dart index 21a8922..c638652 100644 --- a/lib/utils/board_utils.dart +++ b/lib/utils/board_utils.dart @@ -1,4 +1,5 @@ import 'dart:math'; +import 'dart:async'; import '../entities/cell.dart'; import '../provider/data.dart'; @@ -56,16 +57,30 @@ class BoardUtils { List cellsToFill = BoardUtils.getSiblingFillableCells(myProvider, 0, 0, [[0, 0]]); int progressBeforeMove = cellsToFill.length; - for (var cellIndex = 0; cellIndex < cellsToFill.length; cellIndex++) { - myProvider.updateCellValue(cellsToFill[cellIndex][1], cellsToFill[cellIndex][0], value); - } - - int progressAfterMove = BoardUtils.getSiblingFillableCells(myProvider, 0, 0, [[0, 0]]).length; - int progressDelta = progressAfterMove - progressBeforeMove; - myProvider.updateProgressDelta(progressDelta); - myProvider.updateProgress(progressAfterMove); - - myProvider.incrementMovesCount(); + // Sort cells from the closest to the furthest, relatively to the top left corner + cellsToFill.sort((a, b) => (pow(a[0], 2) + pow(a[1], 2)).compareTo(pow(b[0], 2) + pow(b[1], 2))); + + Timer _timerAnimateBoard; + const interval = const Duration(milliseconds: 10); + int cellIndex = 0; + _timerAnimateBoard = new Timer.periodic( + interval, + (Timer timer) { + if (cellIndex < cellsToFill.length) { + myProvider.updateCellValue(cellsToFill[cellIndex][1], cellsToFill[cellIndex][0], value); + cellIndex++; + } else { + timer.cancel(); + + int progressAfterMove = BoardUtils.getSiblingFillableCells(myProvider, 0, 0, [[0, 0]]).length; + int progressDelta = progressAfterMove - progressBeforeMove; + myProvider.updateProgressDelta(progressDelta); + myProvider.updateProgress(progressAfterMove); + + myProvider.incrementMovesCount(); + } + }, + ); } static List getSiblingFillableCells(Data myProvider, row, col, siblingCells) { -- GitLab