survey_kit icon indicating copy to clipboard operation
survey_kit copied to clipboard

Custom Step / Custom Slider

Open pmagnuson opened this issue 2 years ago • 5 comments

I am finding it quite difficult to navigate the documentation of CustomResult & CustomStep.

My goal is to create a custom Slider instead of ScaledAnswerFormat.

Any examples would be greatly appreciated.

pmagnuson avatar Sep 19 '23 23:09 pmagnuson

I have defined the following as a custom step in a NavigableTask

// avoid collisions on Step class
import 'package:survey_kit/survey_kit.dart' as k;

class CustomScaleStep extends k.Step {
  final sliderKey = GlobalKey<SliderTextState>();
  @override
  Widget createView({required k.QuestionResult? questionResult}) {
    return k.StepView(
        step: k.QuestionStep(
            showAppBar: false,
            answerFormat: const k.IntegerAnswerFormat(
              defaultValue: 5,
            )),
        title: const Text('title'),
        child: SliderText(key: sliderKey),  // custom component
        resultFunction: () => k.IntegerQuestionResult(
            id: k.Identifier(id: 'identifier'),
            startDate: DateTime.now(),
            endDate: DateTime.now(),
            valueIdentifier: 'valueIdentifier',
            result: sliderKey.currentState!.sliderCurrentValue.toInt()));
  }

  @override
  Map<String, dynamic> toJson() {
    return {};
  }
}

The showAppBar: false, seems to be ignored.

pmagnuson avatar Sep 20 '23 14:09 pmagnuson

With the following custom Step:


// avoid collision with material Step
import 'package:survey_kit/survey_kit.dart' as sk;

class CustomScaleStep extends sk.Step {
  final sliderKey = GlobalKey<SliderTextState>();

  @override
  Widget createView({required sk.QuestionResult? questionResult}) {
    return sk.StepView(
        step: sk.QuestionStep(
            // showAppBar: false,
            answerFormat: const sk.IntegerAnswerFormat(
          defaultValue: 5,
        )),
        title: const Text('title'),
        child: SliderText(key: sliderKey),
        resultFunction: () {
          print('result ${sliderKey.currentState!.sliderCurrentValue.toInt()}');
          return sk.IntegerQuestionResult(
            id: sk.StepIdentifier(),
            startDate: DateTime.now(),
            endDate: DateTime.now(),
            valueIdentifier:
                sliderKey.currentState!.sliderCurrentValue.toInt().toString(),
            result: sliderKey.currentState!.sliderCurrentValue.toInt(),
          );
        });
  }

  @override
  Map<String, dynamic> toJson() {
    return {};
  }
}

and in the enclosing task, this navigation rule is added:


    task.addNavigationRule(
      forTriggerStepIdentifier: task.steps[1].stepIdentifier,
      navigationRule:
          sk.ConditionalNavigationRule(resultToStepIdentifierMapper: (input) {
        if ((input == '1') || (input == '2')) {
          return null;
        }
        return task.steps[0].stepIdentifier;
      }),
    );

in the ConditionalNavigationRule the input variable reflects the value of valueIdentifier in the CustomScaleStep.

It seems that the choice of naming between valueIdentifier and result could be improved in the resultFunction. It would be more intuitive if input reflected the result parameter.

pmagnuson avatar Sep 22 '23 15:09 pmagnuson

I am also interested in building a CustomStep that will play a video and then move to the next step. Is there a way to trigger the Next button on an arbitrary event? I would be best to disable the Next button or not show until the event has occurred. Thanks!

pmagnuson avatar Sep 22 '23 19:09 pmagnuson

@pmagnuson Hi, are you using CustomStep via json? How are you able to add the json parser for the custom step in survey_kit?

adar2378 avatar Jan 08 '24 07:01 adar2378

@adar2378 I am no longer using this library.

pmagnuson avatar Jan 08 '24 17:01 pmagnuson