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

Flutter开发:如何禁用Stepper组件的上一步点击操作

解决Flutter Stepper禁用回退上一步的问题

很简单,你只需要在onStepTapped回调里加个判断,拦截掉回退到之前步骤的操作就行。

核心思路

用户点击步骤标题切换时,onStepTapped会触发并传入目标步骤的索引。我们只需要限制只能跳转到当前步骤或后续步骤,不允许回到之前的步骤,就能实现你要的效果。

修改后的代码

把你原来的onStepTapped部分替换成下面的逻辑:

Form(
  key: _formKey,
  child: Stepper(
    steps: steps(),
    currentStep: currStep,
    onStepContinue: () {
      setState(() {
        if (formKeys[currStep].currentState.validate()) {
          if (currStep < steps().length - 1) {
            currStep += 1;
          } else if (steps().length == 2) {
            print('Done');
          } else {
            currStep = 0;
          }
        }
      });
    },
    // 修改这里的onStepTapped逻辑
    onStepTapped: (step) {
      setState(() {
        // 只允许前进或停留在当前步骤,禁止回退
        if (step >= currStep) {
          currStep = step;
        }
        print(step);
      });
    },
  ),
)

额外说明

如果你只需要针对特定步骤做限制(比如只有步骤2不能回步骤1,其他步骤允许回退),可以把判断条件改成更精准的:

onStepTapped: (step) {
  setState(() {
    // 当当前处于步骤2时,不允许跳转到步骤1
    if (!(currStep == 1 && step == 0)) {
      currStep = step;
    }
    print(step);
  });
},

这样就完美解决了你的问题,现在处于步骤2时,点击步骤1的标题不会再切换回去啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:07:36