checkSuccessAndUpdate function

void checkSuccessAndUpdate(
  1. BuildContext context,
  2. int activeId,
  3. int activeKey
)

This function is the foundation of the logic behind the test. When called, it checks if any key has been pressed and if so it executes the corresponding instructions.

Its arguments are:

activeId, which is the integer of the symbol currently in the middle of the screen

activeKey, which is the number of the last key pressed

context to access the providers

First it checks the KeyboardProvider.keyFlag to see if a key has been pressed. If true, it sets the flag to false and adds the current time to TimeProvider.partialTimes. Then it checks if the key pressed is the correct one or not, and adds one to the ProgressProvider.progressCounter which counts the correct symbols, or to the ProgressProvider.mistakesCounter. After that, calls SymbolsProvider.changeActiveId to update the central symbol and if it is the trial test increments the SymbolsProvider.trialCounter. It also increments the ProgressProvider.symbolsDisplayed.

The providers are instanced with listen:false because the function is outside the widget tree and it does not need to react to changes in the providers.

Implementation

void checkSuccessAndUpdate(BuildContext context,
    int activeId,
    int activeKey) async{
  final progressProvider = Provider.of<ProgressProvider>(context, listen:false);
  final keyboardProvider = Provider.of<KeyboardProvider>(context, listen: false);
  final parametersProvider = Provider.of<ParametersProvider>(context, listen: false);
  final symbolsProvider = Provider.of<SymbolsProvider>(context, listen: false);
  final timeProvider = Provider.of<TimeProvider>(context, listen: false);


  // Comprobar si se ha presionado el teclado
  if (keyboardProvider.keyFlag) {
    keyboardProvider.setFlag(false);
    timeProvider.addPartialTime(timeProvider.elapsedMilliseconds);
    //Verificar si la tecla presionada coincide con el símbolo activo
    if (activeId == (activeKey - 1)) {
      progressProvider.incrementProgressCounter();
      debugPrint('score: ' + '${progressProvider.progressCounter}');
    }
    else{ //Sumamos los errores
      progressProvider.incrementMistakesCounter(progressProvider.thirdsCounter);
      debugPrint('mistakes: ' + '${progressProvider.totalMistakes}');
    }

    symbolsProvider.changeActiveId(newId: newRandom(activeId, parametersProvider.isTrialTest, symbolsProvider.trialCounter, symbolsProvider.trialOrder)); //Generamos nuevo simbolo
    if(parametersProvider.isTrialTest) {
      symbolsProvider.incrementTrialCounter();
    }
    progressProvider.incrementSymbolsDisplayed(progressProvider.thirdsCounter);
    debugPrint('total: ' + '${progressProvider.totalDisplayed}');
  }


}