You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter survey_kit:如何配置双CompletionStep的跳转与结束逻辑

使用Survey Kit实现问卷跳转与直接结束规则

需求说明

  • 用户在id为1的年龄输入问题中,若年龄<18,跳转至id为11的CompletionStep
  • 年龄≥18时正常完成问卷,到达id为10的CompletionStep后直接结束问卷,跳过id为11的步骤

实现代码

1. 年龄判断的条件导航规则

针对id为1的步骤添加条件导航,根据输入的年龄值跳转对应步骤:

task.addNavigationRule(
  forTriggerStepIdentifier: getStepIdentifier(task, "1"),
  navigationRule: ConditionalNavigationRule(
    resultToStepIdentifierMapper: (input) {
      // 解析用户输入的年龄值
      int age = int.parse(input!);
      if (age < 18) {
        // 年龄小于18,跳转至id为11的CompletionStep
        return getStepIdentifier(task, "11");
      } else {
        // 年龄≥18,继续下一个步骤(id为2)
        return getStepIdentifier(task, "2");
      }
    },
  ),
);

2. id为10的CompletionStep直接结束问卷

为id为10的完成步骤添加直接结束规则,确保点击按钮后直接结束问卷,不会跳转到后续步骤:

// 定义id为10的CompletionStep
CompletionStep(
  stepIdentifier: StepIdentifier(id: '10'),
  text: '感谢参与本次问卷,祝您游戏愉快',
  title: '完成!',
  buttonText: '结束问卷',
);

// 添加直接结束的导航规则
task.addNavigationRule(
  forTriggerStepIdentifier: getStepIdentifier(task, "10"),
  // 设置endSurvey为true,触发直接结束问卷逻辑
  navigationRule: DirectNavigationRule(endSurvey: true),
);

关键说明

  • getStepIdentifier方法需确保能从task实例中正确匹配对应id的StepIdentifier
  • DirectNavigationRule(endSurvey: true)是Survey Kit中直接结束问卷的标准写法,无需传入字符串参数,通过endSurvey标记即可触发结束流程

内容的提问来源于stack exchange,提问作者Felpower

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:00:11