Code Monkey home page Code Monkey logo

Comments (4)

Numoy avatar Numoy commented on June 12, 2024

Hi, first of all thanks for your feedback.

I tried a similar example by wrapping the "getTask" method in the example with a future and used that in the FutureBuilder. It works completly fine for me.

Maybe your build method with the FutureBuilder is called several times, so your backend is called more than once, which could result in a rebuild of the survey.
Hope that helps you.

from survey_kit.

 avatar commented on June 12, 2024

from survey_kit.

Numoy avatar Numoy commented on June 12, 2024

So i tried it again with your code sample. I modified it a little bit but it should be quite similar to your example:

import 'package:flutter/material.dart';
import 'package:survey_kit/survey_kit.dart' as Survey;

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SurveyForm(),
      ),
    );
  }
}

class SurveyForm extends StatefulWidget {
  @override
  _SurveyFormState createState() => _SurveyFormState();
}

class _SurveyFormState extends State<SurveyForm> {
  Future<List<Survey.Step>> getQuestionSteps() async {
    List<Survey.Step> questionSteps = List.filled(
        1,
        Survey.InstructionStep(
          title: 'Your journey starts here',
          text:
              'In order to identify actions and possibilities for the coaching process, the coachee is asked to answer the following questions. This is an open exploration, the coach presents himself totally available and at the service of the coachee, to find out their current energy for change, in intensity and direction.	',
          buttonText: 'START QUESTIONNAIRE',
        ),
        growable: true);
    final _questions = [
      Survey.QuestionStep(
        title: 'How old are you?',
        answerFormat: Survey.IntegerAnswerFormat(
          defaultValue: 25,
          hint: 'Please enter your age',
        ),
      ),
      Survey.QuestionStep(
        title: 'Medication?',
        text: 'Are you using any medication',
        answerFormat: Survey.BooleanAnswerFormat(
          positiveAnswer: 'Yes',
          negativeAnswer: 'No',
          result: Survey.BooleanResult.POSITIVE,
        ),
      ),
      Survey.QuestionStep(
        title: 'Tell us about you',
        text: 'Tell us about yourself and why you want to improve your health.',
        answerFormat: Survey.TextAnswerFormat(
          maxLines: 5,
        ),
      ),
      Survey.QuestionStep(
        title: 'Select your body type',
        answerFormat: Survey.ScaleAnswerFormat(
          step: 1,
          minimumValue: 1,
          maximumValue: 5,
          defaultValue: 3,
          minimumValueDescription: '1',
          maximumValueDescription: '5',
        ),
      ),
    ];
    questionSteps.addAll(_questions);
    questionSteps.add(Survey.CompletionStep(
      id: Survey.StepIdentifier(id: '1123123'),
      title: 'You are done',
      text: 'You have finished !!!',
      buttonText: 'Submit survey',
    ));
    return questionSteps;
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return MaterialApp(
      home: Scaffold(
          body: FutureBuilder<List<Survey.Step>>(
        future: getQuestionSteps(),
        builder: (ctx, snapshot) {
          // Checking if future is resolved or not
          if (snapshot.connectionState == ConnectionState.done) {
            // If we got an error
            if (snapshot.hasError) {
              return Center(
                child: Text(
                  '${snapshot.error} occured',
                  style: TextStyle(fontSize: 18),
                ),
              );
              // if we got our data
            } else if (snapshot.hasData) {
              // Extracting data from snapshot object
              final questionSteps = snapshot.data!;
              return Survey.SurveyKit(
                  onResult: (Survey.SurveyResult result) {
                    print(result.finishReason);
                  },
                  task: getSampleTask(questionSteps));
            }
          }
          // Displaying LoadingSpinner to indicate waiting state
          return Center(
            child: CircularProgressIndicator(),
          );
        },
      )),
    );
  }

  Survey.Task getSampleTask(List<Survey.Step> _questionSteps) {
    var task = Survey.NavigableTask(
      id: Survey.TaskIdentifier(),
      steps: _questionSteps,
    );
    task.addNavigationRule(
      forTriggerStepIdentifier: task.steps[2].id,
      navigationRule: Survey.ConditionalNavigationRule(
        resultToStepIdentifierMapper: (input) {
          switch (input) {
            case "Yes":
              return task.steps[0].id;
            case "No":
              return task.steps[3].id;
            default:
              return null;
          }
        },
      ),
    );
    return task;
  }
}

It still does work for me. One thing i noticed is that you have some navigation rules in place. If on the third step the user types/clicks "Yes" (The valueIdentifier) the user goes back to the first Step (InstructionsStep). Maybe that is the reason?

 task.addNavigationRule(
      forTriggerStepIdentifier: task.steps[2].id,
      navigationRule: Survey.ConditionalNavigationRule(
        resultToStepIdentifierMapper: (input) {
          switch (input) {
            case "Yes":
              return task.steps[0].id;
            case "No":
              return task.steps[3].id;
            default:
              return null;
          }
        },
      ),
    );

from survey_kit.

 avatar commented on June 12, 2024

from survey_kit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.