onEvent method

void onEvent(
  1. Map<Question, String?> questionState,
  2. String? answer
)

This method is used to used to set the answers or states of the question cards as and when the user taps on the choices.

If there are instances of NestedQuestion its children will be expanded or unpacked based on the answer. The parameter questionState contains the old state of the question. The answer parameter contains the new answer selected by the user.

Implementation

void onEvent(Map<Question, String?> questionState, String? answer) {
  int location = 0;
  if ((location = _expanded.indexOf(questionState)) != -1) {
    _expanded[location][questionState.keys.toList()[0]] = answer;

    if (questionState.keys.toList()[0] is PolarQuestion)
      (questionState.keys.toList()[0] as PolarQuestion).hasError = false;

    if (questionState.keys.toList()[0] is NestedQuestion) {
      if (((questionState.keys.toList()[0] as NestedQuestion).children) !=
              null &&
          questionState[questionState.keys.toList()[0]] != null) {
        (questionState.keys.toList()[0] as NestedQuestion)
            .children!
            .forEach((key, value) {
          if (key != answer) {
            int count =
                location + 1; //location value points after nested question
            value.forEach((element) {
              if (count < _expanded.length &&
                      _expanded[count].containsKey(element) ||
                  count < _expanded.length &&
                      _expanded[count].keys.toList()[0].parent != null &&
                      _expanded[count]
                          .keys
                          .toList()[0]
                          .parent!
                          .containsKey(questionState.keys.toList()[0])) {
                _expanded.removeAt(count);
              }
            });
          }
        });
        for (int i = location + 1; i < _expanded.length; i++) {
          if (location + 1 < _expanded.length &&
              _expanded[location + 1].keys.toList()[0].parent != null &&
              _expanded[location + 1]
                  .keys
                  .toList()[0]
                  .parent!
                  .containsKey(questionState.keys.toList()[0])) {
            _expanded.removeAt(location + 1);
          }
        }
        (questionState.keys.toList()[0] as NestedQuestion)
            .children!
            .forEach((key, value) {
          if (key == answer) {
            int count = location + 1;
            value.forEach((element) {
              Map<Question, String?> temp = new Map<Question, String?>();
              element.parent =
                  _expanded[location].keys.toList()[0].parent == null
                      ? _expanded[location]
                      : _expanded[location].keys.toList()[0].parent;
              if (!(element is NestedQuestion || element is PolarQuestion))
                element.answer = new TextEditingController();
              temp[element] = null;

              _expanded.insert(count++, temp);
            });
          }
        });
      }
    }
  }

  notifyListeners();
}