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

Flutter Stepper组件条件隐藏步骤问题求助

需求与问题

我希望仅当第一步的API调用返回true时,才显示Stepper的第二步(验证码步骤)。

初始代码

Stepper(
          currentStep: _currentStepper,
          onStepContinue: _continueStep,
          steps: [
            Step(
              title: const Text('Let\'s start with your phone number'),
              // call api
            ),
            Step(// 此步骤仅在第一步返回true时显示
                title: const Text(
                    'Please enter the code sent to your mobile device'),
                content: OTP CODE Step
          ],
        ),


  void _continueStep() {
    if (_currentStepper == 0) {
      if (_formKey.currentState!.validate()) {
        _signUpUser();
        _currentStepper = 1;
      }
    }
  }

应用MendelG提供的方案后问题仍未解决,遇到以下情况:

  • 为Stepper组件添加key后,步骤内的输入控件表现异常;
  • 不添加key时,点击继续按钮会触发错误。

我的实现代码

void _continueStep() {
    if (_currentStepper == 0) {
      if (_formKey.currentState!.validate()) {
        print('Validated');
        GlobalVariables().cleanUserPhoneNumber(_phoneNumber);
        _signUpUser();
        isVisible = !isVisible;
        // _currentStepper = 1;
      }
    }
  }

  Widget build(BuildContext context) {
    final _steps = [
      Step(
        title: const Text("Let\'s start with your phone number"),
        content: _intlPhoneFieldWidget(),
      ),
      Step(
        title: const Text("Please enter the code sent to your mobile device"),
        content: _otpCodeWidget(),
      )
    ];

    return Scaffold(
      body: Form(
        key: _formKey,
        child: Stepper(
          currentStep: _currentStepper,
          onStepContinue: _continueStep,
          steps: isVisible
              ? _steps
              : [
                  Step(
                    title: const Text('Let\'s start with your phone number'),
                    content: _intlPhoneFieldWidget(),
                  )
                ],
        ),
      ),
    );
  }

  Widget _intlPhoneFieldWidget() {
    return Column(
      children: [
        const SizedBox(height: 50),
        SizedBox(
          child: IntlPhoneField(
            decoration: const InputDecoration(
              labelText: 'Phone Number',
            ),
            style: TextStyle(
                fontFamily: GlobalVariables().textDisplayFont,
                fontSize: GlobalVariables().textDisplayFontSize),
            autofocus: true,
            initialCountryCode: 'CA',
            onChanged: (phone) {
              setState(() {
                _phoneNumber = (phone.completeNumber).trim();
              });
            },
          ),
        ),
      ],
    );
  }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 07:01:03