Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Step / Custom Slider #131

Open
pmagnuson opened this issue Sep 19, 2023 · 5 comments
Open

Custom Step / Custom Slider #131

pmagnuson opened this issue Sep 19, 2023 · 5 comments

Comments

@pmagnuson
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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!

@adar2378
Copy link
Contributor

adar2378 commented Jan 8, 2024

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

@pmagnuson
Copy link
Author

@adar2378 I am no longer using this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants