onTap method
Implementation
void onTap(int index) async {
if (state.isLoading) {
return;
}
final originalOpenedIndexes = state.openedIndexes;
final openedIndexes = [...originalOpenedIndexes, index];
final isFirst = openedIndexes.length % 2 == 1;
final url = state.items[index];
state = state.copyWith(
openedIndexes: openedIndexes,
isLoading: !isFirst,
);
await Future.delayed(const Duration(milliseconds: 500));
if (state.openedIndexes.length == state.items.length) {
completeGame();
}
if (!isFirst) {
final hasDublicate = originalOpenedIndexes.any(
(i) => state.items[i] == url,
);
if (!hasDublicate) {
final newOpenedIndexes = List.of(originalOpenedIndexes)..removeLast();
state = state.copyWith(
openedIndexes: newOpenedIndexes,
isLoading: false,
);
} else {
state = state.copyWith(
isLoading: false,
);
}
}
}